Microcontroller part 5

Embed Size (px)

Citation preview

  1. 1. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Embedded System PART 5 (Timers) ENG.KEROLES SHENOUDA 1
  2. 2. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Previous Session covered .. 2
  3. 3. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Previous session 3 Covered those points Interrupt definition & Servicing Interrupts IVT & ISR & Vector Section Polling vs. Interrupt Steps in executing an interrupt Sequential interrupt processing VS Nested interrupt processing Types of Interrupts Interrupt Controller Examples On Interrupt Controllers Interrupt Controller On Atmega32
  4. 4. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here How to generate assembly file from elf image (executable file)? 4
  5. 5. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Disable auto generate MakeFile 5
  6. 6. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Add this command on your MakeFile 6
  7. 7. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Then Build you will see ATMEGA32_Drivers_disassemply.txt is generated 7 Now you can see the _Vectors section
  8. 8. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here 8 Interrupts & Timers Interrupt Latency: Time from event to execution of service routine Interrupt Response Time: Interrupt latency + Time for service routine Interrupt Termination: Time taken after interrupt service routine
  9. 9. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Timer EMBEDDED MODULE (GENERIC CONCEPT) 9
  10. 10. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here What is a timer ? At a high level, a timer is just a Counter register that counts up or down automatically. A Counter can be used to keep track of time. It can increment/decrement a count register based on a clock and to check if the count has reached some value and trigger an action. 17 Peripherals Timers generally have a resolution of 8 or 16 or 32 Bits. So a 8 bit timer is 8Bits wide so capable of holding value within 0-255. But this register has a magical property ! Its value increases/decreases automatically at a predefined rate (supplied by user). This is the timer clock. And this operation does not need CPUs attention. 10
  11. 11. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Counters Counter: like a timer, but counts pulses on a general input signal rather than clock e.g., count cars passing over a sensor Can often configure device as either a timer or counter 11 16-bit up counter Clk 16 Cnt_in 2x1 mux Mode Timer/counter Top Reset Cnt
  12. 12. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here HOW TO COUNT Up: the timer starts at zero, then counts up until it hits some maximum value. The maximum value is stored in a special register called a compare register. The maximum value can be the largest value the timer register can hold (like 0xFF for 1Byte Counter) or some arbitrary value. Down: the timer starts at some value you set and counts down until it reaches zero. Up/Down: the timer counts up until it reaches the value in the compare register, and then counts down until it reaches zero. 12 Counter register External source Oscillator Counter/Timer COUT 0 1 Flag
  13. 13. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Timer used to timer will produce a regular output with the same accuracy as the input clock. This output could be used to generate a periodic interrupt like a real-time operating system (RTOS) timer tick. provide a baud rate clock to a UART. drive any device that requires a regular pulse. Delay generating Counting Wave-form generating Capturing 13
  14. 14. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Timer Infra Structure 14 If we want to calculate 1ms CLK Oscillator = 24 MHz Timer clock= (24 / 12) MHz = 2 MHz Timer cycle = 500ns Delay count = delay time / cycle time 1 ms / 500 ns = 2000 Counter numbers= (216 -1)-2000 = 63535 Set Count value 63535 to max or set count 0 > 2000-1 16-bit counterClk 16 External Clock 2x1 mux Mode Controller irq Reset Cnt Clock Divider External Input Pin Output pin
  15. 15. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Timer Infra Structure Timer May have a number of counters is called Channel Each channel have a counter 15
  16. 16. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Timer Modes Group 16 Synchronous Modes/ Cascaded counters Asynchronous Mode/ Independent Modes The Master Channel Out will Control the other Channels Maybe each channel/counter runs in different Mode
  17. 17. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Watchdog (WDOG) 17
  18. 18. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Watchdog (WDOG) WDOG is intended to be used to apply a reset to a system in the event of a failure. WDOG consists of counter with a programmable timeout interval that has the capability to generate an interrupt and a reset signal on timing out. For normal operation the software has to ensure that the counter never reaches zero (kicking the dog) Usually WDOG interrupt is set with highest priority. 18
  19. 19. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Timers in ATMEGA32 TWO 8-BIT TIMERS AND ONE 16-BIT TIMER IN ATMEGA32 19
  20. 20. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Timers in ATMEGA32 TCNTn (Timer/Counter register) TOVn (Timer Overflow flag) TCCRn (Timer Counter control register) OCRn (output compare register) OCFn (output compare match flag) 20 Flag Counter register External source Oscillator Counter/Timer TCNTn TCCRn TOVn OCRn = OCFn Comment: All of the timer registers are byte-addressable I/O registers
  21. 21. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Atmega 32 Timer 0 Modes Normal Mode Clear Timer on Compare Match (CTC) Mode Fast PWM Mode Phase Correct PWM Mode 21
  22. 22. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Hand Writing Notes 22
  23. 23. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Hand Writing Notes 23
  24. 24. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Hand Writing Notes 24
  25. 25. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Normal Mode The simplest mode of operation is the Normal mode (WGM01:0 = 0). In this mode the counting direction is always up (incrementing), and no counter clear is performed. The counter simply overruns when it passes its maximum 8-bit value (TOP = 0xFF) and then restarts from the bottom (0x00). In normal operation the Timer/Counter Overflow Flag (TOV0) will be set in the same timer clock cycle as the TCNT0 becomes zero. The TOV0 Flag in this case behaves like a ninth bit, except that it is only set, not cleared. However, combined with the timer overflow interrupt that automatically clears the TOV0 Flag, the timer resolution can be increased by software. There are no special cases to consider in the Normal mode, a new counter value can be written anytime. The Output Compare unit can be used to generate interrupts at some given time. Using the Output Compare to generate waveforms in Normal mode is not recommended, since this will occupy too much ofthe CPU time. 25
  26. 26. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here OCF2 TOV2 ICF1 OCF1A OCF1B TOV1 OCF0 TOV0 TIFR TCNT0 TCCR0 TOV0 OCR0 = OCF0 Timer 0 (an 8-bit timer) 26
  27. 27. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here TCCR0 COM01WGM00FOC0 COM00 CS02 CS01 CS00 TCCR0WGM01 CS02 CS01 CS00 Comment 0 0 0 No clock source (Timer/Counter stopped) 0 0 1 clk (No Prescaling) 0 1 0 clk / 8 0 1 1 clk / 64 1 0 0 clk / 256 1 0 1 clk / 1024 1 1 0 External clock source on T0 pin. Clock on falling edge 1 1 1 External clock source on T0 pin. Clock on rising edge Clock Selector (CS) WGM00 WGM01 Comment 0 0 Normal 0 1 CTC (Clear Timer on Compare Match) 1 0 PWM, phase correct 1 1 Fast PWM Timer Mode (WGM) 10-bit T/C Prescaler Clear 0 CS00 CS01 CS02 T0 clkIO PSR10 Timer/Counter0 clock source clk/1024 clk/256 clk/64 clk/8 0 1 2 3 4 5 6 7 27
  28. 28. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here 28
  29. 29. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here 29 TCCR0
  30. 30. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here 0xFF TCNT0 0 TOVTOV TOV time 0TOV0: 1 FE FF TOV0 = 1 2 1 0 Normal Mode 30
  31. 31. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here $100 == 256 -$0E $F2 14 = $0E COM01WGM00FOC0 COM00 CS02 CS01 CS00 TCCR0WGM01 0 0 10 0 Counter Register COUT UP Load 8 $F2 10-bit T/C Prescaler Clear 0 CS00 CS01 CS02 T0 clkIO PSR10 Timer/Counter0 clock source clk/1024 clk/256 clk/64 clk/8 0 1 2 3 4 5 6 7
  32. 32. https://www.facebook.com/groups/embedded.system.KS/ Follow us Press here Example 1: write a program that waits 14 machine cycles in Normal mode. DDRB = 1