In the main program:
• Load TMR0L with 100
• Set T0CON to 0xC4
• Set INTCON to 0xA0
• Increment the counter with 1-second delays
In the interrupt service routine:
• Re-load TMR0L to 100
• Refresh displays
• Set INTCON to 0x20 (reenable TMR0 interrupts and clear timer interrupt flag)
The circuit diagram of this project is same as in Figure 6.32 where a dual 7-segment display is connected to PORTB and PORTC of a PIC18F452 microcontroller.
The PDL of the project is shown in Figure 6.35. The program is in two sections: the main program and the interrupt service routine. Inside the main program, TMR0 is configured to generate interrupts every 5ms and the counter is incremented with a one-second delay. Inside the interrupt service routine, the timer interrupt is reenabled and the display digits are refreshed alternately every 5ms.
MAIN PROGRAM:
START
Configure PORTB as outputs
Configure PORTC as outputs
Clear variable Cnt to 0
Configure TMR0 to generate interrupts every 5ms
DO FOREVER
Increment Cnt between 0 and 99
Delay 1 second
ENDDO
END
INTERRUPT SERVICE ROUTINE:
START
Re-configure TMR0
IF Digit 1 updated THEN
Update digit 2
ELSE
Update digit 1
END
END
Figure 6.35: PDL of the project
The program is called SEVEN4.C, and the program listing is given in Figure 6.36. At the beginning of the main program PORTB and PORTC are configured as outputs. Then register T0CON is loaded with 0xC4 to enable the TMR0 and set the prescaler to 32. TMR0L register is loaded with 100 so that an interrupt is generated after 5ms. The program then enters an endless loop where the value of Cnt is incremented every second.
/*********************************************************************
Dual 7-SEGMENT DISPLAY COUNTER
==============================
In this project two common cathode 7-segment LED displays are connected to
PORTC of a PIC18F452 microcontroller and the microcontroller is operated
from a 4MHz resonator. Digit 1 (left digit) enable pin is connected to port
pin RB0
and digit 2 (right digit) enable pin is connected to port pin RB1
of the microcontroller.
The program counts up from 0 to 99 with one second delay between each count.
The display is updated in a timer interrupt service routine at every 5ms.
Author: Dogan Ibrahim
Date: July 2007
File: SEVEN4.C
**********************************************************************/
#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;
TMR0L = 100; // Re-load TMR0
INTCON = 0x20; // Set T0IE and clear T0IF
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() {
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
//
T0CON = 0xC4; // Prescaler = 32
TMR0L = 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
}
}
Figure 6.36: Program of the project
Inside the interrupt service routine, register TMR0L is reloaded, TMR0 interrupts are reenabled, and the timer interrupt flag is cleared so that further timer interrupts can be generated. The display digits are then updated alternately. A variable called Flag is used to determine which digit to update. Function Display is called, as in Project 6, to find the bit pattern to be sent to PORTC.
In Figure 6.36 the display counts as 00 01…09 10 11…99 00 01… (i.e., the first digit is shown as 0 for numbers less than 10). The program could be modified so the first digit is blanked if the number to be displayed is less than 10. The modified program (called SEVEN5.C) is shown in Figure 6.37. Here, the first digit (MSD) is not enabled if the number to be displayed is 0.
/************************************************
Dual 7-SEGMENT DISPLAY COUNTER
==============================
In this project two common cathode 7-segment LED displays are
connected to PORTC of a PIC18F452 microcontroller and the
microcontroller is operated from a 4MHz resonator. Digit 1 (left
digit) enable pin is connected to port pin RB0 and digit 2
(right digit) enable pin is connected to port pin RB1 of the
microcontroller. The program counts up from 0 to 99 with one
second delay between each count.
The display is updated in a timer interrupt service routine at
every 5ms.
In this version of the program the first digit is blanked if the
number is 0.
Читать дальше