Introduction
As mentioned earlier, Timer0 works in either timer and counter mode. Timer0 Clock Select bit toggle between these two modes. Implementation of timer of a microcontroller has already described in previous post.
In this programming example of using Timer0 module, timer creates a time tick that's a square wave output on a microcontroller digital output pin.
Configuration
Timer0 module of PIC16F887 is configured to operate in timer mode. Prescaler is assigned to WDT to get 1:1 ratio.
Microcontroller clock is its 8MHz internal oscillator. Microcontroller executing speed is,
Finstruction = Fosc/4 = (8MHz)/4 = 2MHz.
Microcontroller instruction executing cycle is,
Tinstruction = 1/2MHz = 500ns.
Input clock pulse is directly drive to Timer0 module. Timer0 overflow time is,
Timer0overflow = 256 * 500ns = 128us.
Each time Timer0 overflow RD0 of Port D toggles, creating an output square wave. The period of square wave is,
Tinterrupt = 2 * 128us = 256us.
Its frequency is,
1 / (256us) = 3906Hz.
Programming for Timer0
This programming example is simulated using Proteus.
Schematic Diagram |
#define OUTPUT PORTD.RD0 void main() { PORTD=0x00; // CLEAR PORTD TRISD=0x00; // SET PORTD AS OUTPUT OSCCON|=0x70; // SELECT INTERNAL OSCILLATOR 8MHz OPTION_REG.T0CS=0; // SELCT INTERNAL SOURCE OPTION_REG.PSA=1; // Prescaler Assigned to WDT OPTION_REG&=0xF8 ; // SELECT 1:1 Prescaler TMR0=0; // CLEAR TIMER0 while(1) if(INTCON.T0IF){ INTCON.T0IF=0; PORTD^=1; } }
Click here to download this programming example.
No comments:
Post a Comment