0
\$\begingroup\$

Hi I have got a problem that will be simple to you guys but i am newbie so its difficult for me to get things done correctly,

I Have buzzer interfaced with PinC0 of atmega162 , with crystal 12MHz Problem is that Its not producing beep with delays.

#define F_CPU 12000000UL
#include<avr/io.h>
#include<util/delay.h>

int main(void)
{
DDRC |= 1<<PINC0;
PORTC |= 1<<PINC0;
while(1){
PORTC ^=1<<PINC0;
_delay_ms(1000);
}
}

Buzzer is connected on PINc0 and then grounded on another end. its just a common buzzer.I dont know its model or datasheet

Every thing was working fine some days before I don't exactly know what happened all of sudden.

Proteus Simulation is working correct but realtime is producing long beep in slow voice with no delays

Awaiting for your appraising replies. :)

\$\endgroup\$
  • \$\begingroup\$ As per your edit if you have a multimeter because you don't know the buzzer's details it might be worth connecting straight to Vcc on the microcontroller and put the meter in series to measure what current it needs. That will also confirm when connected directly the buzzer itself is still OK. \$\endgroup\$ – PeterJ Jan 26 '15 at 10:18
  • \$\begingroup\$ put the meter on C0 too, see if the voltage there goes far enough to run your buzzer. see what happens with the buzzer disconnected, it could be that you need a transistor to switch the curren for your buzzer. \$\endgroup\$ – Jasen Jan 26 '15 at 10:24
  • \$\begingroup\$ @Jasen Voltage across buzzer is 3.12V current it need is 23.4mA P.S. Everything has worked perfectly 2,3 days before. I think my delay has got a problem because buzzer is giving beep continuoisly. \$\endgroup\$ – user3306314 Jan 26 '15 at 10:59
  • \$\begingroup\$ @PeterJ Buzzer is Ok. Its also gives beep with my code above, but I want delay in beeps. Thats not happening. \$\endgroup\$ – user3306314 Jan 26 '15 at 11:00
1
\$\begingroup\$

You can't do delay_ms(1000) with F_CPU so high,

the manual says the most you can get at 12MHz is a little under 22ms

Perhaps do this instead of _delay_ms(1000)

for(int x=0;x<50;++x) _delay_ms(20);
\$\endgroup\$
  • \$\begingroup\$ I'd have hoped for a compiler warning, or error in such cases, but apparently they didn't see the need. \$\endgroup\$ – Jasen Jan 26 '15 at 11:13
  • \$\begingroup\$ I used _delay_ms(23); but still getting continous beep. by using ur for loop i got error "../buzzer.c:11: error: 'for' loop initial declaration used outside C99 mode " \$\endgroup\$ – user3306314 Jan 26 '15 at 11:19
  • \$\begingroup\$ 23 was wrong, and is too short to hear anyway. you'll need to enable c99 mode or move the int x outside of the for {int x;for(x=0;x<50;++x) _delay_ms(20);} \$\endgroup\$ – Jasen Jan 26 '15 at 11:26
  • \$\begingroup\$ Still unable to get it done. still producing long beep. without delay \$\endgroup\$ – user3306314 Jan 26 '15 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.