EXPT 05 - Timer0 Polling - ISR - Interrupt Buzzer On - Off
EXPT 05 - Timer0 Polling - ISR - Interrupt Buzzer On - Off
05
Experiment No. 05
PROBLEM STATEMENT:
Write an Embedded C program for Timer programming ISR based buzzer on/off. Use
timer0 interrupt for ON period and OFF period delay.
OBJECTIVE:
a. To understand the basic concepts of Timer and Counter
b. To study in detail Timer0 of PIC Microcontroller
c. To Study interrupt structure of PIC Microcontroller
d. To use timer interrupt and it’s related SFR.
THEORY
Timers as the name suggests pertain to time-related operations. They are mostly used for
exact delay generation. Timers are also used in various other operations like PWM signal
generation, auto-triggering of several other peripherals etc. The working and configuration
of PIC18F4550 Timers have been explained in this article.
Timers are the most essential peripheral for a microcontroller and every controller provides a
provision for using them. Beginners are advised to go through the tutorial on Timers before
going any further.
PIC18F4550 is equipped with four Timers namely, Timer0, Timer1, Timer2 and Timer3. Before
going for the details of Timer configurations, it is important to learn how time delay is
calculated by the timer since exact delay generation is the most common application of Timers.
Timer0:
· Timer0 can work as Timer/Counter in both 8-bit and 16-bit modes
· Dedicated 8-bit, software programmable prescaler
· Selectable clock source (internal or external)
· Interrupt on overflow
1
Experiment No. 05
Timer1:
· Timer1 can work as 16-bit timer or counter
· Readable and writable 8-bit registers (TMR1H and TMR1L)
· Selectable clock source (internal or external)
· Alternate clock source can be provided at Timer1 oscillator pins (T1OSO & T1OSI)
· Interrupt on overflow
· Timer1 can be multiplexed with other peripherals like ADC etc. and generates special event
triggering for CCP (Capture, Compare and PWM) events.
Timer2:
· 8-bit Timer and Period registers (TMR2 and PR2, respectively)
· Software programmable prescaler (1:1, 1:4 and 1:16)
· Software programmable postscaler (1:1 – 1:16)
· Interrupt on TMR2 to PR2 match
· Optional use as the shift clock for the MSSP (Master Synchronous Serial Port) module
Timer3:
· Timer3 can work as 16-bit timer or counter
· Readable and writable 8-bit registers (TMR3H and TMR3L)
· Selectable clock source (internal or external)
· Alternate clock source can be provided at Timer1 oscillator pins (T1OSO & T1OSI)
· Interrupt on overflow
· Timer3 can be multiplexed with other peripherals like ADC etc. and generates special event
triggering for CCP (Capture, Compare and PWM) events.
2
Experiment No. 05
T0PS2: T0PS0: These are prescaler selection bits. The bits setting is shown in the following
table to choose desired prescaler.
Fig. 3: Bit Configuration of T0PS2: T0PS0 prescaler selection bits for configuring Timer in PIC18F4550
PSA: This bit is set to high if there is no need to assign a prescaler value.
1 = Timer0 prescaler is not assigned. Timer0 clock input bypasses prescaler.
0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.
T0SE: This bit is used when external source is selected for the Timer. This bit is used to select
external clock edge to increment the Timer.
1 = Increment on high-to-low transition on T0CKI pin (Pin6)
0 = Increment on low-to-high transition on T0CKI pin (Pin6)
T0CS: This bit is used to select the proper clock source for Timer0.
1 = Transition on T0CKI pin (Pin6)
0 = Internal instruction cycle clock (CLKO)
TMR0ON: This bit is set to high (1) to enable the Timer0 and set to low (0) to stop it.
3
Experiment No. 05
TMR0IF: This is Timer0 overflow flag bit. This bit is set when TMR0 register overflows. This bit
cleared by the software.
Interrupt Vs Polling:
Interrupt and Polling are the two ways to handle the events generated by the I/O
devices that can happen at any moment while CPU is busy in executing another
process.
Polling and Interrupt let CPU stop what it is currently doing and respond to the more
important task.
Whereas, in ISR Method, the I/O device interrupts the CPU and tell CPU that it need
CPU service. When an interrupt signal occurs, which in turns suspends the main
routine, Then the MCU will leave the main program to execute a specific pre-defined
code (ISR Handler).
• Analogy of polling method is like a salesperson. The salesman goes from door to
door while requesting to buy a product or service.
• Analogy of an interrupt is like a shopkeeper. If anyone needs a service or
product, he goes to him and apprises him of his needs.
4
Experiment No. 05
Sample Example:
Fosc(XtalFrquency) = 20Mhz ,
Time delay :50ms
Prescalar factor=1:16
PART A:A) LED Blinking using Polling Method programming using Timer Delay.
Algorithm
1. Configure Port D pins as output and initial value0.
2. Load the value into the T0CON register as per requirement.
3. Load reg. TMR0H followed by reg. TMR0L with initial value
4. Start the timer0. (T0CONbits.TMR0ON =1 )
5. Keep monitoring the timer flag (TMR0IF) to see if it is raised
6. While (INTCONbits.TMROIF==0);
7. As TMR0IF gets high ,Stop the timer0 (T0CONbits.TMR0ON =0 )
8. Clear the TMR0IF .(INTCONbits.TMROIF=0)
9. Go Back to step 2 for repeat.
1. Interfacing Diagram
5
Experiment No. 05
Algorithm
1. Configure Port pin RB3 as output and initial value0.
2. Load the value 0x02 in T0CON indicating which timer in 16-bit mode, clock
source Fosc/4, prescaler assignment with1:8.
3. Load the registers TMR0H first and then TMR0L with calculated initial count
values.
4. Enable the Timer0 Interrupt and Global Interrupt using INTCONRegister
5. Start the timer by setting TMR0ON bit inT0CON.
6. Write the ISR at Interrupt vector 0x0018 which containsfollowing.
a. Toggle the PORT pinRB3
b. In ISR Clear the TMR0IF flag for the nextround.
c. In ISR reload TMR0H and TMR0Lvalues.
6
Experiment No. 05
Conclusion:
7
Experiment No. 05
Source code
PART A: LED blinking (circular Shift LED O/p) using Timer Delay (Polling Method)
programming:
Source Code
B) Buzzer ON and OFF using on-chip timerinterrupt (ISR Method).
//Expt.5b :Configure TIMER0 to get delay of 50ms to make Buzzer ON and OFF
using Timer0 Interrupt Set the timer to 16-bit,Fosc/4,1:16 pre-scaler.( Note
: please set ROM ranges 4 – 3FFF in Project properties compiler , memory
model –ROM ranges)
8
Experiment No. 05
4. References:
a. Mazidi, PIC microcontroller & embedded system 3rd Edition,Pearson
b. Datasheet of PIC18F4550 / PIC18F4520 / PIC18F458,www.microchip.com
c. PIC Development BoardManual.