Author Topic: Generate single frequency(audio siren) without CPU ussage  (Read 412 times)

0 Members and 1 Guest are viewing this topic.

Offline BinaryWorld

  • Contributor
  • Posts: 9
Generate single frequency(audio siren) without CPU ussage
« on: May 20, 2018, 02:33:23 am »
Hi everyone,

I'm doing my project on SparkFun ESP32 Thing with ArduinoIDE
 https://botland.com.pl/plytki-zgodne-z-arduino-sparkfun/7911-sparkfun-esp32-thing-modul-wifi-i-bluetooth-ble-kompatybilny-z-arduino-ide.html

and i try to generate siren start procedure(ready .. go; 3 signals: low pitched, low pitched, higher pitched).

When i add loop audio():

Code: [Select]
void audio(float frq){ // frq  Hz 

  if(sound == 0)
    {
      sound = micros();
    }

  if(sound + (1/frq)*500000 >= micros() )
    {
      digitalWrite(buzzer, HIGH);
    }
  else
    {
      digitalWrite(buzzer, LOW);
    }
  if(sound + (1/frq)*1000000 <= micros() )
    {,
      sound = 0;
      digitalWrite(buzzer, HIGH);
    }
   
}
//2500
//2500
//3000 hz
sound isn't clear. I don't use delay() in my program, but other things happen at the same time (reading expander pcf8574 via i2c, every 1ms).

I read about pwm(dma?), in technical references:
https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf
but i don't know how to use it(without any library, becouse it should be easy).


Will there be a helpful hand that will help me understand how to easily generate any frequency from 1-10 khz on gpio?

Thank you for all ideas
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 4478
  • Country: us
 

Offline BinaryWorld

  • Contributor
  • Posts: 9
Re: Generate single frequency(audio siren) without CPU ussage
« Reply #2 on: May 20, 2018, 04:24:11 am »
Unfortunately, this feature does't work with esp library.

I found this:
https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM
Phase-Correct PWM look great. Its tutorial for ATmega.
I am trying to translate this to esp32 (with esp32 register).

I understand how here set(choose) prescaler value and cycles:
Output A frequency: 16 MHz / 64 / 256 = 976.5625Hz
The output frequency is the 16MHz system clock frequency, divided by the prescaler value (64), divided by the 256 cycles it takes for the timer to wrap around.

« Last Edit: May 20, 2018, 05:18:52 am by BinaryWorld »
 

Offline MarkF

  • Frequent Contributor
  • **
  • Posts: 722
  • Country: us
Re: Generate single frequency(audio siren) without CPU ussage
« Reply #3 on: May 20, 2018, 04:56:06 am »
I'm not familiar with the SparkFun ESP32.  However, you need to have a fixed update rate in order to generate the desired frequency.  You will get this via some hardware function.  Most liklely you are looking at a PWM function or some interrupt.

It appears that you are trying to generate a square wave.  I that case I would use one of the PWM channels and let the hardware do the timing for the updates.

If you wish to generate a sine wave. You would store one period of a sine wave in memory. (The number of samples you have will be determined by cpu clock, desired frequency output, sine wave step size.) Then via an interrupt, sequence through the samples sending each to a DAC channel.  The rate and how you step through the sine samples and the interrupt frequency will determine the frequency.
 

Offline BinaryWorld

  • Contributor
  • Posts: 9
Re: Generate single frequency(audio siren) without CPU ussage
« Reply #4 on: May 20, 2018, 05:16:41 am »
If I understand you correctly, you are talking about the method used here:
https://github.com/espressif/esp8266_mp3_decoder
I2S bus + DAC. Propably it's best way to generate audio(like mp3) but it need interrupt.
I'd rather avoid it.

I want to set it once and do it without the processor (like PWM dma).
I don't use interrupts to detect false start.
My (extended) program is able to detect the change at the input gpio without interrups.
Accuracy is below 1 ms, measurement is up to 1 ms - its ok.

Probably the rectangular course will sound good. If it turns out that I need sine wave, maybe an external chip could be used?

 

Offline MarkF

  • Frequent Contributor
  • **
  • Posts: 722
  • Country: us
Re: Generate single frequency(audio siren) without CPU ussage
« Reply #5 on: May 20, 2018, 05:48:37 am »
In a word - No.
I'm not familiar with the I2S interface.

If you want a pure tone, you will need to generate a sine wave.  A square wave contains many harmonics caused by the voltage steps.  If you just want a siren sound, a square wave maybe good enough and the easiest to setup.

In the sine wave generation, I'm talking about digital signal processing.  Unless you use the processor, you will need to add hardware to create a sine wave.  I would recommend you look at the AD9834 chip.  Using the processor, you need a constant update rate (i.e. send a sample each time an interrupt occurs) to send sine wave samples to a D/A converter.  Either the built-in D/A (if it has enough resolution) or some external D/A like a MCP4802.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf