Simple PIC18 Projects:Two-Digit Multiplexed 7-Segment LED Counter with Timer Interrupt

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 x clock periodÞ x Prescaler x ð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.25ms, and Time ¼ 5ms. Selecting a prescaler value of 32, the number to be loaded into TMR0L can be calculated as follows:

Simple PIC18 Projects-0047

Taking the don’t-care entries (X) as 0, the hexadecimal value to be loaded into register INTCON is 0xA0.

When an interrupt occurs, the program automatically jumps to the interrupt service routine. Inside this routine we have to reload register TMR0L, reenable the TMR0 interrupts, and clear the TMR0 interrupt flag bit. Setting INTCON register to 0x20 reenables the TMR0 interrupts and at the same time clears the TMR0 interrupt flag.

The operations to be performed can thus be summarized as follows: 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)

Project Hardware

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.

Project PDL

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.

Simple PIC18 Projects-0048

Project Program

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.

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.

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

Simple PIC18 Projects-0049

Simple PIC18 Projects-0050

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.

Simple PIC18 Projects-0051

Simple PIC18 Projects-0052

Leave a comment

Your email address will not be published. Required fields are marked *