Program Timer0 with normal mode and prescaler of 8 to genera

Program Timer0 with normal mode and prescaler of 8 to generate a square wave of 2 KHz on the PORTB.7 bit. Assume that XTAL = 8 MHz.

Solution

Note: Before solving this problem solution you must know about Atmel studio or any other studio to make a hex file for the desired solution .Basically the hex file is the coding of our solution. So for our timer square wave form we must have any software that can make hex file for AVR Atmega 32 .
And also you must have Proteous of any version for testing simulation before going towards the hardware .
Here I am using Atmel studio 6.1 and proteous 8 for my task .

Solution:

First of all we have to calculate the frequency will be

2000*82 = 164000 Hertz is approx equal to 0.164 Mhz

Steps to Program 8-Bit, Timer 0 in Normal Mode:

1. Load TCNT0 register with initial values from which the timer will count up.
2. Load TCCR0 register according to the operation required (Mode selection, prescaler setting etc.)
3. Keep monitoring TOV0 flag in TIFR register. If TOV0 flag is raised, timer overflow occurs.
4. Now clear TOV0 flag and proceed to step 1 again.

Calculating Delay :

For a clock generation of (2000*82)Hz=164000 (6.09us), timer should be overflowed twice, so:

Timer overflow = 6.09ms / 2 = 3.9us (3.045us high, 3.045us low)

Crystal Clock = 8MHz

Prescalar used = 8

Timer clock = 8MHz / 8 = 1000000 Hz

Timer Period = 1/1000000 = 1us

Timer Value = 3.045us / 1us = 3.045 = 3 (approx)

TCNT Value = (256+1)-3 = 254 = 0xFE

Program :

#define F_CPU 8000000
#include<avr/io.h>
#include<util/delay.h>

//Macros definition
#define BitGet(p,m) ((p) & (m))
#define BitSet(p,m) ((p) |= (m))
#define BitFlip(p,m) ((p) ^= (m))
#define Bit(x) (0x01 << (x))

void TimerDelay(void); //Function prototype declaration

void main(void)
{
DDRA = 0xFF; //Make PortB output
BitSet(PORTB, Bit(7)); //Initially set PB7
while(1)
{
BitFlip(PORTB, Bit(7)); //Toggle PB7
TimerDelay(); //Generates delay of 3.045us
}
}


void TimerDelay(void)
{
TCNT0 = 0xFE; // (256+1)-3 = 254 = 0xFE
TCCR0 = 0x05; //Timer0 ON, clk/8 prescaler, Normal Mode
while(!BitGet(TIFR, Bit(7))); // Wait for timer0
overflow & TOV0 flag is raised
TCCR0 = 0x00; //Stop Timer
BitSet(TIFR, Bit(7)); //Clear TOV0
}

Program Timer0 with normal mode and prescaler of 8 to generate a square wave of 2 KHz on the PORTB.7 bit. Assume that XTAL = 8 MHz.SolutionNote: Before solving
Program Timer0 with normal mode and prescaler of 8 to generate a square wave of 2 KHz on the PORTB.7 bit. Assume that XTAL = 8 MHz.SolutionNote: Before solving

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site