Question

In: Electrical Engineering

Implement on Tinkercad  AVR Atmega 32 Timer 0 Prescaler 1024 Delay 10ms You must be able to...

Implement on Tinkercad  AVR Atmega 32 Timer 0 Prescaler 1024 Delay 10ms

  • You must be able to set an arbitrary delay (in milliseconds) using your own function, mimicking the behavior of delay().
  • You must be able to turn on and off an LED attached to pin 13. Each second you must toggle the state of the LED.

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:

    • Choose a prescaler for the desired delay
    • Disable the I-Flag in the SREG. Alternatively, save a copy of SREG to restore later.
    • Set the proper value in TCNT0.
    • Set Timer0 to Normal Mode.
    • Set the prescaler bits.
    • Wait until the overflow flag TOV0 is set.
    • Stop the clock.
    • Clear the TOV0 flag by setting it.
    • Restore the interrupts by setting the I-Flag in the SREG. Or load SREG from the copy you made before.

#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)
}

Solutions

Expert Solution

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()
}
}

Related Solutions

Provide Atmel AVR assembly language statements to implement the following pseudocodes. You can assume that all...
Provide Atmel AVR assembly language statements to implement the following pseudocodes. You can assume that all the variables A, B, C, D correspond to CPU general purpose registers as follows: A →R0, B→R1, C→R2, D→R3. You can also use additional registers from R4- R31 as needed. You must not use any variant of the multiplication instructions. Your assembly code segments should be properly commented. (a) if D < 0 C = A+B else C = A-B (b) if D <...
Your startup is currently cash-constrained, and you must make a decision about whether to delay paying...
Your startup is currently cash-constrained, and you must make a decision about whether to delay paying one of its suppliers, or take out a loan. You owe the supplier $11000 with terms of 5/10 Net 40, so the supplier will give you a 5% discount if you pay today (when the discount period expires). Alternatively, you can pay the full $11000 in one month when the invoice is due. You are considering three options: Alternative A: Forgo the discount on...
You must show your work on all mathematical questions and I must be able to follow...
You must show your work on all mathematical questions and I must be able to follow your steps. The institutional research office has recently conducted a survey group of consumers. According to demographic data, the research office has given you the following:                         Typical weekly consumer income                    $500                         Price of Frosty Cola                                        $2.50 per case                         Price of Coca-Cola                                         $5.00 per case You want to break this data down for the chief financial officer in two forms:...
You must show your work on all mathematical questions and I must be able to follow...
You must show your work on all mathematical questions and I must be able to follow your steps. Your assistant was called away for a medical emergency while he was working on Frosty Cola’s cost structure for last month. He was not able to finish his analysis; however, you have a meeting with the chief financial officer in fifteen minutes and he is expecting this information in its entirety. Your assistant was able to jot down the following for you...
You must show your work on all mathematical questions and I must be able to follow...
You must show your work on all mathematical questions and I must be able to follow your steps. You are the manager of a local factory that produces plastic bottles for soft drink manufacturers. Your assistant comes to you with exciting news about a new assembly line for the company. He presents the following data to you that he’s researched:             Estimated life of assembly line:                 4 years             Initial investment cost:                                   $800,000             Estimated salvage value:                                none...
Are you able to implement a stack using just one queue? If yes, please provide the...
Are you able to implement a stack using just one queue? If yes, please provide the pop(only) algorithm to simulate the stack operations with just one queue. If yes, please provide the pop(only) algorithm to simulate the stack operations with just one queue. If no, explain.
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
You must be able to describe (in Australian accounting context/environment), to a manager or to a...
You must be able to describe (in Australian accounting context/environment), to a manager or to a client, the ATO requirements and relevant accounting terminology that applies to the preparation and submission of BAS and IAS. Some of the terminology includes the listed items. Briefly explain what each of the following is: RCTI FBT WET GST BAS IAS TASA withholding tax GST inclusive ABN the business’s document ID ATO TPB entity reconcile salaries and wages Code of Professional Conduct
IN JAVA Implement Quicksort with ‘median-of-three’ partitioning You should be able to test a 1000, 10000...
IN JAVA Implement Quicksort with ‘median-of-three’ partitioning You should be able to test a 1000, 10000 and 10000 length array with inters spanning from 1 to 10,000. You cannot use functions from standard libraries implementing Quicksort.
Laws for Accountants There are several terms you must know to be able to handle commercial...
Laws for Accountants There are several terms you must know to be able to handle commercial paper issues. Here are the terms you need to begin with-what is the definition and what are the specific elements required? 1. commercial paper 2. holder 3. holder in due course 4. real defenses 5. personal defenses 6. elements of negotiability 7. shelter principle a. Let's begin with these terms-their definitions and the elements required to fulfill the requirements for these various terms are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT