Книга: Advanced PIC Microcontroller Projects in C
Project Program
Project Program
The program is called SEVEN1.C and the listing is given in Figure 6.27. At the beginning of the program character variables Pattern and Cnt are declared, and Cnt is cleared to 0. Then Table 6.8 is implemented using array SEGMENT. After configuring the PORTC pins as outputs, the program enters an endless loop using a for statement. Inside the loop the bit pattern corresponding to the contents of Cnt is found and stored in variable Pattern. Because we are using a common anode display, a segment is turned ON when it is at logic 0 and thus the bit pattern is inverted before it is sent to PORTC. The value of Cnt is then incremented between 0 and 9, after which the program waits for a second before repeating the above sequence.
/*****************************************************************************
7-SEGMENT DISPLAY
=================
In this project a common anode 7-segment LED display is connected to PORTC
of a PIC18F452 microcontroller and the microcontroller is operated from a 4MHz
resonator. The program displays numbers 0 to 9 on the display with a one
second delay between each output.
Author: Dogan Ibrahim
Date: July 2007
File: SEVEN1.C
******************************************************************************/
void main() {
unsigned char Pattern, Cnt = 0;
unsigned char SEGMENT[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,
0x7D,0x07,0x7F,0x6F};
TRISC = 0; // PORTC are outputs
for(;;) // Endless loop
{
Pattern = SEGMENT[Cnt]; // Number to send to PORTC
Pattern = ~Pattern; // Invert bit pattern
PORTC = Pattern; // Send to PORTC
Cnt++;
if (Cnt == 10) Cnt = 0; // Cnt is between 0 and 9
Delay_ms(1000); // 1 second delay
}
}
Figure 6.27: Program listing