Exp7 Timer and Its Mode (Normal, Counter, CTC, PWM)
Exp7 Timer and Its Mode (Normal, Counter, CTC, PWM)
(ATMEGA32)
Submitted by
Ronit Dutta, MS in IOT and Signal Processing
Department of Electrical Engineering, IIT Kharagpur
Overall, timers improve the efficiency and reliability of embedded systems by handling
time-dependent operations without burdening the processor.
2
Embedded System on AVR ATMEGA32:Exp7
One way to generate a time delay is to clear the counter at the start and wait until it
reaches a specific value. For example, we want to generate a 100us delay. Assume the
microcontroller oscillator frequency is 1 MHz, the counter increases once per
microsecond. So, to generate 100-microsecond delay, we clear the counter and wait
until it reaches 100.
3
Embedded System on AVR ATMEGA32:Exp7
Another method is to use the counter’s overflow flag. Each counter has a flag that
turns ON when the counter overflows and resets to zero. The flag is then cleared by
software. To generate a delay using this method, we load a starting value into the
counter and wait for the overflow flag to turn ON. For instance, in a microcontroller
with a 1 MHz frequency and an 8-bit counter, if we want a 3-microsecond delay, we
set the counter to 0xFD. After three ticks, the counter overflows, resets to 0x00, and
the flag is set.
In AVR microcontrollers, there are one to six timers, named Timer0, Timer1, Timer2,
etc. These timers can be used to generate delays or count external events. Some
timers are 8-bit, while others are 16-bit. In the ATmega32, there are three timers:
● Timer0 (8-bit)
● Timer1 (16-bit)
● Timer2 (8-bit)
These Timers can be used in different ways:
4
Embedded System on AVR ATMEGA32:Exp7
Below table shows the COM01:0 bit functionality when the WGM01:0 bits are set to
a normal or CTC mode (non-PWM).
5
Embedded System on AVR ATMEGA32:Exp7
Below table shows the COM01:0 bit functionality when the WGM01:0 bits are set to
fast PWM mode.
Table: Compare Output Mode, Fast PWM Mode
Below table shows the COM01:0 bit functionality when the WGM01:0 bits are set to
phase correct PWM mode.
Table: Compare Output Mode, Phase Correct PWM Mode
6
Embedded System on AVR ATMEGA32:Exp7
The Timer/Counter Register gives direct access, both for read and write operations, to
the Timer/Counter unit 8-bit counter. Writing to the TCNT0 Register blocks (removes)
the compare match on the following timer clock. Modifying the counter (TCNT0) while
the counter is running, introduces a risk of missing a compare match between TCNT0
and the OCR0 Register.
The Output Compare Register contains an 8-bit value that is continuously compared
with the counter value (TCNT0). A match can be used to generate an output compare
interrupt, or to generate a waveform output on the OC0 pin.
7
Embedded System on AVR ATMEGA32:Exp7
Since, we are using Timer0 as Normal Timer mode so Overflow flag will be checked. To
overflow the register TCNT0 after 244 cycles, the initial value of the TCNT0 will be
TCNT0= 256-244= 12
First we will complete this experiment through Polling Method then we will complete
same experiment with Interrupt Method.
For Polling Method: We will continuously check TOV0 bit in TIFR register.
For Interrupt Method: We set TOIE0 bit in TIMSK and I-bit in SREG. The
corresponding interrupt service routine will be executed.
8
Embedded System on AVR ATMEGA32:Exp7
Experiment 1: Create 250ms delay with Timer0 with polling method. MCU Oscillator
Frequency=1MHz
Calculation is same as above.
/* Normal Mode Timer0 Programming for delay routine through polling method.
Create 250ms delay with Timer0. MCU Oscillator Frequency= 1MHz. */
.INCLUDE "M32DEF.INC"
.ORG 0x0000
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
// Initialization Timer0
LDI R16,0x05 // Normal Mode and timer clock frequency= oscillator frequency/1024
OUT TCCR0,R16
Simulation on SimulIDE:
Verify the calculated frequency with simulated frequency. Make the above circuit with
hardware and verify the frequency with DSO.
9
Embedded System on AVR ATMEGA32:Exp7
Experiment 2: Create 500ms delay with Timer0 polling method. MCU Oscillator
Frequency=8MHz.
We want to use pre-scaler 1024.
Therefore, the value of three clock select bits are CS02=1, CS01=0, CS00=1.
Therefore, for this case the Clock Frequency of Timer0= 8000000/1024=7812.5Hz
Time Period= 1/7812.5= 0.000128sec= 0.128ms. This Time Period is also called Clock
Tick.
To generate 500ms delay, the Number of Timer Cycles= 500/0.128= 3906.25= 3906
cycles.
Since Timer0(8-bit timer) can count maximum upto 255. We have to make 3906 as
multiplication of two integers which are less than equal to 255.
Therefore, we can write 3906= 217*18
We will use Timer0 to count 217 clocks for 18times.
Since, we are using Timer0 as Normal Timer mode so Overflow flag will be checked. To
overflow the register TCNT0 after 244 cycles, the initial value of the TCNT0 will be
TCNT0= 256-217= 39
/* Normal Mode Timer0 Programming for delay routine through polling method.
Create 500ms delay with timer0. MCU Oscillator Frequency=8MHz */
.INCLUDE "M32DEF.INC"
.ORG 0x0000
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
// Initialization Timer0
LDI R16,0x05 // Normal Mode and timer clock= oscillator frequency/1024
OUT TCCR0,R16
LDI R16,18
LOOP: IN R19,TIFR
AND R19,R18
BREQ LOOP
DEC R16
10
Embedded System on AVR ATMEGA32:Exp7
OUT TCNT0,R17
OUT TIFR,R18
BRNE LOOP
RET
Simulation on SimulIDE:
Verify the calculated frequency with simulated frequency. Make the above circuit with
hardware and verify the frequency with DSO.
Experiment 3: Create 500ms delay with Timer0 Interrupt method. MCU Oscillator
Frequency=8MHz.
// Timer0 using Interrupt
.INCLUDE "M32DEF.INC"
.ORG 0X0000
JMP MAIN
.ORG 0x0016
JMP Timer0_Overflow
//Timer0 Initialization
LDI R16,0x01
OUT TIMSK,R16
LDI R16,0x05
OUT TCCR0,R16
LDI R17,39
OUT TCNT0,R17
SEI
LDI R16,18
Infinity_LOOP: NOP
11
Embedded System on AVR ATMEGA32:Exp7
JMP Infinity_LOOP
Simulation on SimulIDE:
Verify the calculated frequency with simulated frequency. Make the above circuit with
hardware and verify the frequency with DSO.
Experiment 4: UART Bit-Banging (Software UART)
Data Frame of UART Communication is
12
Embedded System on AVR ATMEGA32:Exp7
We want to generate this data frame using any GPIO pin of the ATmega32. This method of
generating a UART data frame with GPIO is known as UART bit-banging. Since we are creating
this data frame through programming, it is also referred to as software UART.
Note: To install the required software and check the corresponding hardware details, please
refer to the lab manual Exp9A.
UART Transmitter Bit-Banging:
We want to transfer data at a baud rate of 9600. Therefore, each data bit stays on the GPIO
for 1000000/9600=104.167us= approximately 104 microseconds.
Implement a UART bit-banging transmitter on PORTC0, operating at a baud rate of 9600
with one stop bit and no parity bit. The ATmega32 microcontroller runs at an 8 MHz
oscillator frequency. The transmitter will continuously send the character 'A' in an infinite
loop.
/* UART Bit-Banging Tx through PORTC0 with 9600 Baud. MCU Oscillator= 8MHz*/
.INCLUDE "M32DEF.INC"
.ORG 0x0000
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
// Set Timer0 registers to create delay with respect to 9600 baud rate
LDI R16,0x02
OUT TCCR0,R16
13
Embedded System on AVR ATMEGA32:Exp7
Simulation on SimulIDE:
Make the below hardware to verify the UART transmitted data on Realterm.
14
Embedded System on AVR ATMEGA32:Exp7
.INCLUDE "M32DEF.INC"
.ORG 0x0000
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
//DDDR of PORTA
LDI R16,0xFF
OUT DDRA,R16
// Set Timer0 registers to create delay with respect to 9600 baud rate
LDI R16,0x02
OUT TCCR0,R16
15
Embedded System on AVR ATMEGA32:Exp7
AND R18,R17
BREQ LOOP
RET
Simulation on SimulIDE: Set the print type to BIN on the serial terminal to display the binary
representation of the corresponding entered character. The LED bar graph can be used to
verify this binary sequence.
Make the below hardware to verify the UART received data on LED bar graph.
Choose the correct COM port and set the baud rate with no parity and one stop bit. Navigate
to the Display menu and select 'Half Duplex' to monitor the character entered from the
keyboard. Finally, click 'Open' to start the receiver operation.
16
Embedded System on AVR ATMEGA32:Exp7
operates at an 8 MHz oscillator frequency. The received data will be displayed on an LED
bar graph connected to PORTA.
2. Timer0 as Counter Mode:
Experiment 5: In this experiment, Timer0 is configured in Counter Mode to count external
pulses. The value stored in the TCNT0 register is then visualized on an LED bar graph
connected to PORTA, allowing real-time monitoring of the counter's progress.
/* Timer0 as counter mode.
Display the counter value(TCNT0) on a LED Bar Graph at PORTA. */
.INCLUDE "M32DEF.INC"
.ORG 0x0000
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
//DDDR of PORTA
LDI R16,0xFF
OUT DDRA,R16
LOOP: IN R17,TCNT0
OUT PORTA,R17
JMP LOOP
Simulation on SimulIDE:
17
Embedded System on AVR ATMEGA32:Exp7
Make the below hardware to verify the counter through LED bar graph.
If an external tactile switch on the breadboard is not preferred, the built-in user switch on the
development board can serve as an alternative for input control.
18
Embedded System on AVR ATMEGA32:Exp7
The comparison value OCR0=99. Therefore, to loop between 99 to 99, the 100 clock will be
required.
Therefore, OC0 remains logic high for 100x1us=100us and logic low for 100us.
Hence, the time period of the generated square wave at OC0= 200us.
Therefore, the frequency of the generated square wave= 5KHz.
.INCLUDE "M32DEF.INC"
.ORG 0x0000
LDI R16,HIGH(RAMEND)
OUT SPH,R16
LDI R16,LOW(RAMEND)
OUT SPL,R16
// Set Timer0 registers as CTC mode and Toggle OC0 on compare match
LDI R16,0x1A
OUT TCCR0,R16
// Reset TCNT0 register
LDI R16,0x00
OUT TCNT0,R16
// Load OCR0 for compare match
LDI R16,99
OUT OCR0,R16
LOOP: NOP
JMP LOOP
19
Embedded System on AVR ATMEGA32:Exp7
Simulation on SimulIDE:
20
Embedded System on AVR ATMEGA32:Exp7
Pulse Width Modulated signals with different duty cycle are shown below:
21
Embedded System on AVR ATMEGA32:Exp7
ii. The leading edge is kept constant while the trailing edge is moved.
22
Embedded System on AVR ATMEGA32:Exp7
Fast PWM:
To set Fast PWM mode, we have to set WGM00: 01= 11. To generate a PWM
waveform on the OC0 pin, we need to set COM01:00= 10 or 11.
COM01:00= 10 will generate Noninverting PWM output waveform.
23
Embedded System on AVR ATMEGA32:Exp7
The advantage of using PWM mode in AVR is that it is an inbuilt hardware unit for
waveform generation and once we set the PWM mode and duty cycle, this unit starts
generating PWM and the controller can do other work.
Experiment 7: Write an assembly program for Atmega32 to Generate Fast PWM using
Timer0 and change the duty cycle with two push buttons (Increment & Decrement).
// Interrupt Initialization
SEI
LDI R16,0xC0
OUT GICR,R16
LDI R16,0x0A
OUT MCUCR,R16
24
Embedded System on AVR ATMEGA32:Exp7
Infinite_Loop: NOP
JMP Infinite_Loop
Simulation on SimulIDE:
Make the above circuit in hardware to verify the PWM & frequency with DSO. Connect one
LED to check the change of intensity with PWM.
25
Embedded System on AVR ATMEGA32:Exp7
COM01:00= 11 will generate Inverting PWM (Set OC0 on compare match when up-
counting. Clear OC0 on compare match when down counting.) output waveform.
Class Assignment 4: Write an assembly program for Atmega32 to Generate Phase Correct
PWM using Timer0 and change the duty cycle with two push buttons (Increment &
Decrement).
26