Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Unable to generate 125 Khz free running square wave...

Status
Not open for further replies.

vinil1410

New Member
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.

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?
 
I've rewritten your program for CodeVision AVR and tested with AVR Studio.

It seems it will work perfect.

Test the program in simulator of Studio yourself.

Is the Pin wired to GND anywhere?

Code:
#include <mega32.h>

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here

}


void pin0(void)
{
#asm("NOP");
}

void pin1(void)
{
#asm("NOP");
}


//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
 #asm("CLI"); //disable all interrupts
 timer2_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 #asm("SEI"); //re-enable interrupts
 //all peripherals are now initialized
}


// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x0F;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0xFF;



// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;






{
 init_devices();
 while(1)
 {
 if(PIND.7==0)
    {
    pin0();
    }
 else
    {
    pin1();
    }
 };
}

// Global enable interrupts
#asm("sei")

/*while (1)
      {
      if(TIFR0 == 1)
      { PORTA.0=1;}
      else
      { PORTA.0=0; };
    
      };*/
}
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top