Buzzer toning problem

Go To Last Post
18 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I want to make a buzzer toning with timer as pwm on PD4

it doesn't make any sound at all

but when i connect it to PD5 it makes sound with different levels as OCR1A values change

i think it doesn't work as pwm with those configuration

i'm using 8MHz F_CPU and prescaling of 64

or any ideas about sample code as interrupt to use PD5 for a buzzer and a Led if possible 

This my code , What seems to be the problem

 

#define F_CPU 8000000UL /* Define CPU Frequency e.g. here its 8MHz */

#include <avr/io.h> /* Include AVR std. library file */

#include <stdio.h> /* Include std. library file */

#include <util/delay.h> /* Include Delay header file */

int main(void) {

    DDRD |= (1<<PD4); 

    TCNT1 = 0; /* Set timer1 count zero */

    ICR1 = 2499; /* Set TOP count for timer1 in ICR1 register */
    

    /* Set Fast PWM, TOP in ICR1, Clear OC1A on compare match, clk/64 */

    TCCR1A = (1<<WGM11)|(1<<COM1A1);

    TCCR1B = (1<<WGM12)|(1<<WGM13)|(1<<CS10)|(1<<CS11)|(1<<COM1B1);

    while(1) { OCR1B = 200;

        _delay_ms(500);
        OCR1B = 100;
        _delay_ms(500);

    }
    }

This topic has a solution.
Last Edited: Thu. Mar 28, 2019 - 10:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which AVR are you using?

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which AVR are you using?  A Mega32 by chance?

 

We have another freak here using the same IP you are using....you two related by chance?

 

Heres his code for trying to control a servo:

#define F_CPU 8000000UL /* Define CPU Frequency e.g. here its 8MHz */

#include <avr/io.h> /* Include AVR std. library file */

#include <stdio.h> /* Include std. library file */

#include <util/delay.h> /* Include Delay header file */

int main(void) {

DDRD |= (1<<PD5); /* Make OC1A pin as output */

TCNT1 = 0; /* Set timer1 count zero */

ICR1 = 2499; /* Set TOP count for timer1 in ICR1 register */

/* Set Fast PWM, TOP in ICR1, Clear OC1A on compare match, clk/64 */

TCCR1A = (1<<WGM11)|(1<<COM1A1);

TCCR1B = (1<<WGM12)|(1<<WGM13)|(1<<CS10)|(1<<CS11);

while(1) { OCR1A = 65; /* Set servo shaft at -90° position */

_delay_ms(1500);

OCR1A = 175; /* Set servo shaft at 0° position */

_delay_ms(1500);

OCR1A = 300; /* Set servo at +90° position */

_delay_ms(1500);

}

}

 

 

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

That is a REALLY odd use of a timer. I really suggest that you use EITHER (just one) of

 

1) A timer in PWM mode

 

2) A chain of delays: set port pin high, delay, set port pin low, delay, set port pin high ....

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Mon. Mar 25, 2019 - 08:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i have both atmega 16 and 32

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No , but it seems he is using some of the websites i'm surfing

it's exactly the same but i still can't run the buzzer toning throughout PD4

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How can i make a timer in PWM mode on PD4

can you show me code initialization for it ? 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The COM1B1 bit goes in TCCR1A.

 

--Mike

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i want to initialize the value of OCR1B that makes the buzzer so quite ( no volume at all )

i set it up to OCR1B = 0;

but it still has a volume i can hear like ticking one , i want to make it stop till later on i use it in my code

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

James McGarphy wrote:

i want to initialize the value of OCR1B that makes the buzzer so quite ( no volume at all )

i set it up to OCR1B = 0;

but it still has a volume i can hear like ticking one , i want to make it stop till later on i use it in my code

 

You can't. You have to turn off the output.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Set your three CS bits to zero - NO CLOCK, or as Brian mentions simply turn off the output Pin

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Turn it off like PORTD &=~(1<<PD4);

OR Clear CS Like this   

    TCCR1B &=~ (1<<CS10);
    TCCR1B &=~ (1<<CS11);
    TCCR1B &=~ (1<<CS12);

Last Edited: Wed. Mar 27, 2019 - 02:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

James McGarphy wrote:

Turn it off like PORTD &=~(1<<PD4);

OR Clear CS Like this   

    TCCR1B &=~ (1<<CS10);
    TCCR1B &=~ (1<<CS11);
    TCCR1B &=~ (1<<CS12);

 

Try it and see which works best for you.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

what is toning?  volume level?

 

In any case, certain PWM modes can't go down to 000000000 (absolute zero), you still get a runt pulse.

A way around that is to set the IO pin PWM control bits to give an inverted value.  You then simply adjust your pwm setting, ex:  use 255-former value (for an  8 bit example).

With those changes, you can get to zero, but then not 100% (call it 99.99)

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
what is toning

I read it as the OP is trying to have the buzzer make a simple tone.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

There are two approaches to this problem.  One is to research, test, and debug the use of a timer to specifically output a PWM-based tone to a (often pre-determined) pin.  This takes a long time and entails lots of data sheet reading, timer configuration (never easy on the peripheral configuration register level), and repeated hardware experimentation.   But you can use your code on any of the 100+ AVR variants.

 

Or you can get an Arduino Nano/UNO module board (based on the ATmega 328P) from eBay for about $3-4 USD and download the Arduino IDE. 

Now add the line below to your Arduino program/sketch:

 

tone(pin, frequency, duration)tone(pin, frequency, duration)     tone(D8, 500, 0.5);   // produces a 500Hz half-second beep on piezo-speaker connected to Arduino pin/via marked D8.

 

There are lively discussions here about which is the better approach.   Personally I'd go with the tone() function Arduino-expanded-keyword-set approach.  Because any code that you write directly transfers to any CPU that implements to Arduino-expanded-keyword-set.   So you don't have to re-read over-and-over the cryptic data sheets for insight on how make a processor-specific  implementation of same embedded-systems peripheral with every different processor that you use.  Learn one set of expressions for accessing I2C and use the same code on the Tiny85, the mid-range ATmega328P,  the fat-boy ATmega2560, and the high-end CPUs like ESP8266 WiFi, HM10 BlueTooth, and STM32F103 ARM-based Arduino module boards.

 

True, you won't become an expert on any individual CPU.  But the time that you would have spent doing this is now better spent on developing the specific applications that the customer is paying for.  And not dinking around with a thousand individual CPU programming details.

Last Edited: Wed. Mar 27, 2019 - 11:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Arduino's are designed for kids & tinkerers playing around, professionals use real development tools and custom circuits 

 

Home computers are made for kids & tinkerers playing around, professionals use Mainframes and flowchart templates

 

History repeats itself!

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:
Or you can get an Arduino Nano/UNO module board (based on the ATmega 328P) from eBay for about $3-4 USD and download the Arduino IDE.

 

Yes, that is of course an easy option, but the part of the world the OP comes from they seem to like using the MEga32, and its variants, so the Arduino IDE is not a good option in this case.

 

And whats wrong with a little research and reading?  Setting up the Timer(s) in the AVR is not really all that hard(says someone who has failed at this once or twice).  It's a matter of understanding how the peripheral works.

 

If the OP elects to switch to an Arduino then we can work with that when the time comes.  FOr now we can concentrate on what the Thread is based on....a Mega 32 or Mega 16

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user