Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Waveform Generation Create a Waveform on PD4 using Timerl to make a sound on the

ID: 3349427 • Letter: W

Question

Waveform Generation Create a Waveform on PD4 using Timerl to make a sound on the buzzer. Alternate the frequency of the waveform every 500ms to create a sirene. Buzzer is located at PORTD.4. (Do not forget to set the jumper on the board, as well as set correctly the directions of the Ports in your code. Show your progress to professor. The group shall prepare a report and submit the source code very well commented (every line of code), showing that you know what you are doing. Submit the report through Moodle. Extra Credit-20 %-Execute at least a 15 seconds of a known tune. The report shall have the answers to the following questions: 1-) Assuming XTAL-8MHz, calculate the frequency of the wave generated by the following program: TCCRIA = 0x40; TCCR I B = 0x09; OCR IAH 0:02; OCRIAL 0x00; (b)OCR1A is loaded with 0x0500 (c)XTAL-MHz and OCR1A is loaded with 0x0005 (d)a prescaler option of 8 is chosen, XTAL = 4MHz. OCR IA = 0x 1 50 e)a prescaler option of N is chosen, XTAL -Fosc, OCRIA-X

Explanation / Answer

$$FTimer = CPU Frequency/Prescalar $$ $$Ftimer = 16MHz/1024 = 15.625KHz $$ $$Ttick = 1/ 15.625K = 64 mu seconds$$ $$Ttotal = 64mu s X 255 = 16ms$$

Of-course 16ms is not enough, so the next obvious question is:


How many times should the timer overflow to generate a delay of approximately 500msec?

$$ OverFlowCount = 500ms/16ms = 31.25 31 $$

Now let's write a simple program which will toggle a port pin (PD4) after the timer 0 overflows 6 times.

Load TCNT0 with 0x00

Set CS00 and CS02 bits in TCCR0 register. This will start the timeWe will calculate the tick time in just a moment.r at Clk/1024 speed.

Monitor the TOV0 flag in the TIFR0 register to check if the timer has over-flowed, keep a timerOverFlowCount.

If timerOverFlowCount >= 31, toggle the led on PD4 and reset the count

#include<avr/io.h>

#define LED PD4

int main()

{

uint8_t timerOverflowCount=0;

DDRD=0xff; //configure PORTD as output

TCNT0=0x00;

TCCR0 = (1<<CS00) | (1<<CS02);

while(1)

{

while ((TIFR & 0x01) == 0);

TCNT0 = 0x00;

TIFR=0x01; //clear timer1 overflow flag

timerOverflowCount++;

if (timerOverflowCount>=31)

{

PORTD ^= (0x01 << LED);

timerOverflowCount=0;

}

}

}

#include<avr/io.h>

#define LED PD4

int main()

{

uint8_t timerOverflowCount=0;

DDRD=0xff; //configure PORTD as output

TCNT0=0x00;

TCCR0 = (1<<CS00) | (1<<CS02);

while(1)

{

while ((TIFR & 0x01) == 0);

TCNT0 = 0x00;

TIFR=0x01; //clear timer1 overflow flag

timerOverflowCount++;

if (timerOverflowCount>=31)

{

PORTD ^= (0x01 << LED);

timerOverflowCount=0;

}

}

}