Monday, August 30, 2021

ATMega16 Timer/Counter0 CTC Mode


Introduction

Another feature of ATMega16 AVR microcontroller Timer/Counter0 peripheral is its Clear Timer on Compare Match (CTC) Mode. In this case the controller can only generate a square wave output on its OC0 output pin.

Simulation Sample

Its free running Timer/Counter0 Register (TCNT0) starts count until it reaches the value of the Output Compare Register (OCR0). At comparison matching the TOP, the TOV0 flag is set. Output logic change at OC0 pin is selected using software – set, clear, toggle, and disconnect.

ATMega16 Timer/Counter0 CTC Mode
Timing Diagram for CTC mode of Timer/Counter0

Assigning WGM01 of Waveform Generation Mode Bit of TCCR0 register to 1, put Timer/Counter0 to operate in Clear Timer on Compare Match (CTC) mode.


ATMega16 Timer/Counter0 CTC Mode

CTC is Mode 2 by setting WGM01 of TCCR0


 To generate square wave output at OC0, the software must set the Output Compare Mode bit in non-PWM mode to toggle OC0 pin at compare match.

ATMega16 Timer/Counter0 CTC Mode
Output Compare Mode bit in non-PWM mode

Output Compare Register (OCR0) is a reference value for TCNT0. It’s changeable during run-time to change the output square wave’s frequency. Device’s datasheet states the formula of finding the frequency of square wave output as below,

ATMega16 Timer/Counter0 CTC Mode
N is prescaler factor 1, 8, 64, 256, or 1024

 

When the OCR0 register is set to 0 the maximum frequency of output waveform is haft of microcontroller clock.

ATMega16 Coding Using AVR GCC

Embedded controller’s program generates an output waveform at OC0 pin as square wave with a determined frequency. Software setting puts Timer/Counter0 module of this AVR device to work in CTC mode as we have described above.


We select CTC mode with its clock prescaler of 256, and OCR0 = 50. Then the output frequency of waveform at OC0 pin is,

ATMega16 Timer/Counter0 CTC Mode
Calculation for output frequency

Without sophisticated laboratory equipment – oscilloscope, I test this microcontroller program in Proteus simulator.

ATMega16 Timer/Counter0 CTC Mode
Simulation in Proteus – The output frequency is 613Hz in software

 AVR GCC coding is done in Atmel Studio 7 as a preferred IDE.

  1. /*
  2.  * m16_timer_0_ctc.c
  3.  *
  4.  * Created: 12/15/2020 11:00:35 AM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10.  
  11. int main(void)
  12. {
  13. /*CTC Mode, With 1:256 Prescaler*/
  14. TCCR0=(1<<WGM01)|(1<<COM00)|(1<<CS02);
  15. /*Set a preferred value of OCR0*/
  16. OCR0=50;
  17. /*OC0/PB3 Square Wave Output*/
  18. DDRB=(1<<3);
  19. /*Main Program Loop Stays Here*/
  20. while (1)
  21. {
  22. }
  23. }

Click here to download zip file of this AVR tutorial.

A Program to Create a 50Hz Frequency

Since Output Compare Register (OCR0) used for adjusting the frequency of output waveform in this mode, we can find its actual value to create a preferred frequency.

To get a lower 50Hz frequency we need to scale timer input to 1:1024 ( N=1024). With an equation stated by device’s manufacturer, we can find the value of OCR0 as follow,

ATMega16 Timer/Counter0 CTC Mode
Equation to find a 50Hz frequency

So in software embedded C we must set CS02=1 and CS00=1, and OCR0=155.

  1. /*
  2.  * m16_ctc_50Hz.c
  3.  *
  4.  * Created: 12/15/2020 5:48:40 PM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10.  
  11. int main(void)
  12. {
  13. /*CTC Mode, With 1:1024 Prescaler*/
  14. TCCR0=(1<<WGM01)|(1<<COM00)|(1<<CS02)|(1<<CS00);
  15. /*Set a preferred value of OCR0 to get
  16.   a 50Hz waveform*/
  17. OCR0=155;
  18. /*OC0/PB3 Square Wave Output*/
  19. DDRB=(1<<3);
  20. /*Main Program Loop Stays Here*/
  21. while (1)
  22. {
  23. }
  24. }

This embedded program is simulated in Proteus simulator. 

ATMega16 Timer/Counter0 CTC Mode
Simulation result in Proteus

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)