In: Electrical Engineering
Implement on Tinkercad AVR Atmega 32 Timer 0 Prescaler 1024 Delay 10ms
You need to implement the following functions:
need show calculations pulse count = Required Delay/Clock Period
void delay_one_ms(): This function uses Timer0 to implement a one millisecond delay. Follow the steps below to get this function working properly:
#include
void delay_one_ms() {
//use timer0 to implement 1 ms delay
}
void delay_generic(unsigned long ms)
{
//do something here with delay ms
for (int i = 0; i delay_one_ms();
}
}
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
//delay_generic(1000);
digitalWrite(13, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
Timer is a particular type of clock used to evaluate time intervals. A timer that counts from zero upwards to measure the time remaining is often called a stopwatch. It is a device that counts down from a specified time interval and is used to obtain time delay.
In AVR ATmega 32 there are three timer modules.
TIMER 0 - 8 bit
TIMER 1 - 16 bit
TIMER 2 - 16 bit
Registers associated with timers in ATmega 32
Timer/Counter Register - TCNTn - This is the register which holds the value of the timer or counter. When system is reset it will be cleared. It counts up with each clock pulse. All three timers consists of seperate timer/counter register.
Timer overflow flag - TOVn - This flag will be set when the timer overflows. Each timer has corresponding overflow flag.
Timer Counter Control Register - TCCRn - This register is used for the configuration of timers and counters. To select different modes of operation.
Output Compare Register - OCRn- The value of this register is compared to the content of the TCNTn register. When they are identical, the OCFn flag will be set.
TIMER 0
Timer /Counter Register 0 - TCNT0 - This is the 8-bit timer register for TIMER 0 module. According to the required delay the content to be loaded to this register varies. The value in this register increments with the clock pulse.
Timer/Counter Control Register 0 - TCCR0 - This 8-bit register is used to control the timer 0.
Bit 0:2 - CS00:02 - Clock Source Select - These bits are used to choose the clock source. If CS02: CS00 = 000, the timer is disabled. When it gets the value from 001 to 101, it gets the clock source and begins as the timer.
CS02 | CS01 | CS00 | |
0 | 0 | 0 | No clock source |
0 | 0 | 1 | clock (no pre-scaling) |
0 | 1 | 0 | clock/8 |
0 | 1 | 1 | clock/64 |
1 | 0 | 0 | clock/256 |
1 | 0 | 1 | clock/1024 |
1 | 1 | 0 | External clock source on T0 pin. Clock on falling edge |
1 | 1 | 1 | External clock source on T0 pin. Clock on rising edge |
Bit 4:5 - COM00:COM01 -Compare Output ModE - These bits control the waveform generator. Used in compare mode of the timer.
Bit 3,6- WGM01, WGM00 - Waveform Generation Mode -
WGM00 | WGM01 | TMER0 Mode |
0 | 0 | Normal |
0 | 1 | CTC (Clear timer on Compare Match) |
1 | 0 | PWM, Phase correct |
1 | 1 | Fast PWM |
Bit 7 - FC0 - Force compare match - Write a bit that can be used when producing a shock. Adding 1 to this bit allows the wave generator to behave as though a match has happened.
Timer Counter Interrupt Flag Register - TIFR - This register monitors the overflow flag.
Bit 7 - OCF2 - Timer2 Output Compare match flag
Bit 6 - TOV2 - Timer2 Overflow flag
Bit 5 - Input Capture flag
Bit 4 - OCF1A - Timer1 Output Compare A match flag
Bit 3 - OCF1B - Timer1 Output Compare B match flag
Bit 2 - TOV1 - Timer1 Overflow flag
Bit 1 - OCF0 - Timer0 Output Compare match flag. If it is 0 then compare match did not occur and if it is '1' then compare match occurred .
Bit 0 - TOV0 - Timer0 Overflow flag. If it is '1' then Timer0 has overflown and if it is '0', then Timer0 is did not overflow.
So to create a delay of 10 ms with 1024 prescaler. First we need to load the TCNT0 register with the value that will generate 10 ms delay.
The frequency of oscillation of ATmega 32 is 8 MHz.
Therefore,
And prescaler is 1024.
So, the timer clock source frequency will be
So time period of cycle will be,
Therefore, for a delay of 10 ms, number of cycles required will be,
So 78 timer cycles required to generate a delay of 10 ms.
So the value to be loaded to TCNT0 = (256 -78 ) = 178 which is 0xB2 in hexadecimal.
Step 1 - Initially load TCNT0 with 0xB2.
Step 2- Since the timer operates in normal mode the WGM00:01 bits of TCCR0 must be cleared and since prescalar is 1024 the clock source select bits CS02 and CS00 must be set. So the value of TCCR0 will be 0x05.
Step 3 - Monitor TOV0 flag whether it is set or not.
Step 4 - If TOV0 is set then load TCCR0 by 0 then the clock source will be disconnected. So timer stops.
Step 5 - Clear the TOV0 flag by writing 1 to the TOV0 .
So this will generate a delay of 10 ms.
#include<avr/io.h>
int i,j
void setup()
{
pinMode(13, OUTPUT);
}
void loop()
{
digitalWrite(13, HIGH);
delay_generic(100); // Will generate 100 * 10 ms delay
digitalWrite(13, LOW);
delay_generic(100); // Will generate 100 * 10 ms delay
}
void delay_10_ms()
{
TCCR0 = 0x05; // Timer0, normal mode, /1024 prescalar
TCNT0 = 0xB2; // Load TCNT0, count for 10 ms
while((TIFR&0x01)==0); // Wait for TOV0 to roll over
TCCR0 = 0;
TIFR = 0x1; // Clear TOV0 flag
}
void delay_generic(i)
{
for(j=1;j<=i;j++)
{
delay_10_ms()
}
}