PDA

View Full Version : Play & Record PWM audio?



Art
- 9th April 2015, 04:56
Hi Guys,
I’m wondering if anything has been done in the way of converting audio directly to PWM
and playback with hardware PWM? Or am I on my own?

Recording either analogue input, or digitising to 1 bit and counting frequency directly.
I’m going to try the latter first.

I know about RB’s pic sound playback, but IIRC the PBP implementation could not be fast enough.
Cheers, Art.

Normnet
- 9th April 2015, 08:49
It can be done and it sounds decent.

Norm

Art
- 9th April 2015, 11:16
Which method and at what speed are you talking about?
Even at 10MHz with midrange pic I can:


loop:
outputpin = inputpin
goto loop

and that starts to get somewhat intelligible, but you couldn’t do anything with it.
If I sample the audio frequency for a period, then set the HPWM,
I find the more often I set the HPWM, the less gate time I get to count the input.

Normnet
- 9th April 2015, 13:58
Art

I used a 18F26K20 hardware PWM and a timer interrupt to set 10 bit data read from an SD card WAV file to an audio amp at a 44.1K data rate.
If I remember right that's CD mono quality.
There are very few components i.e. PIC, SD card, audio amp and a speaker.


Norm

Art
- 10th April 2015, 03:09
If the CD player had a 1 bit DAC.
I’ve used RB’s audio reading from EEPROM fine in projects, the slow down is processing incoming audio as well.
I’ll wait for a faster crystal to continue, but might be better off measuring the width of an audio wave rather than counting zero crossings,
so then there is more time to do something with the data.

HPWM command is a bit crook as well trying to update it fast. I’m sure it has to figure out,
and write all four registers associated with HPWM each time it’s called.
I got it smoother by limiting the frequency range and only altering the PR2 value.
Still I don’t think it can be saved to EEPROM, even if interleaved to avoid the 10ms wait.

I have so far kind of a voice controlled PWM, some fun could be had there, so I’ll look at making that faster.

HenrikOlsson
- 10th April 2015, 06:31
Yeah, the HPWM command adds a bit of overhead (write configuration register, period register, duty register) so if you need fast duty cycle updates just configure the CCP module manually and write the duty register each update.

ruijc
- 10th April 2015, 09:19
Hi guys,

This is a very interesting topic.
Does anyone has a sample code so we can try?

Regards
Rui

Art
- 10th April 2015, 13:56
It’s really a matter of reading a pin for a period and outputting that frequency.
You could probably use PULSIN for a period after sorting out mic hardware to trigger the pic input.
I used an LM358 op-amp to drive a condenser mic.

I’m using DT’s timer, and most assembler now, but still a simple program.
On interrupt, a frequency value is written to hardware PWM one way or another (that I haven’t settled on),
then the frequency variable cleared before returning to main program.
The main program only counts audio transitions with the frequency variable in a loop.

Tonight I got desperate and connected a 64.5 MHz crystal to the pic 16F628A and it works fine!
I don’t know if it’s actually clocking at that frequency, but I have used 24/25 MHz crystals which have always gone fine,
and it’s now much faster than them.

Art
- 12th April 2015, 11:45
The example where it’s the loudest in the video (quite an obvious one),
I think I would have 80’s style speech recognition down.
That variation of the code was very repeatable for words/phrases.

https://www.youtube.com/watch?v=GnfMJtfvTU0