Êíèãà: Advanced PIC Microcontroller Projects in C

Modifying the Program

Modifying the Program

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.
Author: Dogan Ibrahim
Date:   July 2007
File:   SEVEN5.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
  if (Msd != 0) {
   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 TMR0 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.37: Modified program

Îãëàâëåíèå êíèãè


Ãåíåðàöèÿ: 1.935. Çàïðîñîâ Ê ÁÄ/Cache: 3 / 0
ïîäåëèòüñÿ
Ââåðõ Âíèç