counter timers overview
play

Counter/Timers Overview ATmega328P has two _____ and one ______ - PowerPoint PPT Presentation

13.1 13.2 Counter/Timers Overview ATmega328P has two _____ and one ______ counters. Can configure to count at some frequency up to some value (a.k.a. ________________), generate an _____________ Unit 13 Timers and Counters and


  1. 13.1 13.2 Counter/Timers Overview • ATmega328P has two _____ and one ______ counters. – Can configure to count at some frequency up to some value (a.k.a. ________________), generate an _____________ Unit 13 – Timers and Counters and ___________________ counting again, if desired – Useful for performing operations at specific time intervals. Every time an interrupt occurs, do something. – Can be used for other tasks such as pulse-width modulation (covered in future lectures) • But don't we already have delay()…why do we need timers? – So that we can do _________________________ while we are waiting for time to elapse! 13.3 13.4 General Overview of Timer HW Counter/Timer Registers • Bad News: Lots of register bits to deal with Timer HW Module Modulus A (OCRxA) 0000 0010 0000 0000 Start Over @ 0? Interrupt 0000 0001 1001 1100 = if equal 16-bit Counter (TCNTx) System Clock Interrupt (16MHz Arduino) Increments every = ÷ ÷ ÷ ÷ if equal prescaled "clock" 0000 1010 0110 1100 Prescalar (1,8,256,1024) Modulus B (OCRxB) We'll just use the modulus A register so you can ignore B for our class

  2. 13.5 13.6 Counter/Timer Registers Computing the Desired Cycle Delay • Good News: Can _______________ for simple timing • Primary step : calculate how many processor _________________ are required for your desired delay – Desired clock cycles = ________________ × ______________ – Arduino UNO clock is fixed at 16 MHz • Example: 0.25 second delay with a 16 MHz clock – Desired clock cycles = 16,000,000 c/s × 0.25s = _____________ cycles • Problem: The desired value you calculate must fit in at most a __________________ (i.e. max _________) – If the number is bigger than 65,535 then a prescalar must be used to reduce the clock frequency to the counter from 16MHz to something slower 13.7 13.8 Calculating the Prescalar Counter/Timer Initialization 1 • Set the mode for “Clear Timer on Compare” (CTC) – WGM13 = ________, WGM12 = __________ • The counter prescaler divides the processor clock down to a – This tells the hardware to _______________ at 0 lower frequency so the counter is counting ______________. once the counter is reaches your desired value • Can divide the processor clock by four different powers of // Set to CTC mode • Enable “Output Compare A Match Interrupt” TCCR1B |= (1 << WGM12); two: _____, _______, ________, or _________. – OCIE1A = 1 // Enable Timer Interrupt • Load the 16-bit counter modulus into ___________ TIMSK1 |= (1 << OCIE1A); • Try prescalar options until the cycle count fits in 16-bits – This is the value the counter will count up to and // Load the MAX count – 4,000,000 / 8 = 500,000 // Assuming prescalar=256 then generate an interrupt. // counting to 15625 = – 4,000,000 / 64 = 62,500 // 0.25s w/ 16 MHz clock – The counter then clears to zero and starts counting OCR1A = 15625; 4,000,000 / 256 = 15,625 – up again. – In C, the register can be accessed as… – 4,000,000 / 1024 = 3906.25 • A 16-bit value "OCR1A" • In this example, either of the _____________ could work but Or as two eight bit values "OCR1AH" and OCR1AL”. • since we can only store integers in our timer count registers the last one would not yield exactly 0.25s (more like 0.249984s)

  3. 13.9 13.10 Counter/Timer Initialization 2 Counter/Timer Initialization 3 • Select the prescalar value with bits: • Make sure you have an appropriate #include <avr/io.h> #include <avr/interrupt.h> CS12, CS11, CS10 in TCCR1B reg. ISR function defined // Set to CTC mode volatile unsigned char qsecs = 0; TCCR1B |= (1 << WGM12); 000 = stop ⟸ Timer starts when – Using name ISR(TIMER1_COMPA_vect) void init_timer1(unsigned short m) – { // Enable Timer Interrupt TCCR1B |= (1 << WGM12); prescaler set to non-zero TIMSK1 |= (1 << OCIE1A); TIMSK1 |= (1 << OCIE1A); OCR1A = m; – 001 = clock/1 // Load the MAX count // Assuming prescalar=256 TCCR1B |= (1 << CS12); // counting to 15625 = } – 010 = clock/8 // 0.25s w/ 16 MHz clock int main() OCR1A = 15625; { – 011 = clock/64 init_timer1(15625); // Set prescalar = 256 – 100 = clock/256 // and start counter sei(); TCCR1B |= (1 << CS12); – 101 = clock/1024 while(){ // do something w/ qsecs // Enable interrupts • Enable _________________________ sei(); } return 0; } ISR(TIMER1_COMPA_vect){ // increments every 0.25s qsecs++; } 13.11 13.12 8-bit Counter/Timers ISR Names • The other two counters are similar but only 8-bits. • In CTC mode, an "Output Compare A Match Interrupt" will vector to an ISR with these names: • Same principle: find the count modulus that fits in an 8-bit value. – ISR(TIMER0_COMPA_vect) { } /* 8-bit Timer 0 */ – ISR(TIMER1_COMPA_vect) { } /* 16-bit Timer 1 */ – ISR(TIMER2_COMPA_vect) { } /* 8-bit Timer 2 */

  4. 13.13 13.14 An Example • Let's design a stopwatch (_______ units) • What are the inputs and outputs • Inputs – Buttons for ______________ – ____________________ • Outputs – LCD [___________ time format] Stopwatch Lab • Question: – What do I need state for in USING STATE MACHINES TO this design? Answer: • SIMPLIFY & ORGANIZE DESIGNS Start/Stop – Anytime you provide the same input (Top Btn) and different outputs/actions occur, there is state inside Lap/Reset – Different actions for same button press (Bottom/Btn) 13.15 13.16 Why Use State Machines Why Use State Machines • Easier to decouple relationship between input and output • It can be very hard/difficult to design a system • Let inputs _________________, then examine the state to where all the inputs can __________ each of decide what ________________ should be or do the outputs (i.e. an all-to-all relationship) • Similar to the popular MVC GUI & Web app design approach – Model->View->Controller (MVC design) – Model (State), View (Output), Controller (Input) Button1 Button1 LCD LCD Button 2 Button 2 State Other Output Other Timer Output Timer

  5. 13.17 13.18 Stopwatch Application Suggested Guidelines ... • What states do we need to differentiate button presses • Use a timer to generate an int main() { Start_Stop Btn. Lap_Reset interrupt every 0.1s // be sure to init. state On Startup unsigned char state = 0; • Use the timer ISR to // init and start timer Stopped Started Lapped perform ____________ // used to check inputs // and perform state updates while(1) Start_Stop and ______________ Lap_Reset / Lap_Reset || { Btn. Set_to_Zero Start_Stop // Poll inputs and update • Use __________ in main() // state } • When timer interrupt occurs examine the state to decide how return 0; to detect input button to update the display (or just leave current displayed time) } presses and update state • What else in this design is technically "state"? // Use to perform output tasks ISR(TIMER1_COMPA_vect) – Time: SS.Tenths (but not necessarily { // update time and – Every time the timer interrupts check to see if time needs to update & // display based on state display unless necessary) increment the time if necessary }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend