450 likes | 471 Views
Learn about the timers of PIC16 and their associated registers, as well as the various modes of the timers. Program the PIC16 timers in Hitech-C to generate time delays and use them as event counters.
E N D
EET203Microcontroller Systems DesignPIC16 Timer Programming in Hitech-C
Today’s Lecture • List the Timers of PIC16 and their associated registers • Describe the various modes of the PIC16 timers • Program the PIC16 timers in ‘Hitech C’ to generate time delays • Program the PIC16 timers in ‘Hitech C’ • As event counters
Introduction • PIC16 has one(1) to three(3) timers • Depending on the family number/models. • Ex: PIC16F877A have three(3) timers; Timer0, Timer1 and Timer2. • These timers can be used as • Timers to generate a time delay. • Counters to count events happening outside the C.
Programming timers 0 and 1 • A register whose value is continually increasing to 255, and then it starts all over again: 0, 1, 2, 3, 4...255....0,1, 2, 3......etc. • Every timer needs a clock pulse to tick. • Clock source can be • Internal 1/4th of the frequency of the crystal oscillator on OSC1 and OSC2 pins (Fosc/4) is fed into timer • External: pulses are fed through one of the PIC18’s pins Counter
Timer0 registers and programming • All instructions are written to the TMR0 register. • Consist of a register called ‘OPTION_REG’ Register, which is 8 bits of size. • Readable and writable register which contains various control bits to configure the TMR0.
T0CS(Timer0 clock source) • This bit used to decide whether the clock source is internal or external. • If T0CS=0Then Fosc/4 is used as clock source, used for delay generation. • If T0CS=1the clock source is external and comes from an external source, used as event counter.
Timer clock source External Source Internal Source 8
Timer0 registers and programming • Example • If OPTION_REG=0b00110000; • External clock • High-to-low transition • Timer0 module • Prescale 1:2
Timer0 registers and programming • Typical calculations for creating an 18ms interrupt repeat rate using PIC Timer0 (Prescaler ratio of 1:128 and 4Mhz internal clock input). • Fosc/4 or 4MHz/4 =1MHz • Period input to Timer0 which is:1/(1MHz/128) = 128s • 128s is counted by Timer 0 and it will overflow after 141 counts (or 18ms) • 18ms /128s 141counts • Overflowtime=4 x Toscx Prescaler x (256-TMR0)
TMR0IF flag bit • TMR0IF is (Timer0 interrupt flag) is a part of the INTCON (interrupt control) register. • When the timer reaches its maximum value of FFH, it rolls over to 00, and TMR0IF is set to 1. INTCON (Interrupt Control Register) has the TMR0IF Flag
Programming in 8-bit mode • Load the value into the OPTION_REG register. • Load reg. TMR0 with initial value. • Start the timer with instruction OPTION_REG=0b00000011; //Timer0 on • Keep monitoring the timer flag (TMR0IF) to see if it is raised. • Stop the timer. • Clear the TMR0IF flag. INTCON=0b00000000; //Clear interrupt flag • Go Back to step 2.
Example1 Write a program to generate a square wave of 50% duty cycle on pin PORTB.5. Use Timer0 with prescaler 16 (use 10MHz clock). With overflow is 1.152 x10-4 s.
Example1: Program #include <htc.h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 10000000 #define LED5 RB5 void pic_init(void); void timer_init(void); main() { pic_init(); //initialize PIC timer_init(); //initialize Timer Module while(1) { OPTION_REG=0b00000011; //start Timer1 if(INTCON==0b00000100) { OPTION_REG=0b00100011; //off Timer1 LED5 = !LED5; INTCON=0b00000000; TMR0=0xEE; }
Example1: Program } } void pic_init(void) { TRISB=0b00000000; PORTB=0b00000000; } void timer_init(void) { OPTION_REG=0b00100011; TMR0=0xEE; }
1 2 17 18 EE FE 0F EF FF 00 TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=1 Example 1: Analysis • Timer1 counts up from EE, FE, ….,FFH. • From FFH to 00H, TMR0IF is set to 1. • T = 4/10MHz = 0.4s (Each tick consume 0.4s). • How many tick? (FF-EE) + 1 = 18 Decimal ticks. • Time delay = 18 x 0.4s = 7.2s for half the pulse
T1CON (Timer1 control) register • Each timer has a control register called TxCON, to set various timer operation modes. • T1CON is 8-bit register used to control Timer1. • Example • If T1CON= 0b00000010; • 16-bit • No prescaler • Rising edge
Characteristics and operations of 16-bit mode • 16-bit timer, 0000 to FFFFH. • After loading TMR1H and TMR1L, the timer must be started. • Count up, till it reaches FFFFH, then it rolls over to 0000 and activate TMR1IF bit. • Then TMR1H and TMR1L must be reloaded with the original value and deactivate TMR1IF bit.
Programming in 16-bit mode • Load the value into the T1CON register. • Load reg. TMR1H followed by reg. TMR1L with initial value. • Start the timer with instruction T1CON=0b00000001; //Timer1 on • Keep monitoring the timer flag (TMR1IF) to see if it is raised. • Stop the timer. • Clear the TMR1IF flag. PIR1=0b00000000; //Clear interrupt flag • Go Back to step 2.
Example 2: Program • Write a program to generate a square wave of 50% duty cycle on pin PORTB.5. Use Timer1 without prescaler (use 10MHz clock). The overflow is 5.6 us. #include <htc.h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 10000000 #define LED5 RB5 void pic_init(void); void timer_init(void); main() { pic_init(); //initialize PIC timer_init(); //initialize Timer Module while(1) { T1CON=0b00000001; //start Timer1
Example 2: Program if(PIR1==0b00000001) { T1CON=0b00000000; //off Timer1 LED5 = !LED5; //Toggle portb bit 5 PIR1=0b00000000; //Clear interrupt flag TMR1H=0xFF; //Re-load TMR1H and TMR1L TMR1L=0xF2; } } } void pic_init(void) { TRISB=0b00000000; PORTB=0b00000000; } void timer_init(void) { T1CON=0b00000000; TMR1H=0xFF; TMR1L=0xF2; }
1 2 13 14 FFF2 FFF3 FFF4 FFFE FFFF 0000 TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=0 TMR0IF=1 Example 2: Analysis • Timer1 counts up from FFF2, FFF3, FFF4,….,FFFFH. • From FFFFH to 0000H, TMR1IF is set to 1. • T = 4/10MHz = 0.4s (Each tick consume 0.4s). • How many tick? (FFFF-FFF2) + 1 = 14 Decimal ticks. • Time delay = 14 x 0.4s = 5.6s for half the pulse
Delay Calculation • In Hex • (FFFF-FFF2+1)x0.4s • FFF2 are TMR1H and TMR1L initial values(in Hex). b) In Decimal (65536-65523+1)x0.4s 65523 are TMR1H and TMR1L initial values(in Decimal). • General formula for delay calculation • T = 4/(10MHz) = 0.4second
16-bit register timer delay • If we know the amount of timer delay, we find the initial values needed for the TMR1H and TMR1L registers. • From example 2(no prescaler): • T = 4/(10MHz) = 0.4second. • (FFFF-N+1)x0.4s=5.6s. • Whereby N is equal to FFF2H or 65523d.
Example 3 • Write a program to generate a square wave with a period of 10ms on pin PORTB.3. Without prescaler and Fosc= 10 MHz. • Solution: • (FFFF-N+1)x(4/10Mhz)=5ms(5ms low and 5ms high). • Therefore N should be CF2CH or 53036d; need to use 16-bit timer module since N is greater than 8-bit. • If Timer0 chosen, N should be divided by 256(prescaler).
Example 3: Program #include <htc.h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 10000000 #define LED3 RB3 void pic_init(void); void timer_init(void); main() { pic_init(); //initialize PIC timer_init(); //initialize Timer Module while(1) { T1CON=0b00000001; //start Timer1 if(PIR1==0b00000001) { T1CON=0b00000000; //off Timer1 LED3 = !LED3; PIR1=0b00000000; TMR1H=0xCF; TMR1L=0x2C; }
Example 3: Program } } void pic_init(void) { TRISB=0b00000000; PORTB=0b00000000; } void timer_init(void) { T1CON=0b00000000; TMR1H=0xCF; TMR1L=0x2C; }
Prescaler and generating larger delay XTAL Osc ÷ 4 ÷ 64 TMRx 9-36 • The size of delay depend on • The Crystal frequency • The timer’s bit register. • The largest timer happens when TMR1L=TMR1H=0 • Prescaler option is used to duplicate the delay by dividing the clock by a factor of 2,4,8,16,32,64 ,128,256. • If OPTION_REG =0000 0101, then T = 4*64/f.
Counter Programming • Used to counts event outside the PIC • Increments the TMR0 registers • T0CS in T0CON reg. determines the clock source, • If T0CS = 1, the timer is used as a counter • Counts up as pulses are fed from pin RA4 (T0CKI) • What does T0CON=0110 1000 mean? • If TMR1CS=1, the timer 1 counts up as clock pulses are fed into pin RC0
Using external Crystal for Timer1 clock • Timer1 comes with two options, • clock fed into T1CKI • T1OSCEN=0 • Clock from a crystal connected to T1OSI-T1OSO (additional) • T1OSCEN=1 • 32 kHz Crystal is connected • Used for saving power during SLEEP mode doesn’t disable Timer1 while the main crystal is shut down
Timer clock source External Source Internal Source 39
Example 4 • Write a counter program for 8-bit mode to count the pulses and display the state of 8-bit timer on PORTB.
Example 4: Program #include <htc.h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 10000000 void pic_init(void); void timer_init(void); main() { pic_init(); //initialize PIC timer_init(); //initialize Timer Module while(1) { OPTION_REG=0b00100000; //waiting for external input PORTB=TMR0; //TMR0 output through port B if(INTCON==0b00000100) { INTCON=0b00000000; TMR0=0; } } }
Example 4: Program void pic_init(void) { TRISA=0b11111111; //whole PortA as input PORTA=0b00000000; //Clear port A TRISB=0b00000000; //whole PortB as output PORTB=0b00000000; //Clear port B } void timer_init(void) { OPTION_REG=0b00100000; //Timer0 external input, prescale 1:2 TMR0=0; }
References • Jie Hu , ECE692 Embedded Computing Systems , Fall 2010. • PIC Microcontroller And Embedded Systems: using Assembly and C for PIC 18, M. Mazidi, R. McKinlay and D. Causey, Prentice Fall, 2008. • Eng. Husam Alzaq, Embedded System Course, IUG, 2010 45