PDA

View Full Version : PIC16f628a c-program for 7segment display on hitech picc



chintoo
05-23-2011, 11:33 AM
Hello guys. I've been working on developing a small code for driving a 7seg display by a 16f628a using htech picc compliler in MPLAB IDE. I've made the code and due to some odd reasons, when i debug it i dont see it working properly. I've already discussed with some friends of mine, but to no solution. As far as i know, the code is ok. here it goes:


/*
** Program for displaying numerals and alphabets on a 7segment led display
** PIC16f628A, HiTech PICC
*/


#include<htc.h>

__CONFIG(FOSC_INTOSCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & BOREN_OFF & LVP_OFF & CPD_OFF & CP_OFF);

#define _XTAL_FREQ 4000000 //defined for '__delay_ms()' use

#define h2 RB0 //h2 (horizontal 2): central led
#define l1 RB1 //l1: upper left led
#define h1 RB2 //h1 (horizontal 1): top led
#define l2 RB3 //l2: upper right led
#define l3 RB4 //l3: lower left led
#define h3 RB5 //h3: lowermost horizontal
#define l4 RB6 //l4: lower right


//********** 7seg blink routine *********
roll(char ledl1,char ledl2,char ledl3,char ledl4,char ledh1,char ledh2,char ledh3)
{
INTCON = 0b11000000; //Global Int and Peripheral Int Enable
PIE1 = 0b00000001; //T1IEnable
TMR1IF = 0;
TMR1IE = 1;
TMR1L = 0b00000000;
TMR1H = 0b00010010;
T1CON = 0b00110001; //T2on, prescale 8, INTOSC, time to TMR!IF = 0.52428sec

while(!TMR1IF)
{
if (ledl1)
{ PORTB=1;
l1 = 0;
__delay_ms(5);
}
if (ledl2)
{ PORTB=1;
l2 = 0;
__delay_ms(5);
}
if (ledl3)
{ PORTB=1;
l3 = 0;
__delay_ms(5);
}
if (ledl4)
{ PORTB=1;
l4 = 0;
__delay_ms(5);
}
if (ledh1)
{ PORTB=1;
h1 = 0;
__delay_ms(5);
}
if (ledh2)
{ PORTB=1;
h2 = 0;
__delay_ms(5);
}
if (ledh3)
{ PORTB=1;
h3 = 0;
__delay_ms(5);
}
}

NOP(); //for debugging
}


//*********** numerals' definition *********
n0()
{ roll(1,1,1,1,1,0,1); }
n5()
{ roll(1,0,0,1,1,1,1); }
n9()
{ roll(1,1,0,1,1,1,1); }


main()
{
PORTB = 1;
TRISB = 0;
PORTA = 1;
TRISA = 0;

unsigned int n;

for(n=1;n<4;n++) //startup led blinks
{ h2 = 0;
__delay_ms(500);
PORTB = 1;
__delay_ms(500);
}

while(1)
{ n5(); //display 5
n0();
n9();
}
}

The roll function is the one which drives the display. it gets its inputs from whatever number is intended to be displayed.
When i debug (using mplab sim), the control goes is the following manner:
Starting with main() -> n5() -> roll and then on getting the tmr1 interrupt flag set, it comes back to the starting of main().

Also, for some reason, i cant get the arguments of roll() defined as 'bit' (as the arguments have only two possible values, it'll be good to save some space.)

Any comments/solutions??