Monday, April 20, 2009
simple Code Examples (Assembly language)
;*****Set up the Constants****
STATUS equ 03h ;Address of the STATUS register
TRISA equ 85h ;Address of the tristate register for port A
PORTA equ 05h ;Address of Port A
COUNT1 equ 08h ;First counter for our delay loops
COUNT2 equ 09h ;Second counter for our delay loops
;****Set up the port****
bsf STATUS,5 ;Switch to Bank 1
movlw 00h ;Set the Port A pins
movwf TRISA ;to output.
bcf STATUS,5 ;Switch back to Bank 0
movlw 02h ;Set up our w register with 02h
;****Turn the LED on and off****
Start xorwf PORTA,1 ;Toggle the LED
;****Add a delay
call Delay
;****Now go back to the start of the program
goto Start ;go back to Start and turn LED on again
;****Here is our Subroutine
Delay
Loop1 decfsz COUNT1,1 ;This second loop keeps the LED
goto Loop1 ;turned off long enough for us to
decfsz COUNT2,1 ;see it turned off
goto Loop1 ;
return
;****End of the program****
end ;Needed by some compilers, and also
;just in case we miss the goto instruction.
Friday, January 2, 2009
PIC Programming
Here are 4 steps to help you get started with PIC programming.
Note: I haven't used this programmer myself in years. A good and simple programmer schematic and Linux software can be found at http://www.iet.unipi.it/~luigi/pic.html. If you want to use it, replace step I and half of step II appropriately. Please refrain from asking me questions about that site.
I had to remove some of the precise links to Microchip's site, because they keep moving the files around.
1. Build the programmer
This programmer was originally intended to be used with the PIC16C84 processor, but Juho Cederström pointed out that it works with the PIC16F84 as well. It can be built with only a few components, and connects to the COM port of a PC. Here is a schematic of it. The programmer can be integrated to your project's circuit or a separate programmer can be built. A zero insertion force (ZIF) socket is useful in a separate programmer.
2. Get the software
Download the programmer software for DOS. It should work in a Windows 95/98 DOS box too. You also need MPLAB to compile your program to HEX files used by the programmer software. With MPLAB you can also simulate the PIC on your computer.
3. Print the documentation
Printing the user's manuals for the software and the PIC16C84 datasheet makes learning much easier, because you can easily flip through them while coding ASM files. The documents can be found at Microchip's site.
4. Get example code and build a test circuit
The best way to learn is by studying an example, changing it a little, and then running it with the simulator or a test circuit to see what happens. The PIC datasheet contains all information needed for building a test circuit, and reading it is a good way to learn about how the PIC works, too.
Wednesday, December 24, 2008
PIC/PLC Programming
Many people would like to get started with microcontrollers, but don't know how to begin. That's what this page is all about -- how to get started. With a microcontroller you can do things that would be difficult to do with discrete logic. Besides its power, you can easily "change your mind" with a microcontroller -- something you can't do with discrete logic.
The PIC isn't for every project. It won't replace a PC, or even a larger processor. But for many jobs it is just the right size, inexpensive, and doesn't require much in the way of support hardware.
What are some things people have done with a PIC?
| Morse code keyers | |
| Robots | |
| Data logging instruments | |
| Phone dialers | |
| Clocks and timers | |
| Much, much more |
There are several steps you need to take to get started:
1) Select a PIC
2) Assemble the tools you need
3) Learn to program
In truth, if you just want a learning experience, you can download a free simulation environment from Microchip and practice programming without spending any money or buying any chips at all. Of course, its just a simulation, so your pretend program won't do anything, but you can still learn the basic concepts.
UPDATE:
Since this was written, two things have changed. First, Microchip's MPLAB has several new versions. The 5.70 version, which is still available on Microchip's Web site is the version used for this tutorial. Many people still use it because it supports several popular development tools that the newer version (version 6.0 and above) doesn't support. On the other hand, if you get the newest version, they are fairly similar and you'll probably have no trouble figuring out the differences. The PIC18F tutorial, by the way, shows you how to use the latest version, so you might want to browse through it even if you don't want to learn that part in particular.
The other big difference is that the PIC16F84 is pretty much obsolete now. The PIC16F628, for example, is much more powerful and less expensive. And it is almost completely compatible with the older chips. So if you are starting out, forget the 16F84. Sometime in 2004 I will try to update this tutorial with the newer software (or perhaps do a separate tutorial based on this one).