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

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)