I've been suprised by the dearth of examples for programming Microchip PIC® processors in SDCC, given there's been continuous effort in improving it's support since about 2001 or so. As such, I am placing the things I've managed to create or find, here.
Mostly I've been experimenting with the PIC16f628a processor. I like it because it has, among other things:
- An internal oscillator that runs at 4MHz or 48KHz -- no more crystal setting madness, and you still get to slow your programs way down when they get uppity!
- A UART. For real. Set it to 9600 BAUD and talk to your computer or Serial LCD.
- It works reliably with my JDM-style programmer, even under Linux®. The exact programmer I use appears available here, but the JDM is an open design -- buy it or build your own, the PIC doesn't care. :) Of course you'll have a hard time finding all the fiddly zeners and transistors so personally I think you're better off buying the whole programmer -- it's got a nice ZIF socket, and comes with useful Windows softare too. Note you need a real serial port, the crap they put in laptops doesn't get enough voltage and the counterfeits you get in USB don't have the right control pins.
Many of these examples are still easily applicable to the 'standard' PIC®, the PIC16F84.
My workstation is as follows:
- A desktop computer running Gentoo Linux, with SDCC and picprog installed.
- A nice digital logic trainer breadbord that the local university idiotically threw away.
- An old 2-channel 20MHz analog oscilloscope that the local university idiotically threw away.
- A logic probe from the era when Radio Shack sold anything but appliances.
- A 200-watt pre-ATX computer power supply.
- Parallax serial LCD for debugging output.
- Proto-18 PIC design board.
- Proto-18 board, which includes a built-in MAX232 chip and serial connector for easy connecting to computer
- tsmtypes.h -- an include file I made to make it obvious what types are what size. A Uint16 is an unsigned 16-bit type, an Sint8 is a signed 8-bit type, etc. Most of my PIC® programs use this.
- 0001-test.c -- an examle source file modified from an example gleaned from here. It is extremely simple. It counts, and displays the output on ports A and B both. Look for successively larger squarewaves on B0, ..., B7.
- 0002-interrupt.c -- this is a quantum leap for me, a working pic example that uses interrupts! First time in 10 years I managed to get those to do something. The program sits there and does nothing while interrupts fire periodically and increment PORTA. Based on code by Mac Cody on this SourceForge thread.
- 0003-intclock.c -- the PIC16f628a has an internal clock that can be set to one of two frequencies -- 4MHZ, or 48KHz. This program is a modified version of 0002-interrupt.c that alternates between 4MHz and 48KHz every interrupt as well as twiddling PORTA. Watch for the frequency changes on CLKOUT. This example, unlike those above, cannot be modified to work on the PIC16F84, since the PIC16F84 has no internal clock! Note that the clock speed bit doesn't do anything if the PIC is configured to use any other clock source than it's internal oscillator!
- 0004-uart_tx.c -- Configures the USART to asynchronous 9600 N81 and transmits the string "Q! Q! Q! Q! US! US! US! US!" over it. Thoroughly useless, but a decent example and actually works for me. I use the putty terminal program to read the serial port on a Windows PC.
- 0005-uart_tr.c -- Configures the USART to asynchronous 9600 N81, then repeats whatever you type back at you. Very close to 0004-uart_tx.c but transmits AND recieves.
- 0006-interrupt.c -- A simple example using B0 as an interrupt pin. Also illustrates the sleep instruction. Adapted from an example in assembly here.
- 0007-interrupt.c -- A simple example illustrating how the PIC's interrupt-on-B3...B7 feature works. It is modified from 0006.
- 0008-vref.c -- A simple example illustrating how the PIC16f628a's internal voltage reference works. Depending on the input on A0, it sets the voltage reference, output on A2, to either minimum or maximum value.
- 0009-eeprom.c -- An example showing how to write to the internal EEPROM memory of a PIC16f628a. It will write a string into EEPROM memory, which you can read back with your PIC programmer to see whether it actually worked.
- 0010-eeprom_rd.c -- An example showing how to read values from the internal EEPROM memory of a PIC16f628a. Put whatever byte you want at address 0x00 in EEPROM using your PIC programmer, and this program will display that byte on PORTB.
- 0011-ustepper.c -- By request of a denizen of the Reprap Builders Blog, I've put up the source to this unipolar stepper tester.
- 0012-keyboard.c -- simple interfacing of a PIC16f628a to a PS/2 keyboard.
- 0013-keypad.c -- deluxe keyboard example, reads in decimal digits from a numeric keypad to produce ASCII or raw bytes on the serial port.