|Darren's PIC Microcontroller Page|
|Welcome to my microcontroller page. Pic's are one of the easiest microcontrollers to work with. You can program them in Assembly language, C or even Basic. Personally, I use Assembly language because Microchip (the company who manufacture them) provide the assembler for free.
PIC's use RISC architecture so for someone who is used to Von Neuman architecture (CISC), there is a slight learning curve. CISC architecture is the normal format shared by most microprocessors and has a relatively large number of instructions. There are a lot of other differences between RISC and CISC but the instruction set is the most important from a programming point of view. In RISC processors, the instructions for comparing data don't really exist. To perform any conditional jumps, you have to use commands which compare bits such as "BTFSS STATUS,Z" which skips the next instruction if the zero flag is set. In a CISC cpu, you can use a something like:
CMP 0xF8 ;Compare accumulator with literal 0xF8
BNE QUIT ;Exit if they don't match
In a PIC, you have to go about it a little differently since there are no conditional jumps or byte comparisons. Here is the above pseudo-assembly language in PIC language:
XORLW 0xF8 ;XOR contents of W register with 0xF8
BTFSS STATUS,Z ; Skip next instruction if Z flag is set
GOTO QUIT ; Z Flag must not be set so goto QUIT
the command "xorlw" exclusive or's the literal (0xF8) with the "W" register and places the result in the "W" register. If the result is zero, the "Z" flag will be set. If you are not familliar with the XOR function, learn it!!! It is one of the most usefull logic operations . If you xor two identical numbers, the result is zero, therefore, if the two numbers are the same, the Z flag will be set. The "btfss" instruction means BitTest F, Skip if Set. F refers to a memory location and in RISC architecture, most registers are addressable in memory. the status register resides in address 0x83 and Z is the third bit. the "STATUS" and "Z" in the command are definitions set up automatically in the include files which translate to these numbers. If the conditions of a btfss are met, the next instruction will be skipped. It seems a little back-to-front since it's like having an "If-Then Don't" instruction instead of "If-Then" but once you are used to it, it actually makes sense. It takes some time and patience to get your head around the different programming philosophy involved here but in the end, the code is easier to understand. I found conditional branching (as in the above example) to be the most difficult concept to get used to. To help get my head around it, I think of the btfss and goto as being like a single command (in two parts) and the BTFSS works backwards (skip if Set causes the goto to execute if the bit is Cleared and skip if Cleared causes the goto to execute if the bit is Set.
Risc has only one general purpose register (equivalent to the accumulator register in a CISC system) and requires data to be moved to and from memory for storage/processing. Although it seems more complicated, it is actually simpler because variable names are more meaningfull and easier to track mentally that A/X/Y. In the end, RISC is much more logical in my opinion. Another important factor is the instructions all take either one or two cycles to complete. The instructions which branch (Return, Goto, Call, Retlw, Retfie) all take two cycles. The conditional instructions (Decfsz, Incfsz, Btfss, Btfsc) all take one instruction cycle if the condition is not met or two if the conditional is met and a branch occurs. All the rest of the instructions take only one cycle. This makes calculating of timing much easier and code optimization is easier aswell.
Getting started and setting up a development environment:
When working with microcontrollers, your development environment is extremely important. When I started on Phillips microcontrollers, my first project was test boards. I built a board with turned pin sockets to run the chip, including a 5v regulator on the board. Wire from phone/network cables with a single solid core could then be used for point to point wiring from the test board to other boards with switches/ LED's and driver transistors. I had a nice little collection of boards.
When I moved on to PICS, I went for a tidier approach and made up a board with a socket for 18 pin PICS and 8 pin PICS using turned pin sockets to access the port lines. Also, it had a 5v regulator with sockets available to connect straight to the power supply. A MAX232, again with sockets to connect wires to it's input/output pins allows PC serial port assess to be added to a project by plugging in a few wires. A ten LED block along with series resisters allows the status of port pins to be monitored simply by plugging wires in. An 8 way dipswitch allows 'user interraction aswell. Finally, a ULN2003 darlington driver chip allows up to 7 external loads to be switched. And all of this fits on a board 90x80 centimetres in size. The board was bolted to a small slope front project case and took up a little less than half of the space on the cover. The slope front case allows easy access to the board whilst allowing various connectors to be accessed from the rear of the unit. The board has several turned pin sockets connecting into the case through wires soldered to the back of the board which go through holes in the case and connect to internal parts leaving none of the wiring visible. On the back of the case, there is a power socket for 12v AC/DC which connects to a bridge rectifier, a power switch, a power out socket (5v for other modules to connect to), a DB9 socket for RS232 and a PC keyboard socket which is connected through diodes/resisters to sockets on the main board. Also on the top of it, there is a 16 character, 1 line LCD module connected using Myke Predko's famous 2 wire LCD interface with the board housed internally and there is also a 12 key keypad. By the way, check out Myke's website, there's a heap of good information and tutorials.
Recently, after getting sick of switching plugs between my PC and my PIC programmer/development board, I decided to add another slope front case to my development setup. The second case has my programmer mounted on the front with a rotary selector switch next to it. On the back, two RS232 cables come out, one goes to my PC and the other goes to the development board. This addition gets the programmer board off the bench and away from solder splatter and allows me to switch between the programmer and development board without pulling cables.
I have now started experimenting with the bigger PIC's with the purchase of my first 16F877. I made up a board which has a turned pin socket for the chip (ZIF sockets are outside of my price range) and sockets on each IO pin. Also, headers and resisters have been fitted to give me jumper configurable pull-up resisters. There was a bit of space on the bottom of the board so I filled it with a home made logic probe using a 12c508. Some day, I'll get a case for this board aswell. but for now, it's held up off the bench by four bolts in the corners.
I've always found that the golden rule should be to get your environment right first so that when things don't work, you don't have a confusing mass of wires twisted together adding to the frustration. Also, have the development gear right next to your computer. From my keyboard, my soldering iron and development gear is all within reach.
Anyway, I have started putting some of my projects on this website so take a look at them and download them if you want to try them out.