Pages

Monday, August 30, 2021

ATMega16 Timer/Counter0 Compare Output Mode

 

Introduction

Compare Output Mode of Timer/Counter0 is also calls waveform generation. It’s different from normal mode, CTC, and PWM mode.

 

ATMega16 Timer/Counter0 Compare Output Mode

We can use this mode to create a waveform in a form of toggling , set, and clear OC0 pin on compare match. However it’s just like a normal timer mode but it’s additionally able to trigger an output at OC0 pin. Toggling OC0 at compare match is just a square wave output with a programmable frequency.

Programming

Waveform generation mode is selected by Compare Output Mode bit for non-PWM mode of the Timer/Counter Control Register (TCCR0).

ATMega16 Timer/Counter0 Compare Output Mode
Compare Output Mode bit of TCCR0

Within this example I program the ATMega16 to toggle OC0 on compare match. Crystal frequency of microcontroller is 16MHz with its clock select bit of 1:1024. Hence the toggling output occurs for every,

ATMega16 Timer/Counter0 Compare Output Mode
Equation to find the toggling time of waveform generation



 We can find the frequency of square wave output as,

ATMega16 Timer/Counter0 Compare Output Mode
Equation to find waveform generation frequency

 Finally the microcontroller program coded to toggle OC0 output with a frequency of 30.52Hz.

  1. /*
  2.  * timerCounter0_CompareOutputMode.c
  3.  *
  4.  * Created: 12/14/2020 10:36:58 PM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. int main(void)
  11. {
  12. /*CTC Mode, Toggle OC0, 1:1024 Prescaler*/
  13. TCCR0=(1<<COM00)|(1<<CS02)|(1<<CS00);
  14. /*OC0 Output*/
  15. DDRB=(1<<3);
  16. while (1)
  17. {
  18. }
  19. }

Let see the hardware simulation in Proteus.

ATMega16 Timer/Counter0 Compare Output Mode
Proteus simulation for waveform generation

Click here to download zip file for this working example.

No comments:

Post a Comment