SlideShare uma empresa Scribd logo
1 de 31
Baixar para ler offline
Timer Module
Eman Aboelatta

Copyright © 2012 Embedded Systems
Committee
Atmega16Timer Modules:
ONE

TWO
8-bit Timer/Counters with
Separate Prescalers and
Compare Modes

16-bit Timer/Counter with
Separate Prescaler,
Compare Mode, and Capture
Mode

Copyright © 2012 Embedded Systems
Committee
Atmega32Timer Modules:
The timer of the AVR can be monitor three Events:
Timer Overflow.
Compare Match.
Input Capture(in timer1)
Also, Timer use in PWM Generation.

Copyright © 2012 Embedded Systems
Committee
Timer Overflow
 timer overflow means that the counter has counted up to its maximum
A
value and is reset to zero in the next timer clock cycle.
The resolution of the timer determines the maximum value of that timer.

The timer overflow event causes the Timer Overflow Flag (TOVx) to be set
in the Timer Interrupt Flag Register (TIFR).

There are two timers with 8-bit resolution and one timer with 16-bit
resolution on the ATmega16
Copyright © 2012 Embedded Systems
Committee
Compare Match




In cases where it is not sufficient to monitor a timer overflow.
the compare match interrupt can be used.



The Output Compare Register (OCRx) can be loaded with a value [0..
MaxVal] which the timer will be checked against every timer cycle.



When the timer reaches the compare value, the corresponding Output
Compare Flag (OCFx) in the TIFR register is set.



The Timer can be configured to clear the count register to “0” on a
compare match.

Copyright © 2012 Embedded Systems
Committee
Input Capture

The AVR has an input pin to trigger the input capture event.

A signal change at this pin causes the timer value to be read and saved in
the Input Capture Register (ICRx).

 the same time the Input Capture Flag (ICFx) in the TIFR will be set.
At

This is useful to measure the width of external pulses.

Copyright © 2012 Embedded Systems
Committee
Timer / Counter0


Timer0 is an 8 bit timer/counter which can count from 0 to 0xFF.



In the timer mode this peripheral uses an internal clock signal.



in the counter mode an external signal on PORTB.0.



the timer can be operated either in the polling mode or in the interrupt
mode.

Copyright © 2012 Embedded Systems
Committee
Timer / Counter0 „cont
Used registers:

• Timer registers:


TCCR0 (Timer/Counter 0 Control Register)



TCNT0 (Timer/Counter 0 Value)

• Interrupt registers:


TIFR (Timer Interrupt Flag Register).



TIMSK (Timer Interrupt Mask Register)



SREG (I bit - General Interrupt Enable)

Copyright © 2012 Embedded Systems
Committee
Timer / Counter0 „cont
TCCR0 (Timer/Counter 0 Control Register)

 Define module ON/OFF
 Define module T/C
 Prescaler

Copyright © 2012 Embedded Systems
Committee
Timer / Counter0 „cont

Copyright © 2012 Embedded Systems
Committee
Timer / Counter0 „cont
TCNT0 (Timer / Counter 0 Register):
The initial value of the counter is set by writing it

.

Output Compare Register –OCR0

Copyright © 2012 Embedded Systems
Committee
Timer / Counter0 „cont
TIMSK (Timer / Counter Interrupt Mask)

TOIE0: Timer Overflow Interrupt Enable
OCIE0: Output Compare Match Interrupt Enable

Copyright © 2012 Embedded Systems
Committee
Timer / Counter0 „cont
TIFR (Timer / Counter Interrupt Flag Register)

TOV0: Timer/Counter0 Overflow Flag
OCF0: Output Compare Flag 0

Copyright © 2012 Embedded Systems
Committee
Timer / Counter0 „cont
Initializations example :
// Timer/Counter 0 initialization with clock = oscillator / 1024
// Timer initial value is 0xf0;
TCCR0=0x05;
TCNT0=0xf0;
// Timer/Counter 0 initialization
// Clock source: T0 pin Rising Edge
TCCR0=0x07;

Copyright © 2012 Embedded Systems
Committee
Timer / Counter0 „cont
TCCR0 (Timer/Counter 0 Control Register)

Wave Generation Mode

Copyright © 2012 Embedded Systems
Committee
Timer / Counter0 „cont

Copyright © 2012 Embedded Systems
Committee
Timer / Counter0 „cont
Clear Timer on Compare Match (CTC) Mode (WGM01:0 = 2):

the OCR0 Register is used to manipulate the counter resolution.
In CTC mode the counter is cleared to zero when the counter value (TCNT0)
matches the OCR0.

Note

To be able to generate an output from the PWM, the Output Compare Pin
of Timer (OCn) is set up as output.
Copyright © 2012 Embedded Systems
Committee
Timer / Counter0 „cont

Note
that the Data Direction Register (DDR) bit corresponding to the OC0 pin
must be set in order to enable the output driver.

Copyright © 2012 Embedded Systems
Committee
PWM


The PWM is used to generate a pulse train with varying duty-cycle.



An analog filter can be used to shape the digital PWM output to obtain
an analog signal such as a sine wave.



In the AVR, the timer/counters are used to generate PWM signals.



To change the PWM base frequency, the timer clock frequency and top
counter value is changed. Faster clock and/or lower top value will
increase the PWM base frequency, or timer overflow frequency.

Copyright © 2012 Embedded Systems
Committee
Fast PWM Mode


The fast Pulse Width Modulation or fast PWM mode (WGM01:0 = 3)
provides a high frequency.



PWM waveform generation option.



The fast PWM differs from the other PWM option by its single-slope
operation.



The counter counts from BOTTOM to MAX then restarts from
BOTTOM.

Copyright © 2012 Embedded Systems
Committee
TIMER0 for PWM generation


8bit counter counting from 0-255 and the goes to 0 and so on.



We can store any value between 0-255 in OCR0,



say we store 64 in OCR0 then it would appear in the graph as follows
(the RED line).

Copyright © 2012 Embedded Systems
Committee
TIMER0 for PWM generation


When the TIMER0 is configured for fast PWM mode,



while up counting whenever the value of TIMER0 counter (TCNT0 register) matches
OCR0 register an output PIN is pulled low (0)



when counting sequence begin again from 0 it is SET again (pulled high=VCC).



This PIN is named OC0 and you can find it in the PIN configuration

Copyright © 2012 Embedded Systems
Committee

.
TIMER0 for PWM generation

Copyright © 2012 Embedded Systems
Committee
TIMER0 for PWM generation


In fast PWM mode, the compare unit allows generation of PWM waveforms on the OC0 pin.



Setting the COM01:0 bits to 2 will produce a non-inverted PWM .



Inverted PWM output can be generated by setting the COM01:0 to 3.

Copyright © 2012 Embedded Systems
Committee
Phase Correct PWM mode


The phase correct PWM mode (WGM01:0 = 1) provides a high
resolution phase correct PWM



waveform generation option.



The phase correct PWM mode is based on a dual slope operation.



The counter counts repeatedly from BOTTOM to MAX and then from
MAX to BOTTOM.

Copyright © 2012 Embedded Systems
Committee
Phase Correct PWM mode


The dual-slope operation has lower maximum operation frequency than
single slope operation.



However, due to the symmetric feature of the dual-slope PWM modes,
these modes are preferred for motor control applications

Copyright © 2012 Embedded Systems
Committee

.
Phase Correct PWM mode


The PWM resolution for the phase correct PWM mode is fixed to eight
bits.



In phase correct PWM mode the counter is incremented until the
counter value matches MAX. When the counter reaches MAX, it
changes the count direction.



The TCNT0 value will be equal to MAX for one timer clock cycle

Copyright © 2012 Embedded Systems
Committee
Phase Correct PWM mode

Note

To be able to generate an output from the PWM, the Output Compare Pin
of Timer (OCn) is set up as output.
Copyright © 2012 Embedded Systems
Committee
Phase Correct PWM mode
Initializations example :
// Timer/Counter 0 initialization
// Clock source: System Clock / 1024
// Mode: Non-Inverted Fast PWM
TCCR0=0x6D;
TCNT0=0x00;
OCR0=0x00;
TIMSK=0x02;
OCR0 = x; // x is a variable which change the duty cycle.

Copyright © 2012 Embedded Systems
Committee
References
Atmega16 datasheet
http://www.avrfreaks.net/

Copyright © 2012 Embedded Systems
Committee
info@escommittee.net

Copyright © 2012 Embedded Systems
Committee

Mais conteúdo relacionado

Mais procurados

Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051
Rashmi
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware
Prof. Swapnil V. Kaware
 

Mais procurados (20)

Microcontroller 8051
Microcontroller 8051Microcontroller 8051
Microcontroller 8051
 
8051 MICROCONTROLLER ARCHITECTURE.pptx
 8051 MICROCONTROLLER ARCHITECTURE.pptx 8051 MICROCONTROLLER ARCHITECTURE.pptx
8051 MICROCONTROLLER ARCHITECTURE.pptx
 
ARM Micro-controller
ARM Micro-controllerARM Micro-controller
ARM Micro-controller
 
Serial Communication in 8051
Serial Communication in 8051Serial Communication in 8051
Serial Communication in 8051
 
8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware8051 Microcontroller PPT's By Er. Swapnil Kaware
8051 Microcontroller PPT's By Er. Swapnil Kaware
 
8051 Microcontroller ppt
8051 Microcontroller ppt8051 Microcontroller ppt
8051 Microcontroller ppt
 
Serial Peripheral Interface(SPI)
Serial Peripheral Interface(SPI)Serial Peripheral Interface(SPI)
Serial Peripheral Interface(SPI)
 
Unit II Arm 7 Introduction
Unit II Arm 7 IntroductionUnit II Arm 7 Introduction
Unit II Arm 7 Introduction
 
Arm modes
Arm modesArm modes
Arm modes
 
3.programmable interrupt controller 8259
3.programmable interrupt controller 82593.programmable interrupt controller 8259
3.programmable interrupt controller 8259
 
ARM CORTEX M3 PPT
ARM CORTEX M3 PPTARM CORTEX M3 PPT
ARM CORTEX M3 PPT
 
MICROCONTROLLER - INTEL 8051
MICROCONTROLLER - INTEL 8051MICROCONTROLLER - INTEL 8051
MICROCONTROLLER - INTEL 8051
 
Pic microcontroller architecture
Pic microcontroller architecturePic microcontroller architecture
Pic microcontroller architecture
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 
Arm assembly language programming
Arm assembly language programmingArm assembly language programming
Arm assembly language programming
 
PIC Microcontrollers
PIC MicrocontrollersPIC Microcontrollers
PIC Microcontrollers
 
Introduction to pic microcontroller
Introduction to pic microcontrollerIntroduction to pic microcontroller
Introduction to pic microcontroller
 
Arm architecture chapter2_steve_furber
Arm architecture chapter2_steve_furberArm architecture chapter2_steve_furber
Arm architecture chapter2_steve_furber
 
PIC 16F877A by PARTHIBAN. S.
PIC 16F877A   by PARTHIBAN. S.PIC 16F877A   by PARTHIBAN. S.
PIC 16F877A by PARTHIBAN. S.
 
SPI Bus Protocol
SPI Bus ProtocolSPI Bus Protocol
SPI Bus Protocol
 

Destaque

แบบโครงร่างโครงงานคอมพิวเตอร์
แบบโครงร่างโครงงานคอมพิวเตอร์แบบโครงร่างโครงงานคอมพิวเตอร์
แบบโครงร่างโครงงานคอมพิวเตอร์
Lynnie1177
 
แบบโครงร่างโครงงานคอมพิวเตอร์
แบบโครงร่างโครงงานคอมพิวเตอร์แบบโครงร่างโครงงานคอมพิวเตอร์
แบบโครงร่างโครงงานคอมพิวเตอร์
Lynnie1177
 
ประวัติส่วนตัว
ประวัติส่วนตัวประวัติส่วนตัว
ประวัติส่วนตัว
Lynnie1177
 
อังกฤษ
อังกฤษอังกฤษ
อังกฤษ
Lynnie1177
 

Destaque (20)

Lecture 2 timers, pwm, state machine IN PIC
Lecture 2   timers, pwm, state machine IN PIC Lecture 2   timers, pwm, state machine IN PIC
Lecture 2 timers, pwm, state machine IN PIC
 
Timer & Interrupt Atmega16
Timer & Interrupt Atmega16Timer & Interrupt Atmega16
Timer & Interrupt Atmega16
 
Timers and pwm
Timers and pwmTimers and pwm
Timers and pwm
 
Hindavi Technologies Profile
Hindavi Technologies ProfileHindavi Technologies Profile
Hindavi Technologies Profile
 
Mô tả dự án
Mô tả dự ánMô tả dự án
Mô tả dự án
 
Project Report
Project ReportProject Report
Project Report
 
Mo ta du an
Mo ta du anMo ta du an
Mo ta du an
 
Unlocking funding opportunities final
Unlocking funding opportunities finalUnlocking funding opportunities final
Unlocking funding opportunities final
 
Pr i ncess!!!
Pr i ncess!!!Pr i ncess!!!
Pr i ncess!!!
 
Montras com livros 2015
Montras com livros 2015Montras com livros 2015
Montras com livros 2015
 
Mô tả dự án
Mô tả dự ánMô tả dự án
Mô tả dự án
 
Pdhpe slideshow
Pdhpe slideshowPdhpe slideshow
Pdhpe slideshow
 
แบบโครงร่างโครงงานคอมพิวเตอร์
แบบโครงร่างโครงงานคอมพิวเตอร์แบบโครงร่างโครงงานคอมพิวเตอร์
แบบโครงร่างโครงงานคอมพิวเตอร์
 
Atlas corporate profile
Atlas corporate profileAtlas corporate profile
Atlas corporate profile
 
Regulamento concursoleituraconcelho ourem_aeco
Regulamento concursoleituraconcelho ourem_aecoRegulamento concursoleituraconcelho ourem_aeco
Regulamento concursoleituraconcelho ourem_aeco
 
แบบโครงร่างโครงงานคอมพิวเตอร์
แบบโครงร่างโครงงานคอมพิวเตอร์แบบโครงร่างโครงงานคอมพิวเตอร์
แบบโครงร่างโครงงานคอมพิวเตอร์
 
Bigalytics
BigalyticsBigalytics
Bigalytics
 
ประวัติส่วนตัว
ประวัติส่วนตัวประวัติส่วนตัว
ประวัติส่วนตัว
 
Montras com livros
Montras com livrosMontras com livros
Montras com livros
 
อังกฤษ
อังกฤษอังกฤษ
อังกฤษ
 

Semelhante a Timers

Output compare
Output compareOutput compare
Output compare
vusonbkhn
 

Semelhante a Timers (20)

timer counter (1).pptx
timer counter (1).pptxtimer counter (1).pptx
timer counter (1).pptx
 
Timers in Arduino
Timers in ArduinoTimers in Arduino
Timers in Arduino
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
 
AVR_Course_Day7 timers counters and interrupt programming
AVR_Course_Day7 timers counters and  interrupt programmingAVR_Course_Day7 timers counters and  interrupt programming
AVR_Course_Day7 timers counters and interrupt programming
 
AVRTIMER.pptx
AVRTIMER.pptxAVRTIMER.pptx
AVRTIMER.pptx
 
What isn’t told about timers in stm32 application
What isn’t told about timers in stm32 applicationWhat isn’t told about timers in stm32 application
What isn’t told about timers in stm32 application
 
8051 training an interactive tutorial
8051 training an interactive tutorial8051 training an interactive tutorial
8051 training an interactive tutorial
 
8051 Timers
8051 Timers8051 Timers
8051 Timers
 
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
Programmable Logic Controller | Ladder Logic diagrams| Block diagram | I/O Mo...
 
Timer 0 concept
Timer 0 conceptTimer 0 concept
Timer 0 concept
 
Timer And Counter in 8051 Microcontroller
Timer And Counter in 8051 MicrocontrollerTimer And Counter in 8051 Microcontroller
Timer And Counter in 8051 Microcontroller
 
unit 2 lect 6 AND 7 8254.pptx
unit 2 lect 6 AND 7 8254.pptxunit 2 lect 6 AND 7 8254.pptx
unit 2 lect 6 AND 7 8254.pptx
 
Output compare
Output compareOutput compare
Output compare
 
L15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 pL15 timers-counters-in-atmega328 p
L15 timers-counters-in-atmega328 p
 
Peripheral 8245,16550&8237 dma controller
Peripheral 8245,16550&8237 dma controllerPeripheral 8245,16550&8237 dma controller
Peripheral 8245,16550&8237 dma controller
 
Timers
TimersTimers
Timers
 
Best-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbaiBest-embedded-corporate-training-in-mumbai
Best-embedded-corporate-training-in-mumbai
 
Aircraft master warning system with ftc prompting
Aircraft master warning system with ftc promptingAircraft master warning system with ftc prompting
Aircraft master warning system with ftc prompting
 
Advanced Embedded Automatic Car Parking System
	Advanced Embedded Automatic Car Parking System	Advanced Embedded Automatic Car Parking System
Advanced Embedded Automatic Car Parking System
 
8254.pptx
8254.pptx8254.pptx
8254.pptx
 

Mais de محمدعبد الحى (16)

Iso26262 component reuse_webinar
Iso26262 component reuse_webinarIso26262 component reuse_webinar
Iso26262 component reuse_webinar
 
Interfacing using ِAtmega16/32
Interfacing using ِAtmega16/32 Interfacing using ِAtmega16/32
Interfacing using ِAtmega16/32
 
Can bus
Can busCan bus
Can bus
 
Lin bus
Lin busLin bus
Lin bus
 
Embedded Systems in Automotive
Embedded Systems in Automotive Embedded Systems in Automotive
Embedded Systems in Automotive
 
MATLAB Programming
MATLAB Programming MATLAB Programming
MATLAB Programming
 
CPU Architecture
CPU ArchitectureCPU Architecture
CPU Architecture
 
8 bit microcontroller
8 bit microcontroller8 bit microcontroller
8 bit microcontroller
 
Matlab workshop
Matlab workshopMatlab workshop
Matlab workshop
 
Interrupts
InterruptsInterrupts
Interrupts
 
Uart
UartUart
Uart
 
Sw testing
Sw testingSw testing
Sw testing
 
Rtos
RtosRtos
Rtos
 
Dio
DioDio
Dio
 
Micro controller
Micro controllerMicro controller
Micro controller
 
Day1
Day1Day1
Day1
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 

Timers

  • 1. Timer Module Eman Aboelatta Copyright © 2012 Embedded Systems Committee
  • 2. Atmega16Timer Modules: ONE TWO 8-bit Timer/Counters with Separate Prescalers and Compare Modes 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture Mode Copyright © 2012 Embedded Systems Committee
  • 3. Atmega32Timer Modules: The timer of the AVR can be monitor three Events: Timer Overflow. Compare Match. Input Capture(in timer1) Also, Timer use in PWM Generation. Copyright © 2012 Embedded Systems Committee
  • 4. Timer Overflow  timer overflow means that the counter has counted up to its maximum A value and is reset to zero in the next timer clock cycle. The resolution of the timer determines the maximum value of that timer.  The timer overflow event causes the Timer Overflow Flag (TOVx) to be set in the Timer Interrupt Flag Register (TIFR).  There are two timers with 8-bit resolution and one timer with 16-bit resolution on the ATmega16 Copyright © 2012 Embedded Systems Committee
  • 5. Compare Match   In cases where it is not sufficient to monitor a timer overflow. the compare match interrupt can be used.  The Output Compare Register (OCRx) can be loaded with a value [0.. MaxVal] which the timer will be checked against every timer cycle.  When the timer reaches the compare value, the corresponding Output Compare Flag (OCFx) in the TIFR register is set.  The Timer can be configured to clear the count register to “0” on a compare match. Copyright © 2012 Embedded Systems Committee
  • 6. Input Capture  The AVR has an input pin to trigger the input capture event. A signal change at this pin causes the timer value to be read and saved in the Input Capture Register (ICRx).  the same time the Input Capture Flag (ICFx) in the TIFR will be set. At  This is useful to measure the width of external pulses. Copyright © 2012 Embedded Systems Committee
  • 7. Timer / Counter0  Timer0 is an 8 bit timer/counter which can count from 0 to 0xFF.  In the timer mode this peripheral uses an internal clock signal.  in the counter mode an external signal on PORTB.0.  the timer can be operated either in the polling mode or in the interrupt mode. Copyright © 2012 Embedded Systems Committee
  • 8. Timer / Counter0 „cont Used registers: • Timer registers:  TCCR0 (Timer/Counter 0 Control Register)  TCNT0 (Timer/Counter 0 Value) • Interrupt registers:  TIFR (Timer Interrupt Flag Register).  TIMSK (Timer Interrupt Mask Register)  SREG (I bit - General Interrupt Enable) Copyright © 2012 Embedded Systems Committee
  • 9. Timer / Counter0 „cont TCCR0 (Timer/Counter 0 Control Register)  Define module ON/OFF  Define module T/C  Prescaler Copyright © 2012 Embedded Systems Committee
  • 10. Timer / Counter0 „cont Copyright © 2012 Embedded Systems Committee
  • 11. Timer / Counter0 „cont TCNT0 (Timer / Counter 0 Register): The initial value of the counter is set by writing it . Output Compare Register –OCR0 Copyright © 2012 Embedded Systems Committee
  • 12. Timer / Counter0 „cont TIMSK (Timer / Counter Interrupt Mask) TOIE0: Timer Overflow Interrupt Enable OCIE0: Output Compare Match Interrupt Enable Copyright © 2012 Embedded Systems Committee
  • 13. Timer / Counter0 „cont TIFR (Timer / Counter Interrupt Flag Register) TOV0: Timer/Counter0 Overflow Flag OCF0: Output Compare Flag 0 Copyright © 2012 Embedded Systems Committee
  • 14. Timer / Counter0 „cont Initializations example : // Timer/Counter 0 initialization with clock = oscillator / 1024 // Timer initial value is 0xf0; TCCR0=0x05; TCNT0=0xf0; // Timer/Counter 0 initialization // Clock source: T0 pin Rising Edge TCCR0=0x07; Copyright © 2012 Embedded Systems Committee
  • 15. Timer / Counter0 „cont TCCR0 (Timer/Counter 0 Control Register) Wave Generation Mode Copyright © 2012 Embedded Systems Committee
  • 16. Timer / Counter0 „cont Copyright © 2012 Embedded Systems Committee
  • 17. Timer / Counter0 „cont Clear Timer on Compare Match (CTC) Mode (WGM01:0 = 2): the OCR0 Register is used to manipulate the counter resolution. In CTC mode the counter is cleared to zero when the counter value (TCNT0) matches the OCR0. Note To be able to generate an output from the PWM, the Output Compare Pin of Timer (OCn) is set up as output. Copyright © 2012 Embedded Systems Committee
  • 18. Timer / Counter0 „cont Note that the Data Direction Register (DDR) bit corresponding to the OC0 pin must be set in order to enable the output driver. Copyright © 2012 Embedded Systems Committee
  • 19. PWM  The PWM is used to generate a pulse train with varying duty-cycle.  An analog filter can be used to shape the digital PWM output to obtain an analog signal such as a sine wave.  In the AVR, the timer/counters are used to generate PWM signals.  To change the PWM base frequency, the timer clock frequency and top counter value is changed. Faster clock and/or lower top value will increase the PWM base frequency, or timer overflow frequency. Copyright © 2012 Embedded Systems Committee
  • 20. Fast PWM Mode  The fast Pulse Width Modulation or fast PWM mode (WGM01:0 = 3) provides a high frequency.  PWM waveform generation option.  The fast PWM differs from the other PWM option by its single-slope operation.  The counter counts from BOTTOM to MAX then restarts from BOTTOM. Copyright © 2012 Embedded Systems Committee
  • 21. TIMER0 for PWM generation  8bit counter counting from 0-255 and the goes to 0 and so on.  We can store any value between 0-255 in OCR0,  say we store 64 in OCR0 then it would appear in the graph as follows (the RED line). Copyright © 2012 Embedded Systems Committee
  • 22. TIMER0 for PWM generation  When the TIMER0 is configured for fast PWM mode,  while up counting whenever the value of TIMER0 counter (TCNT0 register) matches OCR0 register an output PIN is pulled low (0)  when counting sequence begin again from 0 it is SET again (pulled high=VCC).  This PIN is named OC0 and you can find it in the PIN configuration Copyright © 2012 Embedded Systems Committee .
  • 23. TIMER0 for PWM generation Copyright © 2012 Embedded Systems Committee
  • 24. TIMER0 for PWM generation  In fast PWM mode, the compare unit allows generation of PWM waveforms on the OC0 pin.  Setting the COM01:0 bits to 2 will produce a non-inverted PWM .  Inverted PWM output can be generated by setting the COM01:0 to 3. Copyright © 2012 Embedded Systems Committee
  • 25. Phase Correct PWM mode  The phase correct PWM mode (WGM01:0 = 1) provides a high resolution phase correct PWM  waveform generation option.  The phase correct PWM mode is based on a dual slope operation.  The counter counts repeatedly from BOTTOM to MAX and then from MAX to BOTTOM. Copyright © 2012 Embedded Systems Committee
  • 26. Phase Correct PWM mode  The dual-slope operation has lower maximum operation frequency than single slope operation.  However, due to the symmetric feature of the dual-slope PWM modes, these modes are preferred for motor control applications Copyright © 2012 Embedded Systems Committee .
  • 27. Phase Correct PWM mode  The PWM resolution for the phase correct PWM mode is fixed to eight bits.  In phase correct PWM mode the counter is incremented until the counter value matches MAX. When the counter reaches MAX, it changes the count direction.  The TCNT0 value will be equal to MAX for one timer clock cycle Copyright © 2012 Embedded Systems Committee
  • 28. Phase Correct PWM mode Note To be able to generate an output from the PWM, the Output Compare Pin of Timer (OCn) is set up as output. Copyright © 2012 Embedded Systems Committee
  • 29. Phase Correct PWM mode Initializations example : // Timer/Counter 0 initialization // Clock source: System Clock / 1024 // Mode: Non-Inverted Fast PWM TCCR0=0x6D; TCNT0=0x00; OCR0=0x00; TIMSK=0x02; OCR0 = x; // x is a variable which change the duty cycle. Copyright © 2012 Embedded Systems Committee
  • 31. info@escommittee.net Copyright © 2012 Embedded Systems Committee