Sample Programs for the PIC Microcontroller
The following programs are simple examples for programming 8-bit PIC
microcontrollers. A good reference is the book "Designing Embedded
Systems with PIC microcontrollers: Principles and Applications"
by Tim Wilmshurst. The datasheet for the PIC is also a good reference
for information on the bits for each memory address register in the chip.
The simplest way to program PIC microcontrollers is using a bootloader.
Modtronix makes two nice boards: the BLC876A (around $35) and the BLC877A
(around $45). Each of these boards comes with microchip's bootloader
pre-programmed on the chip. The following example codes in C were
tested on the BLC877A board.
- sertest.c: This program can be used to test
the serial transfer of data. The code simple sends the unsigned char
0 to 255 over and over. On the PC side, one can test if you are
receiving the unsigned char correctly.
- adctest.c: Analog to Digital test. This program
samples pin A0 twenty times and sends back the result via serial. We
were able to perform Analog to Digital at a rate of around 70 KHz using
the PICC-lite complier. Using assembly or a more optimized complier,
one can improve the sample rate.
- chipatwood.c: This program can be used
for our Atwood Machine interface in our first year mechanics lab.
It measures the time between blocking and unblocking of a laser
gate. The interupt service routine part of the code, called when
a timer overflow occurs, is used to keep track of the total time
elapsed.
One loads the programs as follows:
1. First you need to compile the C code to produce a *.hex file for
the chip. To do this you can download the free software:
MPLAB-IDE from microchip, and the HI-TECH PICC-lite compiler. The
PICC-lite complier is ideal for students to learn with. It produces
a *.hex file that is not optimized, but allows you to program the
chip and test your programs. Their PRO version is more professional.
2. After you have installed the compiler software, build the *.c program
to make a *.hex file.
3. To load the program with the bootloader pre-installed on the chip,
one needs to download the free "bootloader PC software" from Modtronix.
4. After you have installed the bootloader software, connect the serial
cable to the board and run the bootloader software. Load your *.hex file,
and hit the reset on the board (i.e. ground the MCLR pin). When you see
the "OK", the chip has been programmed.
Reading the Serial Output from the Chip
An excellent web site for reading and writting to the serial and parallel
port is "www.beyondlogic.com". Below we list a sample program, which
runs in Linux, to read the serial port.
- picserin.c: This program is written in C
and runs in Linux. It reads the serial port 24 times in succession and
prints the bytes to the screen. We just use it as a test to see if
the PIC chip is transmitting serial data correctly.
Timing on the PC via Parallel Port
One can do fairly accurate timing for TTL input using the parallel port.
Below, we list some codes that we use (or have used) in our classroom for
photogate timing.
- pend.c: This program is written in C and runs
in Linux for our pendulum experiment. It checks the parallel port for a
change on pin 10. When a change occurs, a time stamp is obtained using
a call to rdtsc.
- atwoodxpar.c: This program is written in
C and runs in Linux for our atwood machine experiment. It checks the parallel
port for a change on pin 10 on the parallel port. When a change occurs, a
time stamp is obtained using a call to rdtsc. The data can be graphed on
the monitor via the X11 library. To compile use gcc -lm -lX11 atwoodxpar.c
Siegel's Home
Page
Physics Department|
College of Science