Enide! my web project

PIC sound player (PCM to PWM converter)

Description

This project makes a PIC microcontroller speak audio PCM sounds using PWM modulation! Pulse-code modulation (PCM) is a digital representation of an analog signal where the magnitude of the signal is sampled regularly at uniform intervals, then quantized to a series of symbols in a digital (usually binary) code [1]. Pulse-width modulation (PWM) of a signal or power source involves the modulation of its duty cycle, to either convey information over a communications channel or control the amount of power sent to a load [2].

This project uses a speaker as the load. 

Design and Implementation

PIC pictureThe selected microcontroller is a PIC 18F1320 [3] with 8KBytes of flash memory and a ECCP unit. A smaller flash unit also exists (18F1220). The 8 KBytes of flash allow almost a second of PCM audio sampled at 8000Hz to be loaded into the PIC. One second of audio is nothing compared with modern digital audio recorders but is enough to demonstrate the application.

Audio samples can be found free of charge on the internet. As long as they can be converted to WAV format in 8bits per sample, at 8000Hz sampling, mono channel and have less than one second of audio, they can be used in this circuit. 

 

 

Schematic

schematic

The basic circuit includes a 78L05 voltage regulator, 100nF decoupling capacitor, the PIC, the speaker and a low-pass filter. The low-pass filter has its cutoff frequency at 3.3KHz.

The PIC microcontroller is programed to use the internal oscilator running at 8 MHz. That way we save a crystal and two capacitors.

The 78L05 is capable of supply 100mA of current which is enough to power the PIC with the speaker. Even a small amplifier may be included to provide more sound.

 

PCB

No PCB was made because this is only a demonstration.

 

Software

The software for the PIC was developed in C (HI-TECH) and is available. It uses the ECCP unit in PWM mode to generate the PWM in hardware. Timer 2 is used to mark the PWM period. To create pauses, timer 2 interrupt is used to decrement a counter. This counter indicates the number of cycles to pause.

The sound wave form is stored in a H (header) file. An utility, called pcm2h, was created that reads a PCM file and creates the H file. 

The program used to create a PCM file from a WAV file was Linux Mplayer.
The arguments are: mplayer myfile.wav -ao pcm:nowaveheader:file=myfile.pcm

 

Evaluation

Tests show that a first-order lowpass filter is not enough to attenuate the sampling frequency noise. This means that along with the sound comes the 8KHz sampling noise. A second-order filter will probably remove it completely.

The sound is perceptible simply by using a speaker connected to the PIC. A small amplifer like LM386 or uA741 is definitely required if someone else has to hear the audio.

 

Downloads

Microcontroller stuff

PC Software

compile this with something like: gcc -W -Wall -o pcm2h pcm2h.c either in windows or linux

Some audio samples

Microchip MPLAB projects with this code

References

[1] Pulse-code modulation from Wikipedia

[2] Pulse-width modulation from Wikipedia

[3] PIC18F1320 Datasheet from Microchip

[4] The Free Sound Project - Diesel train recording by amabok