Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#define DIGIT1 PORTB.F0
#define DIGIT2 PORTB.F1
unsigned char Cnt = 0;
unsigned char Flag = 0;
// This function finds the bit pattern to be sent to the port to display a number
// on the 7-segment LED. The number is passed in the argument list of the function.
//
unsigned char Display(unsigned char no)
{
unsigned char Pattern;
unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
Pattern = SEGMENT[no]; // Pattern to return
return (Pattern);
}
//
// TMR0 timer interrupt service routine. The program jumps to the ISR at
// every 5ms.
//
void interrupt (){
unsigned char Msd, Lsd;
TMR0 = 100; // Re-load TMR0
T0IF_bit = 0;
Flag = ~ Flag; // Toggle Flag
if(Flag == 0){ // Do digit 1
DIGIT2 = 0;
Msd = Cnt / 10; // MSD digit
PORTC = Display(Msd); // Send to PORTC
DIGIT1 = 1; // Enable digit 1
}
else{ // Do digit 2
DIGIT1 = 0; // Disable digit 1
Lsd = Cnt % 10; // LSD digit
PORTC = Display(Lsd); // Send to PORTC
DIGIT2 = 1; // Enable digit 2
}
}
//
// Start of MAIN Program. configure PORTB and PORTC as outputs.
// In addition, configure TMR0 to interrupt at every 10ms
//
void main(){
PORTC = 0; //Clear initial state
PORTB = 0; //Clear initial state
TRISC = 0; // PORTC are outputs
TRISB = 0; // RB0, RB1 are outputs
DIGIT1 = 0; // Disable digit 1
DIGIT2 = 0; // Disable digit 2
//
// Configure TMR0 timer interrupt
//
OPTION_REG = 0x84; //Prescaler 32
//T0CON = 0xC4; // Prescaler = 32
TMR0 = 100; // Load TMR0L with 100
INTCON = 0xA0; // Enable TMR0 interrupt
Delay_ms(1000);
for(;;){ // Endless loop
Cnt++; // Increment Cnt
if(Cnt == 100) Cnt = 0; // Count between 0 and 99
Delay_ms(1000); // Wait 1 second
}
}