Saturday, August 21, 2021

Introduction To ATMega16 Timer 0

 

Introduction

A timer of a microcontroller usually utilized to create a precise timing delay, a square wave output, a reference time to measure any received external pulse, etc. For instance, decoding the remote control signal requires a precise internal timer of a microcontroller to measure the time of true and false logic.

Introduction To ATMega16 Timer 0
Simulation of Timer 0

As a microcontroller inside’s, a timer formed by one or two timer registers depend on the architectural design. For example, an 8-bit microcontroller with a 8-bit timer has an 8-bit timer register. A 16-bit timer of an 8-bit microcontroller composes of two 8-bit timer registers. Timer register automatically increase its content as the programmer properly configured it. It’s a read/write register. There are some essential registers relate to timer operation, a register to configure the incremental rate, a register to configure and check timer interrupt event.



Timer 0 Of ATMega16

ATmega16 microcontroller comes with a number of timers. Here we will briefly discuss about timer0 and its utilization. Literately, it’s referred as Timer/Counter0 as it could operate in timer or counter mode. For a simple explanation we only list it in timer mode.

A Brief Of Timer/Counter0

Timer/Counter0 is 8-bit timer or counter register. It can generate a PWM output to external analog device via pin OC0 as the programmer configured in codes.

A block diagram lists below take from vendor device specification. It’s a general diagram of the 8-bit Timer/Counter of its ATMegaR microcontroller series.

Introduction To ATMega16 Timer 0
Block diagram of 8-bit Timer/Counter0 of ATMega16 Microcontroller

From this block diagram, the external pin OCn creates a PWM output signal. As the case of Timer/Counter0 it’s named as OC0. Tn is an external input for counter mode. Here it’s name as T0, an external pulse that fed to Timer/Counter0 whenever this module works as counter mode.



Relevant Registers

In Special Function Registers (SFR) space there are a number of registers. Those are storage register, control registers and interrupt flag register.

For a instance we will show only some fundamental registers that configure Timer/Counter0 to work in timer mode.

Timer/Counter Register – TCNT0

The program could directly read and write to this 8-bit register while the program is running.

Introduction To ATMega16 Timer 0
Timer/Counter Register – TCNT0

In PWM mode modifying this register could make some problems.

Timer/Counter Control Register – TCCR0



This register largely control the operation of Pulse Width Modulation (PWM) generated by Timer/Counter0. It contains some bits that set the prescaler of PWM, counter and also timer.

Introduction To ATMega16 Timer 0
Atmega16 TCCR0 register

 There are a lot of details for each bit in this register. Some bits in this register relate to timer mode. By default the module work in timer mode but with no clock source which means that the timer doesn’t increase its content.

The Compare Output Mode COM01:00 determine the operation of this module. The table lists below set the configuration of a non-PWM mode operation.

Introduction To ATMega16 Timer 0
The Compare Output Mode COM01:00

As list above COM01:00 equal to “00” which keep this module in timer mode.



Clock Select Bits CS02:0 determine clock source, prescaler and transition for this module. The transition work only in counter mode.

Introduction To ATMega16 Timer 0
TCCR0 clock selection bits of Timer/Counter0 in ATMega16

Timer/Counter Interrupt Flag Register – TIFR

Whenever the comparison of Timer/Counter match the the value of OCR0 register the Output Compare Flag 0 – OCF0 is set. However we will not talk about this here because the article is only about timer mode.

Introduction To ATMega16 Timer 0
Timer/Counter Interrupt Flag Register – TIFR

 

The Timer/Counter0 Overflow Flag (TOV0) is set whenever the timer register exceeds 0xFF (255 in decimal) and rolls back to 0. Whenever it happens the program set this bit to clear the interrupt flag.



Timer 0 Programming In Atmel Studio 7

Introduction

Using AVR GCC the programming for timer 0 is very short in code, and more understandable. Registers name are just like the labels listed in its device’s datasheet.

Now let program timer 0 of the ATMega16 from scratch. A simple introductory program show how to configure timer 0 in AVR GCC in Atmel Studio 7.

Module Preparation

Timer 0 clock source pulses by the microcontroller crystal clock as it’s pre-soldered on the AVR development board. The crystal oscillator frequency is 16 MHz. It’s clock period is,



1/(16000000 Hz) = 62.5 ns.

With its select-able prescaler I scale this clock source to 1024 time. So clock selection bits of the TCCR0 register must be, CS02=0 and CS00=0.



As a result timer 0 (TCNT0 register) increase its content for every,

1024 x 62.5 ns = 64 us.

This 8-bit register overflows when its content reaches 0xFF, setting the interrupt flag TOV0 in the TIFR register. The duration that the TOV0 flag is set is,

256 x 64 us = 16.3 ms.

Unlike other microcontroller such as PICMicro, to clear the interrupt flag the software must set that interrupt flag. That could be weird to most novice AVR programmer.

AVR Coding



As demonstrated above this embedded programming example toggles an output pin of port B for every 16.3 ms. It’s just like a square wave with a period of,

2 x 16.3 ms = 32.6 ms.

AVR GCC source code lists below.

  1. /*
  2.  * timer_0_demon.c
  3.  *
  4.  * Created: 12/7/2020 4:41:58 PM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. int main(void)
  11. {
  12. DDRB=0x01;
  13. TCCR0=(1<<CS02)|(1<<CS00);
  14. TIFR|=(1<<TOV0);
  15. TCNT0=0;
  16. while(1){
  17. if (TIFR&(1<<TOV0))
  18. {
  19. TIFR|=(1<<TOV0);
  20. PORTB^=0x01;
  21. }
  22. }
  23. }

It’s schematic is drawn and simulated in Proteus because I don’t have a oscilloscope to test the physical hardware at the moment.

Introduction To ATMega16 Timer 0
Schematic Diagram – The ATMega16 clocks from a 16 MHz crystal oscillator.

The output wave form creates by timer 0 interrupt shown on a virtual oscilloscope as it’s simulated in Proteus.

Introduction To ATMega16 Timer 0
The output wave form on PB0 of Port B.

The clock period is 32.75 ms in software simulator. Click here to download zip file of this programming example.



Creating A One Second Delay



As mentioned earlier, the interrupt occurs for every 16.3 ms. The programmer can create more timing delay from this advantage using more optional C variables. For instance we want to create a one second delay. To make this work the programmer create a C variable that its content increase one by one for every 16.3 ms. We can the find the total count in one second by,



oneSecond = 1/0.0163 = 61 counts

AVR GCC program is 90% similar to the program written above but with some additional line of code that create a one second delay.

  1. /*
  2.  * timer_0_blink.c
  3.  *
  4.  * Created: 12/8/2020 9:41:37 PM
  5.  * Author : admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. #define oneSecond 61
  11.  
  12. int main(void)
  13. {
  14. unsigned char cnt=0;
  15. /*PB0 Output*/
  16. DDRB=0x01;
  17. /*Select 1:1024 Pre-Scaler*/
  18. TCCR0=(1<<CS02)|(1<<CS00);
  19. /*Clear Interrupt Flage Of Timer 0*/
  20. TIFR|=(1<<TOV0);
  21. /*Clear Timer/Counter0*/
  22. TCNT0=0;
  23. while(1){
  24. /*Check for interrupt flag*/
  25. if (TIFR&(1<<TOV0))
  26. {
  27. TIFR|=(1<<TOV0);
  28. cnt++;
  29. }
  30. /*One second is here*/
  31. if (cnt>=61)
  32. {
  33. cnt=0;
  34. PORTB^=1;
  35. }
  36. }
  37. }

 

The schematic diagram is no other than the one’s shown above. Click here to download the zip file.

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)