PROJECT 11 – Interrupt Driven Event Counter with 4-digit LED Display
Function
This project shows how the external interrupt input of the microcontroller can be programmed using the C language. The project is a simple interrupt-based event counter which can count external events from 0 to 9999. A TSM5034 type 4-digit display is connected to port 1 of the microcontroller. Bit 0 of port 3 is connected to a push-button switch S1 which is used to clear the display whenever required. External interrupt input INT0 (pin 6) of the
microcontroller is used as an edge-triggered event input. An external event occurs when INT0 is clocked from 1 to 0.
Circuit Diagram
Figure 3.28 shows a block diagram of the hardware. The push-button switch is the reset input. External events are falling edge triggered. A TSM5034 display shows the event count at any time.
The complete circuit diagram of this project is shown in Fig. 3.29. TSM5034 is connected to port 1 of the microcontroller. The clock input is connected to bit 6 of port 1 and the data input is connected to bit 7 of port 1. Bit 0 of port 3 is connected to the event reset switch S1. This input is normally held at logic 1 with the pull-up resistor R3. When the switch is pressed the pin goes to logic 0 which can be detected by the software. External interrupt input INT0 is used as the event counter input. This pin is normally held at logic 1 with the pull-up resistor R4. An external event occurs when this pin is clocked to 0. This
generates an interrupt in the software which increments the count and displays the total number of events occurred.
Program Description
The program first initializes the interrupt registers of the microcontroller so that external interrupts on pin INT0 can be detected. An endless loop is then
formed with a for statement. Inside this loop the reset input (bit 0 of port 3) is checked and when the reset switch is pressed, the counter is cleared to zero. The interrupt service routine simply increments the current event count and displays the result.
The following PDL describes the functions of the program:
The display part of the program is the same as in Project 10 and is not described here again.
Program Listing
The full program listing is shown in Fig. 3.30. Variable DISPLAY_CLOCK is the clock input of the display and is assigned to bit 6 of port 1. DISPLAY_DATA is the data input of the display and is assigned to bit 7 of port 1. Bit 0 of port 3 is assigned to variable RESET_COUNTER. Variable EVENT is used as the event counter. The program first of all clears the display. The interrupt registers of the microcontroller are then programmed. Statement ITO = I sets external interrupt input INT0 sothat interrupts can be recognized on the falling edge (1 to 0) of this pin. Statement EXO = I enables external interrupt INT0. Statement EA = I enables interrupts sothat they can be accepted by the microcontroller. Inside the endless loop the RESET input is checked. If the user presses RESET (i.e. RESET_COUNTER = O), the counter value EVENT is reset tozero and the display is cleared.
The interrupt service routine is declared by the function cnt() interrupt O, where 0 is the interrupt number. 89C2051 interrupt numbers are defined as shown in Table 3.7.
Whenever input INT0 goes from logic 1 to 0 an external interrupt is generated and the program jumps to interrupt service routine declared by function cnt() interrupt O. This routine increments the event counter and displays the result on the TSM5034. The displayed value is thus equal to the total number of events on pin INT0.
Components Required
In addition to the standard components used by the microcontroller, the following components will be required: