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.

My PWM code to generate 40KHz seems to have a problem? im using ATmega32

Status
Not open for further replies.

kris_maher

New Member
Hi,

I wrote code to use ATmega32's "Phase and Frequency Correct" PWM mode. The clock speed is set to 8MHz and I wrote the code so that it generates a 40KHz square wave on the PD5 output pin. This is basically for a Ultrasonic project I'm working on.

Here's my code:

Code:
int PING()
{
// Use this FORMULA for TOP:
/* TOP/ICR1 = Clock_Speed / (2 * Prescaler *
Output_PWM_Frequency) */

// Thus ICR1 = 8000000 / (2 * 1 * 40000) = 100

// Make PD5 as output pin
DDRD = 0b00100000;
PORTD = 0b00100000;

// Turn off PWM while we set it up
TCCR1B |= 0;
TCCR1A |= 0;

// Setup the timer --> 16-bit Phase/Frequency Correct PWM mode with Pre-scaler = 1
TCCR1B |= (1 << WGM13);
TCCR1B |= (1 << CS10);

// Setup the Compare Output to be set to toggle mode. See pg.108 in datasheet
TCCR1A |= (1 << COM1A0);

// Set ICR1 so the output toggles at 40KHz
ICR1 = 100;

// Set the Comparator so we're at 50% duty cycle
OCR1A = ICR1 / 2;

}

When I use my PC Oscilloscope to observe for any waveforms coming out of the Ultrasonic transducer, nothing comes up on the screen.

Now it could be due to:
a) Something wrong with my code. Any ideas?:confused:

OR

b) A battery that's almost out of steam. :mad:I have a regulator circuit that steps down voltage to 5V, for this, I have a 9V rated battery that measures just over 6V with a multimeter.

Thanks everyone..
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top