1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

3 chapter 3 timer counter modules

19 171 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 19
Dung lượng 838,1 KB

Nội dung

MICROCONTROLLERS CHAPTER TIMERS – COUNTERS MODULES Dr Vo Tuong Quan HCMUT - 2011 TIMER - COUNTER Definition -The operations of Timer and Counter in microcontroller are similar -Timer: use to count the time -Counter: use to count the event or some other things -The capacity of Timer/Counter is based on the number of bits they have Ex: Timer bits  can be count from to 255 Timer 16 bits  can be count from to 65535 Timer 32 bits  can be count from to 4294967296 -Timer/Counter have two modes: Count Up/ Count Down - Timer needs a clock source, for example of the clock source of 10KHz is input to a timer then one increment will take 100uS (micro second) This clock source can be obtained from the MCU clock  2011 – Vo Tuong Quan TIMER - COUNTER Definition -The operations of Timer and Counter in microcontroller are similar -Timer: use to count the time -Counter: use to count the event or some other things -The capacity of Timer/Counter is based on the number of bits they have Ex: Timer bits  can be count from to 255 Timer 16 bits  can be count from to 65535 Timer 32 bits  can be count from to 4294967296 -Timer/Counter have two modes: Count Up/ Count Down - Timer needs a clock source, for example of the clock source of 10KHz is input to a timer then one increment will take 100uS (micro second) This clock source can be obtained from the MCU clock  2011 – Vo Tuong Quan TIMER - COUNTER Prescaler The function of prescaler is to divide the CPU clock to obtain a smaller frequency Some typicle prescaler division factors: 256 128 64 32 16 (Prescaler by-passed) What is watchdog timer? A timer that monitors how long it takes the MCU to complete a scan Watchdog timers output an error message if the MCU scan takes too long  2011 – Vo Tuong Quan TIMER - COUNTER Example: Using Timer of pic18f4520 TIMER0 control rsegister Name T0CON Initial Value TMR0ON T08BIT T0CS T0SE PSA PS2 PS1 PS0 1 1 1 1 BIT7 - TMR0ON: Timer0 On, set this to to start the timer BIT6 - T08BIT: =1 for bit mode and =0 for 16 bit mode BIT5 - T0CS: Timer0 clock source =1 for T0CLK pin input i.e counter mode Set to for internal instruction clock BIT4 - T0SE: Used in counter mode only Please see datasheet for details BIT3 - PSA: Prescaler Assignment Set this to to assign prescaler or to by pass it BIT2 to BIT0 - PS2 to PS0: Prescaler Division factor select  2011 – Vo Tuong Quan TIMER - COUNTER PS2 PS1 PS0 0 Divide by 0 Divide by Divide by 1 Divide by 16 0 Divide by 32 1 Divide by 64 1 Divide by 128 1 Divide by 256  2011 – Vo Tuong Quan TIMER - COUNTER Example code: // Using FOSC = 20MHZ, Timer bit unsigned char counter=0;//Overflow counter void main() { //Setup Timer0 T0PS0=1; //Prescaler is divide by 256 T0PS1=1; T0PS2=1; PSA=0; //Timer Clock Source is from Prescaler T0CS=0; //Prescaler gets clock from FMCU (5MHz) T08BIT=1; //8 BIT MODE TMR0IE=1; //Enable TIMER0 Interrupt GIE=1; //Enable INTs globally TMR0ON=1; //Now start the timer //Set RB1 as output because we have LED on it TRISB&=0B11111101; while(1); //Infinited loop }  2011 – Vo Tuong Quan TIMER - COUNTER //Main Interrupt Service Routine (ISR) void interrupt ISR() { //Check if it is TMR0 Overflow ISR if(TMR0IF) //Interrupt Flag { //TMR0 Overflow ISR counter++; //Increment Over Flow Counter if(counter==76) //How to get the 76 value? How many second to delay? { if(RB1==0) RB1=1; else RB1=0; counter=0; //Reset Counter } //Clear Flag TMR0IF=0; TMR0ON=1; } }  2011 – Vo Tuong Quan TIMER - COUNTER How to get the value 76 of counting value? Prescaler = FMCU/256 (Note: FMCU= Fosc/4) As our FMCU=20MHz/4 (We are running 20MHz XTAL) =5MHz Time Period = 0.2uS Prescaller Period = 0.2 x 256 = 51.2uS Overflow Period = 51.2 x 256 = 13107.2 uS = 0.0131072 sec So we need 1/0.0131072 Over Flows to count for sec = 76.2939 Overflows  2011 – Vo Tuong Quan TIMER - COUNTER OPTION_REG: PSA=0; // Prescaler is assigned to the Timer0 module PS0=1; // Prescaler rate bits PS1=1; // are set to “111” PS2=1; // which means divide by 256 TOSE=0; // rising edge TOCS=0; // Internal instruction cycle clock 10  2011 – Vo Tuong Quan TIMER - COUNTER Another method to calculate the value of Timer: The TIMER0 period could be calculated using this formula: TIMER0 period = [(TMR0 + 1)] x x Tosc x (TIMER0 prescaler value) Ex: By selecting the TIMER0 prescaler of 2; PS2=0, PS1=0 and PS0=0 bits in OPTION_REG register and Initial the TMR0 register value to 156 (99 more counts to reach its maximum value of 255) with the system frequency clock of Mhz the PIC microcontroller TIMER0 overflow period can be calculated as follow: TIMER0 period = [((255 - 156) + 1)] x x 1/8000000 x = 0.0001s = 0.1 ms 11  2011 – Vo Tuong Quan Sample Code TIMER - COUNTER /* Init TIMER0: Period: Fosc/4 x Prescaler x TMR0 0.0005 ms x * 100 = 0.1 ms */ OPTION = 0b00000000; // 1:2 Prescaller TMR0 = 156; // Interupt every 0.1 ms T0IE = 1; // Enable interrupt on TMR0 overflow GIE = 1; // Global interrupt enable ……………… // ISR (Interrupt Service Routine) if(T0IF) { // TIMER0 Interrupt Flag pulse_max++; // Pulse Max Increment pulse_top++; // Pulse Top Increment if (pulse_max >= MAX_VALUE) { pulse_max=0; pulse_top=0; RC2=0; // Turn Off RC2 } if (pulse_top == top_value) { RC2=1; } TMR0 = 156; // Initial Value for 0.1ms Interrupt T0IF = 0; // Clear TIMER0 interrupt flag }  2011 – Vo Tuong Quan 12 TIMER - COUNTER How to calculate the value of 156 or the value 99 of the TMR0? TIMER0 period = [(TMR0 + 1)] x x Tosc x (TIMER0 prescaler value)  TMR0 = {TIMER0 period/(4 x Tosc x TIMER0 prescaler value)} –  TMR0 = {0.1ms / (4 x (1/8MHz)) x 2)} – = 99  TMR0 = {0.0001s / (4 x (1/8000000) x 2)} - 1= 99 (Count value) The value input to the TMR0 register = = Max timer value – Count = 255 – 99 = 156 13  2011 – Vo Tuong Quan TIMER - COUNTER If using INTERNAL crystal as clock: fout– The output frequency after the division Tout – The Cycle Time after the division - The division of the original clock (4 MHz) by 4, when using internal crystal as clock (and not external oscillator) Count - A numeric value to be placed to obtain the desired output frequency - Fout (256 - TMR0) - The number of times in the timer will count based on the register TMR0 14  2011 – Vo Tuong Quan TIMER - COUNTER Example: Suppose we want to create a delay of 0.5 second using Timer0 What is the value of Count? Calculation: First, let’s assume that the frequency division by the Prescaler will be 1:256 Second, let’s set TMR0=0 Thus: 15  2011 – Vo Tuong Quan TIMER - COUNTER If using EXTERNAL crystal as clock: Example: What is the output frequency - Fout, when the external oscillator is 100kHz and Count=8? Calculation: First, let’s assume that the frequency division by the Prescaler will be 1:256 Second, let’s set TMR0=0 16  2011 – Vo Tuong Quan TIMER - COUNTER Example: Delay second 17  2011 – Vo Tuong Quan TIMER - COUNTER Exercise: Using PIC16f877, the oscillator is 20MHz, write program to create the square pulse on pin A1 with the cycle is 50% and the frequency is 5KHz Using PIC16f877, the oscillator is 20MHz, write program to create the square pulse on pin A1 with the cycle is 70% and the frequency is 10KHz the duty the duty 18  2011 – Vo Tuong Quan TIMER - COUNTER Timer bits – Timer 16 bits What is the differences between them? 19  2011 – Vo Tuong Quan

Ngày đăng: 07/11/2017, 14:17

TỪ KHÓA LIÊN QUAN

w