Panteltje's audio_pic page

Panteltje's audio_pic page

~

Here you find the listing for a Microchip PIC 18F14K22 to make it function as a class D (PWM output) audio amplifier.

This code is made available under the GPL license.



The PIC can drive a 200 Ohm load directly.
If you use it with earplugs, use a 180 Ohm resistor in series, and beware of the 60 kHz PWM level.
Some lowpass filter is recommended.
Click here to download audio_pic-0.5.asm
The asm source is assembled with gpasm, part of gputils.
It will likely not assemble without modification in Microchip Mplab, here is the assembled hex file:
Click here to download audio_pic-0.5.hex


This diagram shows how to use the PIC:
audio_pic-0.4.gif

The inputs for the switches use internal pullups at about 140 uA.
To use the RS232 you need to for example connect a MAX232 chip, RS232 is not needed for this circuit to work as an audio amplifier.
With a slight modification of the code you can also drive a full external PWM bridge for more power using pin 7 (P1C) and pin 14 (PID)
Pin 19 provides about 1 V DC bias for the audio input on pin 17.
Audio is sampled at about 160 kHz (no typo) when volume is 100 %, and at about 33 kHz when volume down has been activated, as calculating new values for the PWM module takes time.
The PWM frequency is about 63 kHz.
Both sample rate, PWM frequency, PWM output configuraton, and PWM dead time, can be set in software.

The PIC accepts the following commands via RS232 at 19200 Bd, 1 start bit, 8 data bits, no parity, 1 stop bit:
h help, this help.
S enter audio_streaming mode, only a power down will leave this mode, accepts 8 bit unsigned samples at baudrate, default off.
] volume up.
[ volume down.


About the audio streaming mode:
Typing 'S' at the terminal will enable audio streaming mode via RS232.
Send unsigned 8 bits audio at whatever baudrate you can, remember 19200 Bd and 10 bits per character amounts to only about 2000 Hz sample rate:
You can configure for higher baudrates in the source file.
Make a 8 bits audio file like this in Linux:
sox some_cd_track.wav -c 1 -r 2000 -b -u test.wav resample.
cat test.wav > /dev/ttyS0
So for 8000 samples / second (the absolute minimum) you need 160000 Bd, not likely to be from a PC.
But this mode can also be used to send lots of values to the PWM output.
For a bass demo, you can extract the sound below xxx Hz from a sound file, by using the low pass function of sox like this:
sox music_with_lots_off_bass.wav -c 1 -r 2000 -b -u low_ bass_only.wav resample lowpass 100
Communicate with the PIC, and set serial baudrate to 19200:
ptlrc -d /dev/ttyS0
Type 'S' (without the quotes) to put the PIC into streaming mode, then type ESC key to exit ptlrc, and type:
cat bass_only.wav > /dev/ttyS0



Here is a simple parallel port programmer for the PIC 18F14K22:
jppp18


Here is a simpe communication program to talk to the PIC:
ptlrc


Click here for some other programs I make available under the GPL

Click here to send mail