Timer Interrupt Programming and Multiplexing Objectives 1 St
Timer Interrupt Programming and Multiplexing
Objectives
1. Students will program an interrupt driven multiplexed 4 digit 7 segment display
driver in C.
Program Description
1. Write a program that does the following:
a. Drive the 4 multiplexed 7 segment displays on the development board as a
decimal counter that counts up by one every 500 milliseconds from 0 to
9999.
b. The multiplexing code may not be placed in the main loop of your
program. The multiplexing process must be controlled by timer interrupt
service routines.
c. The main loop of your program must only do the following:
i. Decode the counter integer value into BCD digits to be displayed
on the 7 segment displays.
ii. Increment or reset the counter.
iii. Delay for 500 milliseconds.
2. An example of converting a 16-bit integer into a 5 unpacked BCD bytes in an
array is provided on the next page.
Solution
#include<avr/io.h>
 /*Includes io.h header file where all the Input/Output Registers and its Bits are defined for all AVR microcontrollers*/
 
 #define          F_CPU          1000000
 /*Defines a macro for the delay.h header file. F_CPU is the microcontroller frequency value for the delay.h header file. Default value of F_CPU in delay.h header file is 1000000(1MHz)*/
 
 #include<util/delay.h>
 /*Includes delay.h header file which defines two functions, _delay_ms (millisecond delay) and _delay_us (microsecond delay)*/
 
 #define          _7SEGMENT_PORT          PORTB
 /*Defines a macro for the 7segment.h header File. _7SEGMENT_PORT is the microcontroller PORT Register to which the data pins of the 7-segment Display are connected. Default PORT Resister in 7segment.h is PORTB*/
 
 #define          _7SEGMENT_TYPE          COMMON_ANODE
 /*Defines a macro for the 7segment.h header File. _7SEGMENT_TYPE is the type of 7-segment Display (Common Cathode or Common Anode) we are interfacing. Default type of 7-segment display in 7segment.h is Common Anode*/
 
 #include<avr/7segment.h>
 /*Includes 7segment.h header file which defines different functions for 7-segment display. 7segment header file version is 1.1*/
 
 #include<avr/timercounter0.h>
 /*Includes timercounter0.h header file which defines different functions for the timer counter 0. TIMER COUNTER 0 header file version is 1.1*/
 
 #include<avr/interrupt.h>
 /*Includes interrupt.h header file which defines different functions for interrupts*/
 
 volatile unsigned char counter1=0,counter2=0,counter3=0,counter4=0;
 /*Global variable declaration & initialisation*/
 
 /*Interrupt Service Routine for timer counter 0 compare match*/
 ISR(TIMER0_COMP_vect)
 {
counter1++;
 /*Incrementing the 1st place digit*/
counter1=0;
 /*Reseting 1st place digit to 0*/
 
 /*Checking whether the 10th place digit is below the upper limit(9) or not*/
 if(counter2<9)
 {
}
 else
 {
counter3++;
 /*Incrementing the 100th place digit*/
counter3=0;
 /*Reseting 100th place digit to 0*/
 
 /*Checking whether the 1000th place digit is below the upper limit(9) or not*/
 if(counter4<9)
 {
}
 else
 {
}
}
DDRB=0xff;
 /*All the 8 pins of PortB are declared output (all pins of 7 segment display are connected)*/
 
 DDRD=0x0f;
 /*PD0, PD1, PD2 and PD3 pins of PortD are declared output (display select pins 1, 2, 3 and 4 of Quad 7-segment Display are connected)*/
 
 set_timercounter0_mode(2);
 /*Timer counter 0 is set to ctc mode*/
 
 set_timercounter0_output_mode(0);
 /*Timer counter 0 output mode is set for normal port operation*/
 
 set_timercounter0_prescaler(5);
 /*Timer counter 0 frequency is set to 976.5625Hz*/
 
 enable_timercounter0_interrupt(1);
 /*Timer counter 0 compare match interrupt is enabled*/
 
 sei();
 /*Global interrupt is enabled*/
 
 set_timercounter0_compare_value(255);
 /*OCR register value is set to 255*/
 
 /*Start of infinite loop*/
 while(1)
 {
}
}
 /*End of Program*/



