4268

Load your program into the PIC using the audio socket of a computer, tablet or phone. Easy to build board. Software to convert hex file to wav file.

PIC Audio Programmer By Andrew Pratt

This project might remind some readers of a method of transferring programs to the early home computers of the 70s and 80s. The idea is to use this method to load the hex file into the PIC. There are two parts to the project the first is a Python program that converts the hex file to an audio WAV file. The second part is a signal conditioning unit that takes the audio signal and programs the PIC using low voltage In Circuit Programming (ICP). This allows you to program a PIC without a programmer using the headphone socket of a computer, tablet or mobile.Two particular PICs are targetted, the 12F1822 and the 16F1823. Other PICs could be catered for all you need to do is read the relevant data sheet and modify the Python code.

Connections for Programming the PICs
The details of ICP for these chips can be found in Microchip’s data sheet ‘DS41390C’. There is a low voltage method that only requires the +5V supply to the chip and the high voltage method that requires an additional supply of between +8 and +9 V. This project only uses the low voltage method. It would not be difficult to modify the circuit and the code to use the high voltage method if you want.

Figure 1: Connections required for programming.

Fig-1 shows the five connections necessary to program the PIC. There is the power supply, the DATA, CLOCK and MCLR. The audio signal is stereo with one channel carrying the data and the other carrying the clock. The MCLR line has to be held down to 0V during programming and this is done manually by a switch. The 1K resistors are there to protect the PIC’s IO.

The Audio Signal Modulated with Digital Data
The Python program reads the hex file and writes an audio file in the WAV format. The digital clock and data signals modulate the two stereo 22.05 kHz audio tones. Figure 2 shows a sample of the audio input signals, a digital 1 is represented by a burst of 22.05 kHz that is 22 cycles long giving a time interval of 1 ms. The output from the signal conditioning unit is shown by Figure 3.

See Figure 2: Input to the signal conditioning unit.1
See Figure 3: Output from the signal conditioning unit.


The Signal Conditioning Unit
See Figure 4 Schematic diagram of the signal conditioning unit.

Figure 4 shows the schematic diagram of the signal conditioning unit. There are two operational amplifiers (U2 and U4) that require a positive and negative supply. The -5 V is derived from the +5 V by U1 which is an LM7660 charge pump. The amplifiers’ gain is set by the ratios of resistors R2/R1 for the clock channel and R7/R6 for the data channel The diagram shows the gains set at 10, depending on the available amplitude of your audio signal you might need to increase the values of R2 and R7. The LF411 has a minimum gain bandwidth product of 2.7 MHz, therefore it should be possible to increase the gain of these stages up to 120 at 22 kHz.

Figure 5 shows one burst of the 22 kHz audio, this signal is then half wave rectified and smoothed after the amplifiers by D1, R3, and C5 for the clock channel and D2, R8 and C7 for the data channel.

See Figure 5: A burst of 22 cycles at 22 kHz.

Following the half wave rectifying and smoothing there are two Schmitt triggers for each channel to clean up the signal to produce the the nice square wave. Figure 6 shows these two waveforms. The time constant for the smoothing resistors and capacitors is 0.22 ms. Note that it is important to use the component values stated in the diagram to ensure that the decay is not too long as to fail to turn off the trigger before the next pulse and not too short as to give multiple triggering during the on time.

See Figure 6: Schmitt trigger stage input and output.

The Schmitt trigger inverts the 0s and 1s so that is why there are two in each channel to re-invert them. The 74HC14 has 6 triggers available. The LED in the clock channel is optional but it is useful to give an indication that the audio is present. Finally the MCLR line is connected through switch S1 which is used to manually pull the pin down before the programming starts.

The Python Program
Python was chosen as it is cross platform and easy to modify. Only standard Python modules are used. Figure 6 is the program listing. The entry point to the program is the line “if __name__ == '__main__':”. While executing this block of code a number of functions are called. To understand the program you will need to refer to the standards for WAV files, hex files and Microchip data sheet ‘DS41390C’. Note that with Python scripts the indentation is critical.

See See Program 1: Python program listing

Using the Audio Programmer
Remember the Python program here is only for the 12F1822 and 16F1823 PICs. To get you started Program 2 is a simple LED flasher for the 12F1822 PIC. Connect an LED to pin 5 (RA2) through a resistor, see Figure 7. If you are downloading the Python program rather than typing it in yourself be careful to get the one for your system as text files have different line endings on Windows, Linux and Mac.

The program has to be launched from the command line as it requires one command line argument, the name of your hex file. For Windows users there is a batch file (Program 3) that is handy as it will open a terminal in the directory it is in.

To use the project follow these instructions:

Windows
  1. Ensure that Python 3 is installed
  2. Connect everything up.
  3. Un-zip the Python Program, the bat file and the the hex file in a directory.
  4. Click on terminal_here.bat file to open a terminal in the directory with the Python program in.
  5. In the terminal type python 1xF182x-audio_programmer-windows.py test_program-12F1822-windows.hex and press return.
  6. Check that test_program-12F1822-windows.wav has been created.
  7. Close the switch S1 to pull MCLR down to 0 V.
  8. Play this WAV file and note the brilliance of the LED on the conditioning unit. Some experimenting with the amplitude might be required or if you have a scope look at the inputs to the Schmitt triggers.
  9. Open the switch S1 to allow the PIC program to run.
  10. If the program has not been written to the PIC and the LED is dim during transfer you might need to increase the amplitude and if necessary increase the gain of the opamp stages.

Linux
  1. Ensure that Python 3 is installed.
  2. Connect everything up.
  3. Un-tar the Python Program and the hex file in a directory.
  4. Open a terminal window if you are not already using one and navigate to the directory with the files in.
  5. In the terminal type python3 1xF182x-audio_programmer-linux.py test_program-12F1822-linux.hex and press return.
  6. Check that test_program-12F1822-linux.wav has been created.
  7. Close the switch S1 to pull MCLR down to 0 V.
  8. Play this WAV file and note the brilliance of the LED on the conditioning unit. Some experimenting with the amplitude might be required or if you have a scope look at the inputs to the Schmitt triggers.
  9. Open the switch S1 to allow the PIC program to run.
  10. If the program has not been written to the PIC and the LED is dim during transfer you might need to increase the amplitude and if necessary increase the gain of the opamp stages.

Mac
  1. I have not tested this with a Mac but the files to use are ‘….mac.py and ‘….mac.hex’ in the download.

See Figure 7: Connect an LED to pin 5.

See Program 2: LED flasher program..
See Program 3: Windows batch file ‘console_here.bat’

Modifications
If you are familiar with Python you should be able to modify the code to use with other PICs. Start by understanding how this code works and study the data sheets. The same applies if you want to use high voltage programming.I hope you enjoy this different approach to loading programs into PICs.