I've written this code using ICC AVR software for generating 125 khz square wave. I'm using timer2 of Atmega32. But i'm getting continuous low on pin OC2 instead of a square wave.
PORTD.7 generates the 125kHz square wave using timer2
output compare.
- PORTD is set for output
- Waveform Generation Mode is set to "CTC"
- Compare Output Mode is set to "Toggle 0C2 on compare match"
- clock speed is 8 MHz.
Once OCR2 reaches count value, it will toggle OC2 to generate the 0V-5V
125kHz square wave.
what is the error in the program?
PORTD.7 generates the 125kHz square wave using timer2
output compare.
- PORTD is set for output
- Waveform Generation Mode is set to "CTC"
- Compare Output Mode is set to "Toggle 0C2 on compare match"
- clock speed is 8 MHz.
Once OCR2 reaches count value, it will toggle OC2 to generate the 0V-5V
125kHz square wave.
Code:
#include <iom32v.h>
#include <macros.h>
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0xFF;
}
//TIMER2 initialize - prescale:1
// WGM: CTC
// desired value: 125KHz
// actual value: 121.212KHz (3.1%)
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0xE0; //setup
OCR2 = 0x20;
TCCR2 = 0x19; //start
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer2_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
while(1)
{};
}
what is the error in the program?