Книга: Advanced PIC Microcontroller Projects in C

Project Program

Project Program

The program is named SEVEN3.C, and the listing is shown in Figure 6.34. DIGIT1 and DIGIT2 are defined as equal to bit 0 and bit 1 of PORTB respectively. The value to be displayed (the number 25) is stored in variable Cnt. An endless loop is formed using a for statement. Inside the loop, the MSD of the number is calculated by dividing the number by 10. Function Display is then called to find the bit pattern to send to PORTC. Then digit 1 is enabled by setting DIGIT1 = 1 and the program waits for 10ms. After this, digit 1 is disabled and the LSD of the number is calculated using the mod operator (“%”) and sent to PORTC. At the same time, digit 2 is enabled by setting DIGIT2 = 1 and the program waits for 10ms. After this time digit 2 is disabled, and the program repeats forever.

/*********************************************************************
                        Dual 7-SEGMENT DISPLAY
                        ======================
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 displays number 25 on the displays.
Author: Dogan Ibrahim
Date:   July 2007
File:   SEVEN3.C
***********************************************************************/
#define DIGIT1 PORTB.F0
#define DIGIT2 PORTB.F1
//
// 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);
}
//
// Start of MAIN Program
//
void main() {
 unsigned char Msd, Lsd, Cnt = 25;
 TRISC = 0;             // PORTC are outputs
 TRISB = 0;             // RB0, RB1 are outputs
 DIGIT1 = 0;            // Disable digit 1
 DIGIT2 = 0;            // Disable digit 2
 for(;;)                // Endless loop
 {
  Msd = Cnt / 10;       // MSD digit
  PORTC = Display(Msd); // Send to PORTC
  DIGIT1 = 1;           // Enable digit 1
  Delay_Ms(10);         // Wait a while
  DIGIT1 = 0;           // Disable digit 1
  Lsd = Cnt % 10;       // LSD digit
  PORTC = Display(Lsd); // Send to PORTC
  DIGIT2 = 1;           // Enable digit 2
  Delay_Ms(10);         // Wait a while
  DIGIT2 = 0;           // Disable digit 2
 }
}


Figure 6.34: Program listing

Оглавление книги

Оглавление статьи/книги

Генерация: 0.030. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз