Question

In: Computer Science

Timer 0 in normal mode (AVR) MSYS LAB8 I'm lost on how to write this.. #include...

Timer 0 in normal mode (AVR) MSYS LAB8

I'm lost on how to write this..


#include <avr/io.h>
#include "led.h"

// Prototype
void T0Delay();

int main()
{
   unsigned char x = 0;
   //Ready the LED-port
   initLEDport();
       DDRA = 0x00; //port A as input
       DDRB = 0xFF; //port B as output

   while(1)
   {
       // Wait 1/125 seconds
       T0Delay();
       // Increment and show the variable x
       x++;
       writeAllLEDs(x);
   }
}

void T0Delay()
{
   //<----- Write this function so that T0Delay() uses timer 0 in normal mode,
   // in order to wait exactly 1/125 seconds.
   // Use a clock prescaler value of 1024.
}

Solutions

Expert Solution

ASSUMPTIONS:

Since, You have not provided Micro controlle's Frequency and AVR's chip name and number.

Assuming ATmega32 with 8Mhz crystal frequency.

Methods discussed below could be implemented in any AVR chip , So you just need to change few values as per the discussed formulas.

TIMER0 :

(You need to find same registers if you are not working on assumed Micro controller unit,i.e, Atmega32. Prefer to read Datasheet of your chip to do so.)

From Atmega32 Data sheet , in Timer0 section we need to look on the following Data registers:

1) TCCR0(Timer/Counter Control Register 0) :

Now, We need to set following things

a) Operate Timer0 in Normal Mode : WGM00->0 , WGM01->0

b)Set Pre-scaler of 1024 : CS02->1, CS01->0 , CS00->1

So, Overall TCCR0 becomes

So, TCCR0 = 0b00000101 = 0x05

2)TCNT0 : It's a 8 bit register. The value of the counter is stored here and it increases and decreases automatically.

3) TIFR0( TIMER/COUNTER INTERRUPT FLAG REGISTER ):

We are only interested in TOV0 flag , it raises when Counter Overflows and to clear it we need to write 1 to it.

So, Bitwise and with TOV0 flag bit and TIFR register will give 0 until , TOV0 becomes 1;

So, (1<<TOV0) ==> will give bit value of TOV0

So, BItwise and with TIFR will always give 0 , until TOV0 becomes 1.

(TIFR&(1<<TOV0)) == 0 till TOV0 becomes 1.

METHODOLOGY:

In order to produce delay of    .   We need to calculate value that should be written in TCNT0.

Now,

Since we need delay of

Value of TCNT0 = 1 + 0xFF - 63

Here , 0xFF = 255 in decimal.

So,

TCNT0 = 1+255-63 = 193

PROGRAM :

void T0Delay()
{
//Load TCNT0 with calculated value

TCNT0 = 193;

//Set TCCR0 =0x05 ,for Normal Mode with Pre-scalar of 1024

TCCR0 = 0x05;

//Start a while loop until TOV0 raises to 1

while((TIFR&(1<<TOV0))==0);

//Stop Timer 0

TCCR0 =0;

//Clearing TOV0 flag , by setting it to 1

TIFR = 0x1;
}


Related Solutions

Question 1: Answer the following questions related to AVR Timer/counter: Write an AVR program that flashes...
Question 1: Answer the following questions related to AVR Timer/counter: Write an AVR program that flashes an LED (PC0) every 6 ms using CPU clock frequency of 32 kHz and TIMER0. Use Timer/Counter0 Overflow Flag (TOV0) in your code. Write an AVR program that flashes an LED (PC0) every 2 seconds using XTAL clock frequency of 16MHz and Timer1. Use Timer/Counter1 Overflow Flag (TOV1) in your code.
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...
11. Using Timer1 and CTC mode of the ATMega32, write an AVR C program that uses...
11. Using Timer1 and CTC mode of the ATMega32, write an AVR C program that uses the hardware waveform generator to generate a square wave on pin OC1A with a frequency of 50 Hz if PA1 is high, otherwise generate 60Hz. Assume XTAL = 8 MHz. [10 Marks]
Referring 8051 User’s Manual ,study how to use timer when operating in mode 2. Understand the...
Referring 8051 User’s Manual ,study how to use timer when operating in mode 2. Understand the manual (p. 112~)
I'm completely lost can someone really break it down in English with how to answer each...
I'm completely lost can someone really break it down in English with how to answer each For each question, draw the appropriate picture, with shading. Then show all calculations and write a sentence for your answer. The weight of new-born babies in the US is normally distributed with a mean of 7.5 pounds and a standard deviation of 2 pounds. What percentage of new-born babies in the US weighs more than 7.5 pounds? What is the probability that a new-born...
Please explain how to solve and show all work! I'm extremely lost. Part 1: A 180,000...
Please explain how to solve and show all work! I'm extremely lost. Part 1: A 180,000 gal. swimming pool requires treatment with 8.85 L of 7.00% "by mass" chlorinating solution to give chlorine level of 1.00 ppm (1.00 g chlorine per 10^3 kg of pool water). Based on these data, calculate d (in g/ml) for the chlorinating solution nothing that it is close to 1 g/mL. * "% by mass" means "mass of solute/100g solution". thus, this chlorinating solution contains...
I'm using this to study for an exam, please write out all steps and include formulas,...
I'm using this to study for an exam, please write out all steps and include formulas, thank you! The world's fastest land animal, the cheetah, can accelerate at 8.75 m/s2 to a top speed of vcmax = 30.5 m/s. A cheetah observes a passing gazelle traveling at vg = 19.1 m/s and begins to chase it. (a) How long, in seconds, does it take the cheetah to reach its maximum velocity, vcmax, assuming its acceleration is constant? (b) How far,...
Describe the standard normal curve. a. What does the mean, median, and mode equal? How many...
Describe the standard normal curve. a. What does the mean, median, and mode equal? How many standard deviations are on the right side of the curve? How many standard deviations are on the left side of the curve? What does the y-axis represent? Draw the standard normal curve.
write 1 to 2 page film review on Lost Boys of Sudan and how is it...
write 1 to 2 page film review on Lost Boys of Sudan and how is it related to globle health
If possible, could you write out the process of these problems? include a drawn normal distribution...
If possible, could you write out the process of these problems? include a drawn normal distribution with shading please. 1. suppose X-N (4, 2) what value of x is 1.5 standard deviations to the left of the mean? 2. Suppose X-N (15,3) between what x values does 68.27% of the data lie? The range of x values is centered at the mean the distribution (i.e., 15) 3. X-N (54,8) find the probability that x>56 4. height and weight are two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT