Introduction:
This page contains some very basic and simple information about programming the PIC16F84. If you send me email, I probably won't reply. Hopefully this will help someone out someday.
Note: Circuit diagrams are give at the top of most C files.
Note: I've added a "putting it all together" section below. The code in that section is much more clean than in the preceding sections, so if you're not completely new at this, I'd skip right to that section.
What You Need:
A programmer. There are lots of options, and you can cheap out if you like. I purchased PICALL (assembled) from www.qkits.com. Qkits is an excellent resource for any electronic project. For more information about the PICALL programmer, visit www.picallw.com. I chose this programmer because it'll program just about anything. Once you've got the PICALL hardware, you need to download the latest software from www.picallw.com. If you're buying this programmer, do yourself a favour and buy a ZIF socket and a power source from the same distributor as the programmer.
A compiler. There are lots of options, and I decided to cheap out and went with PICC LITE from HI-TECH Software. You could just program in assembler, and I'd love to, but the truth is: 1. no one else will understand your code. 2. You won't understand your code two years from now. 3. as cool as assembler is, it's time you suck it up and write in c.
Hardware. ca.digikey.com or www.digikey.com if you're not in Canada is the best there is. They have everything you can imagine, for good prices and fast delivery. I placed an order at 2:00pm on Wednesday and it arrived in Kingston (shipped from Winnipeg) at 2:30pm the next day.
Examples. All the examples in this section are written in C for PICC LITE and compiled for the PIC16F84. All of these examples use the PICC LITE include pic.h. This just defines constant labels for the pins, and configuration fuses, etc. You should be able to port this code to another compiler without much difficulty. One final note is on the delay routines. They are for a fixed 4MHz clock.
Control a 16x2 Character LCD:
It's pretty simple. Your display probably has the standard 14pin or 16pin connector. Pins 15 and 16 are for the backlight, so they're not important. I think that most LCD's have a standard controller like the Hitachi HD44780, so there is just a simple protocol for talking to them. This example uses four bit mode, meaning we send a byte in two four bit chunks. This just saves I/O pins on your PIC. Anyway, first you initialize the LCD by sending some control codes, with appropriate delays, then you just give it bytes and it displays them at it's current position.
Here is some basic information about your LCD: Control Codes.
Below are the source files. You'll have to take the ".txt" off the file names if you want to compile them. The Delay routines are simple. The last two LCD routines are for custom characters. Your LCD probably has 8 chunks of CGRAM where you can specify custom characters for display. I wanted to draw this logo, so I specified the CGRAM accordingly in lcd_load_custom_characters(). Then lcd_draw_logo() sends them to the appropriate positions on the LCD. The LED in this project simply flashes on and off with the button presses. It is there only as a debugging tool.
main.c lcd.c lcd.h delay.c delay.h MAIN.HEX
At the top of lcd.c and main.c the pinouts for the lcd and your pic are given. The main program prints "Hello World!" and then waits for a press and release on the push button. It then prints "Hello Again!" on the second line of the display and waits for a press and release on the push button. Next it draws the logo.
Serial Communications (rs232) With A max232 Level Converter:
max232 is a chip used to convert voltage levels from the rs232 standard (+-12) to microcontroller levels (+-5). It also inverts the logic, because rs232 is inverted to the norm. You don't need to use the max232 for serial communications with a pic, but I went the trouble of figuring it out, so I thought I'd post it. If you're going to uses Serial communications without the max232 (next heading below) then read this discussion first, because I don't repeat it.
rs232 is dead simple. You have a start bit, then eight data bits then a stop bit. For 9600 baud, each bit needs a duration of 100us. That's all there is to it. Of course, there are many options, like handshaking etcetera that make it complicated. I used hyperterminal, that comes with every version of windows I've ever seen (Start >> All Programs >> Accessories >> Communications >> Hyperterminal). You have to set up the connection correctly: COM1, 9600 baud, 8 data bits, no parity, 1 stop bit, no flow control.
So to send a byte, all you do is hold your pin low for 100us (the start bit), then put each byte on the pin for 100us, then put your pin back to high for at least 100us (the stop bit). This of course requires that when you're not sending anything, your transmit pin must be high; therefore make sure it's initialized to high at the beginning of the program for at least one complete send cycle. To receive bytes, simply watch the receive pin until it goes low (the start bit), then wait for about 125us (which definitely gets you into the first data bit. Then all you do is read the pin every 100us to get your eight bits. When receiving, you don't really care about the stop bit.
Below are the source files. At the top of main.c, there are pinouts for the whole board. I used a mouse cord for my serial connection. I just grabbed an old serial mouse for free, busted it open and ripped the end of the cord off the mouse board. The pinout for the serial cable is given as best I could at the top of main.c. You only use three wires, so you should be able to figure it out. The basic layout is: PC >> serial cable >> max232 >> pic.
main.c rs232.c delay.c delay.h MAIN.HEX
The main program is really simple. It prints "Hello: " to the terminal, then echoes whatever you type.
Serial Communications (rs232) Without A Level Converter:
It seems that the pic is capable of driving sufficient current and voltage to pass the logic thresholds of the rs232 protocol without any extra level conversion. This causes only one problem: the rs232 standard uses inverse logic. Consequently, you can use pretty much the exact same circuit and software as above, but exclude the max232 chip, and invert the levels in your code. I'm not sure if it's necessary, but I included 10k resistors on the pic's transmit and receive pins, just to be safe.
Below are the source files. At the top of main.c, the pinouts are given. The basic layout is: PC >> serial cable >> pic.
main.c rs232.c delay.c delay.h MAIN.HEX
The main program is really simple. It prints "Hello: " to the terminal, then echoes whatever you type.
Putting it all together:
The following application combines rs232 and LCD control in the simplest way I could imagine. The code was initially very similar to the above projects, but I have removed all the fat and redundancies. That implies that the above code isn't as clean as it could be, so if you're feeling brave, you might want to skip all of the above and head straight to this project.
Here are some high resolution photos of the project: ( 1 2 ). The green board is the PICALL programmer, and the blue board is the project. The red and white switch block allows me to safely program in place (also called ICSP or ICP). When the switches are "down" (as in the pictures) Vss, Vdd and reset pins are connected normally (as in the project spec). When the switches are "up" Vss, Vdd and reset pins are connected to the yellow, black, and white wires of my ICSP connector leading to the PICALL programmer (the blue wire is unused). Note that the ICSP connector's green and red wires connect to RB7 and RB6, and these pins are unused in the project. This particular switch block is a 76stc04, where each switch controls three pins and 2 of the 3 are connected in a given position. If you want to simplify things, I'd suggest you leave out the ICSP stuff until later.
Other than that, we have reset hooked up to a pushbutton (top left), we have a simple rs232 connection using two resistors and an old mouse cord (bottom left), and we have the LCD and it's contrast controlling POT (right). You can't see some of the LCD wires because the run under it, but it's all specified in diagrams in the C file.
You need to wire your board, program your chip, and open a hyperterminal connection ([Start >> All Programs >> Accessories >> Communications >> Hyperterminal], [COM1, 9600 baud, 8 data bits, no parity, 1 stop bit, no flow control]). Then press and release the reset button. "Hello:" should appear in Hyperterminal, and the LCD should be blank. The first two characters you type in hyperterminal will echo back, and then the LCD will display the custom logo. The next two characters you type in hyperterminal will echo back doubled, and the LCD will go blank. The next character you type into hyperterminal will echo and appear on the LCD. The next character you type will cause the program to loop back to the beginning, effectively a reset.
Below are all the source files necessary for this project. Only main.c includes pic.h and I believe that the only dependencies in the entire project on that file are constructs: PORTA, PORTB, and the CONFIG fuses