AVR Week 4 Challenge

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Q1.Write a C program to toggle all the bits of PORTB countinuously with some dalay.

Use Timer0,
Normal Mode and no prescaler options to generate the delay.

#include<avr/io.h>
void TODelay();
int main()
{
DDRB = 0xFF; //PORTB output port
while(1)
{
PORTB 0x55;
TODelay();
PORTB = 0xAA;
TODelay();
}
}
void TODelay()
{
TCNT0 = 0x20; //load TCNT0
TCCR0 = 0x01; //Timer0, Normal mode, no prescaler
while ((TIFR&0x1)==0); //wait for TFO to roll over
TCCR0 = 0;
TIFR = 0x1; //clear TFO
}

Q2.Write a C program to toggle only the PORTB. 4 bit continuously every 2ms. Use Timer1,Normal
Mode, and no prescaler to create the delay.Assume XTAL = 8 MHz

#include <avr/io.h>
void T1delay ();
int main ()
{
DDRB = 0xFF;
while (1)
{
T1delay ();
PORTB = PORTB ^ (1<<PB4);      //toggle PB4
T1Delay();      //delay size unknown
}
}
void T1delay ()
{
TCNT1H = 0xC1;       //TEMP 0xC1
TCNT1L = 0x80;
TCCR1A = 0x00;     //Normal mode
TCCR1B = 0x01;     //Normla mode, no prescaler
while ((TIFR&(0x1<<TOV1))==0);   //wait for TOV1 to roll over
TCCR1B = 0;
TIFR-0x1<<TOV1;     //clear TOV1
}

Q3.Assuming that a 1 Hz clock pulse is fed into pin T0,use the TOV0 flag to extend Timer0 to a 16
bit counter and display the counter on PORTC and PORTD
#include<avr/io.h>
int main()
{
PORTB = 0x01; //activate pull-up of PB0
DDRC = 0xFF; //PORTC as output
DDRD = 0xFF; //PORTD as output
TCCR0 = 0x06; //output clock source
TCNT0 = 0x00;
while(1)
{
do
{
PORTC=TCNT0;
}
while((TIFR&(0x1<<TOV0))==0); //wait for TOV0 to roll over
TIFR = 0x1<<TOV0; //TIFR=0x0<<TOV0;
PORTD++;
}
}

You might also like