Выбрать главу

Figure 6.31: Block diagram of the project

Figure 6.32: Circuit diagram of the project

Project PDL

At the beginning of the program PORTB and PORTC pins are configured as outputs. The program then enters an endless loop where first of all the Most Significant Digit (MSD) of the number is calculated, function Display is called to find the bit pattern and then sent to the display, and digit 1 is enabled. Then, after a small delay, digit 1 is disabled, the Least Significant Digit (LSD) of the number is calculated, function Display is called to find the bit pattern and then sent to the display, and digit 2 is enabled. Then again after a small delay, digit 2 is disabled, and this process repeats indefinitely. Figure 6.33 shows the PDL of the project. 

START

 Create SEGMENT table

 Configure PORTB as outputs

 Configure PORTC as outputs

 Initialize CNT to 25

 DO FOREVER

  Find MSD digit

  Get bit pattern from SEGMENT

  Enable digit 1

  Wait for a while

  Disable digit 1

  Find LSD digit

  Get bit pattern from SEGMENT

  Enable digit 2

  Wait for a while

  Disable digit 2

 ENDDO

END

Figure 6.33: PDL of the project

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

PROJECT 6.7 — Two-Digit Multiplexed 7-Segment LED Counter with Timer Interrupt 

Project Description

This project is similar to Project 6 but here the microcontroller’s timer interrupt is used to refresh the displays. In Project 6 the microcontroller was busy updating the displays every 10ms and could not perform any other tasks. For example, the program given in Project 6 cannot be used to make a counter with a one-second delay between counts, as the displays cannot be updated while the program waits for one second.

In this project a counter is designed to count from 0 to 99, and the display is refreshed every 5ms inside the timer interrupt service routine. The main program can then perform other tasks, in this example incrementing the count and waiting for one second between counts.

In this project Timer 0 is operated in 8-bit mode. The time for an interrupt is given by:

 Time = (4 × clock period) × Prescaler × (256 – TMR0L)

where Prescaler is the selected prescaler value, and TMR0L is the value loaded into timer register TMR0L to generate timer interrupts every Time period.

In our application the clock frequency is 4MHz, that is, clock period = 0.25μs, and Time = 5ms. Selecting a prescaler value of 32, the number to be loaded into TMR0L can be calculated as follows:

 

or