PIC Development Board
/*
* Project name:
PIC Development Board
* Copyright
(c) Researchdesignlab.com
* Test configuration:
MCU: PIC16F877A
Dev.Board: PIC
Oscillator: 20.0 MHz
Software: mikroC PRO for PIC v 4.6
*/
unsigned int portb_index,portb_array[4],digit, COUNT=0;
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0xC0;
case 1 : return 0xF9;
case 2 : return 0xA4;
case 3 : return 0xB0;
case 4 : return 0x99;
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
} //case end
}
void interrupt() {
PORTD = 0XFF; // Turn off all 7seg displays
PORTB= portb_array[portb_index]; // bring appropriate value to PORTB
if(portb_index==0)
PORTD = 0XF7;
else if(portb_index==1) // turn on appropriate 7seg. display
PORTD = 0XFB;
else if(portb_index==2) // turn on appropriate 7seg. display
PORTD = 0XFD;
else if(portb_index==3) // turn on appropriate 7seg. display
PORTD = 0XFE;
// increment portd_index
portb_index++ ;
if (portb_index > 4u)
portb_index = 0; // turn on 1st, turn off 2nd 7seg.
TMR0 = 0; // reset TIMER0 value
INTCON = 0x20; // Clear T0IF
}
void display(unsigned int number)
{
digit = number / 1000u ; // extract thousands digit
portb_array[3] = mask(digit); // and store it to PORTB array
digit = (number / 100u) % 10u; // extract hundreds digit
portb_array[2] = mask(digit); // and store it to PORTB array
digit = (number / 10u) % 10u; // extract tens digit
portb_array[1] = mask(digit); // and store it to PORTB array
digit = number % 10u; // extract ones digit
portb_array[0] = mask(digit); // and store it to PORTB array
}
void main() {
TRISD=0X00;
TRISB=0X00;
OPTION_REG = 0x80; // Timer0 settings
digit = 0;
portb_index = 0;
TMR0 = 0;
INTCON = 0xA0; // Enable GIE, T0IE
PORTD = 0xFF;
Delay_ms(200);
COUNT=0;
while(1)
{
display(COUNT);
Delay_ms(300);
COUNT=COUNT+5;
if(COUNT>2000)
COUNT=0;
}
}