Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

AVR_Course_Day7 timers counters and interrupt programming

This is a microcontroller course for atmega328 (the chip used in Arduino Uno) explained very simply .

Alle anzeigen

Ähnliche Hörbücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Als Erste(r) kommentieren

AVR_Course_Day7 timers counters and interrupt programming

1. 1. TimersCounters & Interrupt Programming
2. 2. Contents 1. Introduction 2. 8 bit Timers 3. Interrupts. 4. 16 bit Timers
3. 3. TimerCounters 1. Introduction(Time Terminology) Frequency Consider signal x(t) that repeats itself. This signal called a periodic with period T if it satisfies: x(t)=x(t+T). To measure the frequency of a periodic signal, we count the number of times a particular event repeats within a 1-s period. Period  The flip side of a frequency is a period.  If an event occurs with a rate of 1 Hz, the period of that event is 1 s.  To find a period, given a frequency, or vice versa, f=1/T.
4. 4. TimerCounters 1. Introduction(Time Terminology) Duty Cycle  In many applications, periodic pulses are used as control signals.  A good example is the use of a periodic pulse to control a servo motor.  To control the direction and sometimes the speed of a motor, a periodic pulse signal with a changing duty cycle over time is used.
5. 5. TimerCounters 1. Introduction(Time Terminology) Timing System Overview:  The heart of the timing system is the crystal time base.  The crystal’s frequency of a microcontroller is used to generate a baseline clock signal.  For a timer system, the system clock is used to update the contents of a special register called a free-running counter.  For input time-related activities, all microcontrollers typically have timer hardware components that detect signal logic changes on one or more input pins. Such components rely on a free-running counter to capture external event times.
6. 6. TimerCounters 1. Introduction(Time Terminology)  For output timer functions, a microcontroller uses a comparator, a free-running counter , logic switches, and special-purpose registers to generate time-related signals on one or more output pins.
7. 7. TimerCounters 1. Introduction(Applications)  Measure an input signal timing event.  Count the number of external signal occurrences.  Generate timed signals---termed output compare.  Generate PWM signals.
8. 8. TimerCounters 1. Introduction TimerCounter used for the following: 1) Count an event (by connecting external source to increment the counter registers). 2) Generate time delay (Oscillators or internal clock is used to increment the counter registers). There are three timers on Atmega328: 1) Timer (0) which is 8 bit timer. 2) Timer (1) which is 16 bit timer. 3) Timer (2) which is 8 bit timer
9. 9. TimerCounters 1. Introduction
10. 10. TimerCounters 2. 8 bit Timers(Timer 0) Features: • Two Independent Output Compare Units • Double Buffered Output Compare Registers • Clear Timer on Compare Match (Auto Reload) • Phase Correct Pulse Width Modulator (PWM) • Variable PWM Period • Frequency Generator • Three Independent Interrupt Sources (TOV0, OCF0A, and OCF0B)
11. 11. TimerCounters The architecture of Timer (0) shown in the following figure: 2. 8 bit Timers(Timer 0)
12. 12. TimerCounters 2. 8 bit Timers(Timer 0)
13. 13. TimerCounters 2. 8 bit Timers(Timer 0)
14. 14. TimerCounters 2. 8 bit Timers(Timer 0) Normal Mode:the timer will continually count from 0x00 (BOTTOM) to 0xFF (TOP).  When the TCNT0 returns to 0 on each cycle of the counter, the Timer/Counter Overflow Flag (TOV0) will be set.  The normal mode is useful for generating a periodic ‘‘clock tick’’ that may be used to calculate elapsed real time or provide delays within a system.
15. 15. TimerCounters 2. 8 bit Timers(Timer 0) Clear Timer on Compare Match:In the CTC modem, the TCNT0 timer is reset to 0 every time the TCNT0 counter reaches the value set in OCR0.  The Output Compare Flag 0 (OCF0) is set when this event occurs.  The OCF0 flag is enabled by asserting the Timer/Counter 0 Output Compare Match Interrupt Enable (OCIE0) flag in the TIMSK and when the I-bit in the Status Register is set to 1  The CTC mode is used to generate a precision digital waveform such as a periodic signal or a single pulse.
16. 16. TimerCounters 2. 8 bit Timers(Timer 0) Phase Correct PWM Mode: In the Phase Correct PWM mode, the TCNT0 register counts from 0x00 to 0xFF and back down to 0x00 continually. Every time the TCNT0 value matches the value set in the OCR0 register, the OCF0 flag is set and a change in the PWM signal occurs.
17. 17. TimerCounters 2. 8 bit Timers(Timer 0) Fast PWM:is used to generate a precision PWMsignal of a desired frequency and duty cycle.  It is called the fast PWM because its maximum frequency is twice that of the Phase Correct PWM mode.  When the TCNT0 register value reaches the value set in the OCR0 register, it will cause a change in the PWM output as prescribed by the system designer.  It continues to count up to the TOP value, at which time the Timer/Counter 0 Overflow Flag(TOV0) is set.
18. 18. TimerCounters 2. 8 bit Timers(Timer 0) TCNTn: TimerCounter register it counts up every pulse. Also it can be loaded by a certain value. In each TCNTn there are: WGMnm: Wave generation mode (Normal mode ,CTC mode ,Fast PWM ,..etc.). CSnm: Prescalar(8,64,256,1024). TOVn: Timer overflow flag, when a timer overflow a flag will be set. TCCRn: TimerCounter control register ,its for setting mode of operation by loading the proper value for ex(Normal mode ,CTC mode ,Fast PWM ,..etc.)will be explained later.
19. 19. TimerCounters 2. 8 bit Timers(Timer 0) TIMSK :Timer/Counter Interrupt Mask Register register is used by all three timer channels. Timer 0 uses the Timer/Counter 0 Output Compare Match Interrupt Enable (OCIE0) bit and the Timer/Counter 0 Overflow Interrupt Enable (TOIE0) bit.
20. 20. TimerCounters 2. 8 bit Timers(Timer 0)
21. 21. TimerCounters 2. 8 bit Timers(Timer 0)
22. 22. TimerCounters 2. 8 bit Timers(Timer 0)
23. 23. TimerCounters 2. 8 bit Timers(Timer 0)
24. 24. TimerCounters 2. 8 bit Timers(Timer 0)
25. 25. TimerCounters 2. 8 bit Timers(Timer 0)
26. 26. TimerCounters 2. 8 bit Timers(Timer 0)
27. 27. TimerCounters 2. 8 bit Timers (Normal Mode) There are two equation to calculate the value of TCNT0: 1) In Decimal 256-(TdTc)=TCNT0 2) In Hexa FF+1-(TdTc)=TCNT0 Td: Time on desired from MCU. Tc: Timer Clock of the MCU ((1Fosc)*Prescalar=Tosc*Prescalar). OSC: Oscillators (the Crystal). Prescalar: is used to divide this clock frequency and produce a clock for TIMER.
28. 28. TimerCounters 2. 8 bit Timers (Normal Mode) Ex(1): Assuming xtal=8MHZ,and required to generate a square wave with period of 12.5 us. Td=12.5us/2=6.25us. Tc=1/8MHZ=0.125us. 256-(6.25/0.125)=206=0xCE. TCNT0=0xCE. EX(2): Assuming xtal=8MHZ,Calculate the frequency of square wave knowing that the TCNT0=0x3E Td=(FF+1-3E)*0.125us=24.25us. F=1/2*Td=12*24.25us=20.61KHZ.
29. 29. TimerCounters 2. 8 bit Timers (Normal Mode) Prescaler and generating a large time delay: To get the maximum delay time Td=(256-0)*Tc. (for xtal=8MHZ) ,Td=32us F=12*Td=12*32us=15KHZ To maximize the period prescaler is used Using prescaler=8 Td=256us. Prescaler=64 Td=2048 us. Prescaler=256 Td=8192us. Prescaler=1024 Td=32768us. Ex(3): Assuming xtal=8Mhz generate a square wave of 125 Hz,using timer 0 and prescaler of 256 T=1125=8ms Td=4ms. Tc=18MHZ *256=32us. 256-(4ms32us)=256-125=131. TCNT0=0x83.
30. 30. TimerCounters Steps of Programming Timer 0 for Normal mode : 1) Load the TCCR0 (A,B) with the proper value according to the mode of operation. 2) Load the TCNTn with proper values according to the oscillator frequency(1/f*Prescalar=Tc*Prescalar) and the desired (Td)time required with or without the prescalar. 3) If the timer overflow set the TOV to clear the timer and to count again in (normal mode) Clear the flag or OCF0. 5) Repeat it again. 2. 8 bit Timers (Normal Mode)
31. 31. Ex(4) Find the time delay (Td) when the OCR0=9, assuming that xtal=8Mhz The TCNT0 will be cleared after 9+1=10 counts Td=10*0.125us=1.25us. EX(5) Using the CTC mode find the OCR0 value to generate a time delay of (25.6ms) using the prscaler 1024 Td=25.6ms Tc=0.125us*1024=128us Number of counts=Td/Tc=200 OCR0=200-1=99. TimerCounters 2. 8 bit Timers (CTC mode)
32. 32. TimerCounters Steps of Programming Timer 0 for Normal mode : 1) Load the TCCR0 (A,B) with the proper value according to the mode of operation. 2) Load the TCNTn with proper values according to the oscillator frequency(1/f*Prescalar=Tc*Prescalar) and the desired (Td)time required with or without the prescalar. 3) Set the OCF0 when the TCNT0 = OCR0 to clear the timer and count again. 4) Clear the flag or OCF0. 5) 5) Repeat it again. 2. 8 bit Timers (CTC Mode)
33. 33. Interrupt Interrupts are basically events that require immediate attention by the microcontroller. When an interrupt event occurs the microcontroller pause its current task and attend to the interrupt by executing an Interrupt Service Routine (ISR) at the end of the ISR the microcontroller returns to the task it had pause and continue its normal operations. In order for the microcontroller to respond to an interrupt event the interrupt feature of the microcontroller must be enabled along with the specific interrupt. This is done by setting the Global Interrupt Enabled bit and the Interrupt Enable bit of the specific interrupt. Interrupt Types: 1. Internal Interrupt (e.g. Timer interrupt). 2. External Interrupt ( will be discussed in the next session).
34. 34. Interrupt Each Interrupt associated to vector number shown in the following vector table (See other routines in the data sheet)
35. 35. Interrupt Why using Interrupt in Timers??!!!
36. 36. Interrupt VS Polling: There are two methods to receive a service from microcontroller. 1) Polling method: the microcontroller continuously monitors the state of the device ,when the state of the device met the condition the microcontroller perform the service. After that it returns again to monitor the state again. 2) Interrupt method: whenever any device needs a service, it notifies the microcontroller by sending an interrupt signal. Upon receiving the signal the microcontroller gets out the infinite loop and serves the device which is associated with the interrupt it’s called interrupt service routine. Interrupt
37. 37. Advantages of Interrupts over Polling: 1) Every device can get the attention of the microcontroller by sending an interrupt signal, when two devices send an interrupt signal at same time the microcontroller serves the device with the high priority. 2) Polling wastes the microcontroller time by polling a device(s)that don’t need a service and tying it down from performing any other tasks. Interrupt
38. 38. Interrupt Steps of Executing an Interrupt: 1) The microcontroller finished the instruction that currently executing and then saved the address of the next instruction (program counter) on the stack. 2) It jumps to a fixed location in the memory (interrupt vector).The interrupt vector directs the microcontroller to the address of ISR. 3) The microcontroller starts to executes the ISR until the it reaches the last instruction RETI(return from interrupt). 4) The microcontroller returns to the place where its interrupted first, it gets the program counter from the stack then starts to execute from that address.
39. 39. Interrupt Enabling the Interrupts: To enable the interrupt is should be unmasked as shown in the following figure.  SREG is the Status register ,to enable the interrupt (unmask the interrupt) the bit D7(I) should be activated using sei() set interrupt then use TMISK register to enable interrupts of Timer (0 or 1 or 2)this will covered later.  To disable the interrupt the bit D7(I) should be cleared by cli()clear the interrupt.
40. 40. Interrupt Registers associated with Timer interrupt
41. 41. TimerCounters Assignment Do the same programs of timer 0 on timer 2
42. 42. TimerCounters 3. 16 bit Timers Features: • True 16-bit Design (i.e., Allows 16-bit PWM) • Two independent Output Compare Units • Double Buffered Output Compare Registers. • One Input Capture Unit. • Input Capture Noise Canceler. • Clear Timer on Compare Match (Auto Reload). • Phase Correct Pulse Width Modulator (PWM). • Variable PWM Period. • Frequency Generator. • External Event Counter. • Four independent interrupt Sources (TOV1, OCF1A, OCF1B, and ICF1).
43. 43. TimerCounters 3. 16 bit Timers The architecture of Timer (1) shown in the following figure:
44. 44. TimerCounters 3. 16 bit Timers
45. 45. TimerCounters 3. 16 bit Timers ICNC1: Input Capture Noise Canceler Setting this bit (to one) activates the Input Capture Noise Canceler. When the noise canceler is activated, the input from the Input Capture pin (ICP1) is filtered. ICES1: Input Capture Edge Select This bit selects which edge on the Input Capture pin (ICP1) that is used to trigger a capture event. When the ICES1 bit is written to zero, a falling (negative) edge is used as trigger, and when the ICES1 bit is written to one, a rising (positive) edge will trigger the capture. ICIE1: Timer/Counter1, Input Capture Interrupt Enable When this bit is written to one, and the I-flag in the Status Register is set (interrupts globally enabled), the Timer/Counter1 Input Capture interrupt is enabled. ICF1: Timer/Counter1, Input Capture Flag This flag is set when a capture event occurs on the ICP1 pin
46. 46. TimerCounters 3. 16 bit Timers
47. 47. TimerCounters 3. 16 bit Timers
48. 48. TimerCounters 3. 16 bit Timers
49. 49. TimerCounters 3. 16 bit Timers
50. 50. TimerCounters 3. 16 bit Timers
51. 51. TimerCounters 3. 16 bit Timers
52. 52. TimerCounters 3. 16 bit Timers Assignment Generate a square wave of 1 sec using the normal mode and the CTC mode. Repeat it using interrupt method.