EET 203 Microcontroller Systems Design PIC 16 Timer

  • Slides: 45
Download presentation
EET 203 Microcontroller Systems Design PIC 16 Timer Programming in Hitech-C

EET 203 Microcontroller Systems Design PIC 16 Timer Programming in Hitech-C

Today’s Lecture o List the Timers of PIC 16 and their associated registers o

Today’s Lecture o List the Timers of PIC 16 and their associated registers o Describe the various modes of the PIC 16 timers o Program the PIC 16 timers in ‘Hitech C’ to generate time delays o Program the PIC 16 timers in ‘Hitech C’ as event counters 2

Introduction o PIC 16 has one(1) to three(3) timers n Depending on the family

Introduction o PIC 16 has one(1) to three(3) timers n Depending on the family number/models. n Ex: PIC 16 F 877 A have three(3) timers; Timer 0, Timer 1 and Timer 2. o These timers can be used as n Timers to generate a time delay. n Counters to count events happening outside the C. 3

Programming timers 0 and 1 o A register whose value is continually increasing to

Programming timers 0 and 1 o 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. o Every timer needs a clock pulse to tick. o Clock source can be n Internal 1/4 th of the frequency of the crystal oscillator on OSC 1 and OSC 2 pins (Fosc/4) is fed into timer n External: pulses are fed through one of the PIC 16’s pins Counter 4

Timer 0 registers and programming o All instructions are written to the TMR 0

Timer 0 registers and programming o All instructions are written to the TMR 0 register. o Consist of a register called ‘OPTION_REG’ Register, which is 8 bits of size. n Readable and writable register which contains various control bits to configure the TMR 0. 5

Timer 0 registers and programming 6

Timer 0 registers and programming 6

T 0 CS(Timer 0 clock source) o This bit used to decide whether the

T 0 CS(Timer 0 clock source) o This bit used to decide whether the clock source is internal or external. o If T 0 CS=0 Then Fosc/4 is used as clock source, used for delay generation. o If T 0 CS=1 the clock source is external and comes from an external source, used as event counter. 7

Timer clock source External Source Internal Source 8

Timer clock source External Source Internal Source 8

Timer 0 registers and programming o Example 1 n n n If OPTION_REG=0 b

Timer 0 registers and programming o Example 1 n n n If OPTION_REG=0 b 00110000; External clock High-to-low transition Timer 0 module Prescale 1: 2 9

Timer 0 registers and programming o Typical calculations for creating an 18 ms interrupt

Timer 0 registers and programming o Typical calculations for creating an 18 ms interrupt repeat rate using PIC Timer 0 (Prescaler ratio of 1: 128 and 4 Mhz internal clock input). n Fosc/4 or 4 MHz/4 =1 MHz (divide by 4) n Period input to Timer 0 which is: 1/(1 MHz/128) = 128 s (1/ (answer above x 1/pre-scale value) n 128 s is counted by Timer 0 and it will overflow after 141 counts (or 18 ms) n 18 ms /128 s 141 counts o Overflowtime=4 x Tosc x Prescaler x (256 -TMR 0) 10

TMR 0 IF flag bit o TMR 0 IF is (Timer 0 interrupt flag)

TMR 0 IF flag bit o TMR 0 IF is (Timer 0 interrupt flag) is a part of the INTCON (interrupt control) register. o When the timer reaches its maximum value of FFH, it rolls over to 00, and TMR 0 IF is set to 1. INTCON (Interrupt Control Register) has the TMR 0 IF Flag 11

Programming in 8 -bit mode 1. Load the value into the OPTION_REG register. 2.

Programming in 8 -bit mode 1. Load the value into the OPTION_REG register. 2. Load reg. TMR 0 with initial value. 3. Start the timer with instruction OPTION_REG=0 b 00000011; //Timer 0 on 4. Keep monitoring the timer flag (TMR 0 IF) to see if it is raised. 5. Stop the timer. 6. Clear the TMR 0 IF flag. INTCON=0 b 0000; //Clear interrupt flag 7. Go Back to step 2. 12

Example 1 o Write a program to generate a square wave of 50% duty

Example 1 o Write a program to generate a square wave of 50% duty cycle on pin PORTB. 5. Use Timer 0 with prescaler 16 (use 10 MHz clock). 13

Example 1: Program #include <htc. h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF

Example 1: Program #include <htc. h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 10000000 #define LED 5 RB 5 void pic_init(void); void timer_init(void); main() { pic_init(); //initialize PIC timer_init(); //initialize Timer Module while(1) { OPTION_REG=0 b 00000011; //start Timer 1 if(INTCON==0 b 00000100) { OPTION_REG=0 b 00100011; LED 5 = !LED 5; INTCON=0 b 0000; TMR 0=0 x. EE; } //off Timer 1 14

Example 1: Program } } void pic_init(void) { TRISB=0 b 0000; PORTB=0 b 0000;

Example 1: Program } } void pic_init(void) { TRISB=0 b 0000; PORTB=0 b 0000; } void timer_init(void) { OPTION_REG=0 b 00100011; TMR 0=0 x. EE; } 15

Example 1: Circuit Layout 16

Example 1: Circuit Layout 16

Example 1: Simulation Result 17

Example 1: Simulation Result 17

Example 1: Analysis-Time Delay o o o Timer 0 counts up from EE, FE,

Example 1: Analysis-Time Delay o o o Timer 0 counts up from EE, FE, …. , FFH. From FFH to 00 H, TMR 0 IF is set to 1. T = 4/10 MHz = 0. 4 s (Each tick consume 0. 4 s). How many tick? (FF-EE) + 1 = 18 Decimal ticks. (255 -238)+1 =18 Time delay = 18 x 0. 4 s = 7. 2 s for half the pulse 1 2 17 18 EE FE 0 F EF FF 00 TMR 0 IF=0 TMR 0 IF=1 18

T 1 CON (Timer 1 control) register o Each timer has a control register

T 1 CON (Timer 1 control) register o Each timer has a control register called Tx. CON, to set various timer operation modes. o T 1 CON is 8 -bit register used to control Timer 1. o Example n If T 1 CON= 0 b 00000010; n 8 -bit n No prescaler n Rising edge 19

T 1 CON (Timer 1 control) register 20

T 1 CON (Timer 1 control) register 20

Characteristics and operations of 16 -bit mode q 16 -bit timer, 0000 to FFFFH.

Characteristics and operations of 16 -bit mode q 16 -bit timer, 0000 to FFFFH. q After loading TMR 1 H and TMR 1 L, the timer must be started. q Count up, till it reaches FFFFH, then it rolls over to 0000 and activate TMR 1 IF bit. q Then TMR 1 H and TMR 1 L must be reloaded with the original value and deactivate TMR 1 IF bit. 21

Characteristics and operations of 16 -bit mode 22

Characteristics and operations of 16 -bit mode 22

Programming in 16 -bit mode 1. Load the value into the T 1 CON

Programming in 16 -bit mode 1. Load the value into the T 1 CON register. 2. Load reg. TMR 1 H followed by reg. TMR 1 L with initial value. 3. Start the timer with instruction T 1 CON=0 b 00000001; //Timer 1 on 4. Keep monitoring the timer flag (TMR 1 IF) to see if it is raised. 5. Stop the timer. 6. Clear the TMR 1 IF flag. PIR 1=0 b 0000; //Clear interrupt flag 7. Go Back to step 2. 23

Example 2: Program o Write a program to generate a square wave of 50%

Example 2: Program o Write a program to generate a square wave of 50% duty cycle on pin PORTB. 5. Use Timer 1 without prescaler (use 10 MHz clock). #include <htc. h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 10000000 #define LED 5 RB 5 void pic_init(void); void timer_init(void); main() { pic_init(); //initialize PIC timer_init(); //initialize Timer Module while(1) { T 1 CON=0 b 00000001; //start Timer 1 24

Example 2: Program if(PIR 1==0 b 00000001) { T 1 CON=0 b 0000; LED

Example 2: Program if(PIR 1==0 b 00000001) { T 1 CON=0 b 0000; LED 5 = !LED 5; PIR 1=0 b 0000; TMR 1 H=0 x. FF; TMR 1 L=0 x. F 2; } } //off Timer 1 //Toggle portb bit 5 //Clear interrupt flag //Re-load TMR 1 H and TMR 1 L } void pic_init(void) { TRISB=0 b 0000; PORTB=0 b 0000; } void timer_init(void) { T 1 CON=0 b 0000; TMR 1 H=0 x. FF; TMR 1 L=0 x. F 2; } 25

Example 2: Circuit Layout 26

Example 2: Circuit Layout 26

Example 2: Simulation Result 27

Example 2: Simulation Result 27

Example 2: Analysis o o o Timer 1 counts up from FFF 2, FFF

Example 2: Analysis o o o Timer 1 counts up from FFF 2, FFF 3, FFF 4, …. , FFFFH. From FFFFH to 0000 H, TMR 1 IF is set to 1. T = 4/10 MHz = 0. 4 s (Each tick consume 0. 4 s). How many tick? (FFFF-FFF 2) + 1 = 14 Decimal ticks. Time delay = 14 x 0. 4 s = 5. 6 s for half the pulse 1 2 13 14 FFF 2 FFF 3 FFF 4 FFFE FFFF 0000 TMR 0 IF=0 TMR 0 IF=1 28

Delay Calculation o General formula for delay calculation n T = 4/(10 MHz) =

Delay Calculation o General formula for delay calculation n T = 4/(10 MHz) = 0. 4 second a) In Hex (FFFF-FFF 2+1)x 0. 4 s FFF 2 are TMR 1 H and TMR 1 L initial values(in Hex). b) In Decimal (65535 -65523+1)x 0. 4 s 65523 are TMR 1 H and TMR 1 L initial values(in Decimal). 29

16 -bit register timer delay o If we know the amount of timer delay,

16 -bit register timer delay o If we know the amount of timer delay, we find the initial values needed for the TMR 1 H and TMR 1 L registers. o From example 2(no prescaler): T = 4/(10 MHz) = 0. 4 second. n (FFFF-N+1)x 0. 4 s=5. 6 s. n Whereby N is equal to FFF 2 H or 65523 d. n 30

Example 3 o Write a program to generate a square wave with a period

Example 3 o Write a program to generate a square wave with a period of 10 ms on pin PORTB. 3. o Solution: n (FFFF-N+1)x(4/10 Mhz)=5 ms(5 ms low and 5 ms high). n Therefore N should be CF 2 CH or 53036 d; need to use 16 -bit timer module since N is greater than 8 -bit. TMR 1 H =0 x. CF TMR 1 L=0 x 2 C n If Timer 0 chosen, N should be divided by 256(prescaler). 31

Example 3: Program #include <htc. h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF

Example 3: Program #include <htc. h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF); #define _XTAL_FREQ 10000000 #define LED 3 RB 3 void pic_init(void); void timer_init(void); main() { pic_init(); //initialize PIC timer_init(); //initialize Timer Module while(1) { T 1 CON=0 b 00000001; //start Timer 1 if(PIR 1==0 b 00000001) { T 1 CON=0 b 0000; //off Timer 1 LED 3 = !LED 3; PIR 1=0 b 0000; TMR 1 H=0 x. CF; TMR 1 L=0 x 2 C; } 32

Example 3: Program } } void pic_init(void) { TRISB=0 b 0000; PORTB=0 b 0000;

Example 3: Program } } void pic_init(void) { TRISB=0 b 0000; PORTB=0 b 0000; } void timer_init(void) { T 1 CON=0 b 0000; TMR 1 H=0 x. CF; TMR 1 L=0 x 2 C; } 33

Example 3: Circuit Layout 34

Example 3: Circuit Layout 34

Example 3: Simulation Result 35

Example 3: Simulation Result 35

Prescaler and generating larger delay o The size of delay depend on n The

Prescaler and generating larger delay o The size of delay depend on n The Crystal frequency n The timer’s bit register. o The largest timer happens when TMR 1 L=TMR 1 H=0 o Prescaler option is used to duplicate the delay by dividing the clock by a factor of 2, 4, 8, 16, 32, 64 , 128, 256. n If OPTION_REG =0000 0101, then T = 4*64/f. XTAL Osc ÷ 4 ÷ 64 TMRx 9 -36 36

Counter Programming o Used to counts event outside the PIC n Increments the TMR

Counter Programming o Used to counts event outside the PIC n Increments the TMR 0 registers o T 0 CS in T 0 CON reg. determines the clock source, n If T 0 CS = 1, the timer is used as a counter n Counts up as pulses are fed from pin RA 4 (T 0 CKI) n What does T 0 CON=0110 1000 mean? o If TMR 1 CS=1, the timer 1 counts up as clock pulses are fed into pin RC 0 9 -37

Using external Crystal for Timer 1 clock o Timer 1 comes with two options,

Using external Crystal for Timer 1 clock o Timer 1 comes with two options, n clock fed into T 1 CKI o T 1 OSCEN=0 n Clock from a crystal connected to T 1 OSI-T 1 OSO (additional) o T 1 OSCEN=1 o 32 k. Hz Crystal is connected o Used for saving power during SLEEP mode doesn’t disable Timer 1 while the main crystal is shut down

Timer clock source External Source Internal Source 39

Timer clock source External Source Internal Source 39

Example 4 o Write a counter program for 8 -bit mode to count the

Example 4 o Write a counter program for 8 -bit mode to count the pulses and display the state of 8 -bit timer on PORTB. 40

Example 4: Program #include <htc. h> __CONFIG (FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF

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=0 b 00100000; //waiting for external input PORTB=TMR 0; //TMR 0 output through port B if(INTCON==0 b 00000100) { INTCON=0 b 0000; TMR 0=0; } } } 41

Example 4: Program void pic_init(void) { TRISA=0 b 1111; PORTA=0 b 0000; TRISB=0 b

Example 4: Program void pic_init(void) { TRISA=0 b 1111; PORTA=0 b 0000; TRISB=0 b 0000; PORTB=0 b 0000; } void timer_init(void) { OPTION_REG=0 b 00100000; TMR 0=0; } //whole Port. A as input //Clear port A //whole Port. B as output //Clear port B //Timer 0 external input, prescale 1: 2 42

Example 4: Circuit layout 43

Example 4: Circuit layout 43

Example 4: Simulation Result 44

Example 4: Simulation Result 44

References o Jie Hu , ECE 692 Embedded Computing Systems , Fall 2010. o

References o Jie Hu , ECE 692 Embedded Computing Systems , Fall 2010. o PIC Microcontroller And Embedded Systems: using Assembly and C for PIC 18, M. Mazidi, R. Mc. Kinlay and D. Causey, Prentice Fall, 2008. o Eng. Husam Alzaq, Embedded System Course, IUG, 2010 45