In: Electrical Engineering
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]
The frequency of the wave is given as
where f_clk_I/O is input clock frequency(clock frequency)
N is the Prescaler
OCRnA is the value in OCRA which is a 16-bit number value from 0 to 65535
So, to produce a wave with 60Hz,
We should have Prescaler to 8 and OCR1A to 8,320
To produce a wave with 50Hz
We should have Prescaler to 8 and OCR1A to 9,999
AVR C Code:
int main(void)
{
DDRA |= 1<<DDA1;
//Sets PA1 as Output
TCCR1A |= 1<<COM1A1; //Clears OC1A
on compare match
TCCR1B |= 1<<WGM12;
//Setting to CTC Mode
while(1)
{
if(PORTA &= 1<<PA1)
{ //Setting to 50Hz if
PA1 is High
TCCR1B |=
1<<CS11; //Setting Prescaler to 8
OCR1A =
9999
}
else
{ //Setting to 60Hz if
PA1 is Low
TCCR1B |=
1<<CS11; //Setting Prescaler to 8
OCR1A =
8320;
}
}
}