0% found this document useful (0 votes)
27 views9 pages

EXPT 05 - Timer0 Polling - ISR - Interrupt Buzzer On - Off

The document outlines Experiment No. 05, focusing on timer programming using both polling and interrupt methods with a PIC microcontroller. It details the objectives, theory, and algorithms for LED blinking and buzzer control, along with the configuration of Timer0 and its registers. The document also includes source code examples for both methods and references for further reading.

Uploaded by

Sanjay Thorat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views9 pages

EXPT 05 - Timer0 Polling - ISR - Interrupt Buzzer On - Off

The document outlines Experiment No. 05, focusing on timer programming using both polling and interrupt methods with a PIC microcontroller. It details the objectives, theory, and algorithms for LED blinking and buzzer control, along with the configuration of Timer0 and its registers. The document also includes source code examples for both methods and references for further reading.

Uploaded by

Sanjay Thorat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment No.

05

Experiment No. 05

TITLE: Timer programming Polling based& ISR Method.


A) LED blinking(circular Shift) using Polling Method programming using
Timer Delay using Timer 0.
B) Buzzer ON and OFF using on-chip timer interrupt (ISR Method).

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.

S/W PACKAGES AND H/W USED:


MPLABX IDE / MPLAB IDE, X8 / C18 Compiler, PIC Development Board

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

PIC18F4550 Timer0 Registers:


Every Timer has certain registers related to it which must be configured for desired
operations. The registers of Timer0 have been explained below.

1. T0CON (Timer0 Control Register)

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0


TMR0ON T08BIT T0CS T0SE PSA T0PS2 T0PS1 T0PS0
Fig. 2: Bit Configuration of PIC18F4550 Timer0

T0PS2: T0PS0: These are prescaler selection bits. The bits setting is shown in the following
table to choose desired prescaler.

T0PS2:T0PS0 Prescale Value


000 1:256
001 1:128
010 1:64
011 1:32
100 1:16
101 1:8
110 1:4
111 1:2

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)

T08BIT: This bit selects the Timer mode.


1 = Timer0 is configured as an 8-bit timer/counter.
0 = Timer0 is configured as a 16-bit timer/counter.

TMR0ON: This bit is set to high (1) to enable the Timer0 and set to low (0) to stop it.

3
Experiment No. 05

2. INTCON (Interrupt Control Register)

Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0


GIE/GIEH PEIE/GIEL TMR0IE INT0IE RBIE TMR0IF INT0IF RBIF
Fig. 4: Bit Configuration of Interrupt Control Register in PIC18F4550

TMR0IE: This bit is used to enable/disable the Timer0 overflow interrupt.


1 = Enables the Timer0 overflow interrupt
0 = Disables the Timer0 overflow interrupt

TMR0IF: This is Timer0 overflow flag bit. This bit is set when TMR0 register overflows. This bit
cleared by the software.

3. TMR0 (Timer0 Register)


This register is divided into registers TMR0H and TMR0L. Both registers are accessible
separately thus Timer0 can work in both 8-bit and 16-bit modes. In these registers, pre-
calculated value for delay is filled.

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.

In Pollingthe microcontroller continuously monitors the status of all devices in Round-


robin manner.
• When the conditions are for a given device is met, it performs the service.
• After that, it moves on to monitor the next device until everyone is
Serviced.
The main drawback of this method is waste of time of microcontroller

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

Delay Calculation Methods of TMR0: TMR0H and TMR0Lvalues:

Calculation using formula


TMR0 = Resolution - (( T * Fosc ) / (4 * Prescale))

Where Resolution = 65536 ..(for 16 bit timer )


Resolution = 256 ..(for 08 bit timer )

Sample Example:
Fosc(XtalFrquency) = 20Mhz ,
Time delay :50ms
Prescalar factor=1:16

TMR0 = Resolution - (( T * Fosc ) / (4 * Prescale))


= 65536 - (((50x10-3 * 20 x106) / (4 * 16))
= 65536 – 15625
= 49911 in decimal
= 0xC2F7 Hex

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

Figure 1.3: LED Interfacing to PIC Microcontroller( Using Timer elay )

PART B) Buzzer ON and OFF using on-chip timerinterrupt (ISR Method).

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

Figure 1.3: Buzzer Interfacing to PIC


Microcontroller

Conclusion:

7
Experiment No. 05

Source code
PART A: LED blinking (circular Shift LED O/p) using Timer Delay (Polling Method)
programming:

#pragma config OSC = HS //Oscillator Selection


#pragma config WDT = OFF //Disable Watchdog timer
#include <xc.h>
//Function Prototypes
void main()
{
TRISD=0; // ConfigurePortD as output Port.
LATD=0x01; // Turn on LED on PORT D pin0 As single bit use LAT
T0CON=0x07; // Prescaler= 1:256, 16-bit mode, Internal Clock
while(1) { // Values calculated for 1 second delay with 20MHz crystal
TMR0H=0xB3; // Placing higher byte in TMR0H
TMR0L=0xB4; // Placing Lower byte in TMR0L
T0CONbits.TMR0ON=1;// Timer0 On
while (INTCONbits.TMR0IF==0); // Wait until TMR0IF gets flagged
T0CONbits.TMR0ON=0; // Timer0 Off
INTCONbits.TMR0IF=0; // Clear Timer0 interrupt flag
LATD= (LATD<<1)| (LATD>>7) // Circular right right shift(<<)OR Left shift(>>)
}
}

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)

#include<xc.h> //Include Controller specific.h

// Timer0 ISR Definition


void interrupt ISR(void)
{
TMR0H=0X85; //Reload the timer values afteroverflow TMR0L
=0XEE;
PORTBbits.RB3 = ~PORTBbits.RB3; //Toggle the RB3(~ bitwise NOT)
INTCONbits.TMR0IF = 0; //Reset the timer overflow interrupt flag
}
// Start of main Program void
main()
{
ADCON1=0x0F; //Configuring the PORTE pins as digital I/O
TRISBbits.RB3 = 0; //Configuring the RB3 as output
PORTBbits.RB3=0; //Setting the initialvalue
T0CON = 0x02; //Set the timer to 16-bit,Fosc/4,1:16 pre-scaler
TMR0H = 0x85; //load timer value to generate delay 50 ms
TMR0L =0xEE;
INTCONbits.TMR0IF =0; // Clear Timer0 overflowflag
INTCONbits.TMR0IE =1; // TMR0 interrupt enabled
T0CONbits.TMR0ON = 1; // Start timer0
INTCONbits.GIE=1; // Global interruptenabled

while(1); //Program execution stays here untilthe

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy