&& The Pic 18F Microcontroller ------------------------------ This is a booklet about programming the PIC 18f family of microcontrollers using Linux. A full open-source tool chain for linux exists for the 18F family. == tools .. piklab - a good IDE for programming .. sdcc - a c compiler for the 18f family among others .. gputils - a set of tools compatible with 'microchip' tools .. gpasm - an assembler for the pic compatible with mpasm .. OVERVIEW The Pic Microcontroller is manufactured by Microchip. More than 8 billion chips have been sold (2010). The Pic 18F is a risc instruction set machine with approximately 77 instructions. All chips in the 18F family should be compatible in terms of the instruction set (but not functionality). OTHER MICROCONTROLLERS Here is a quick list of popular microcontrollers @@ 8051 Originally designed by Intel now manufactured by various comanies (including Atmel) @@ 68HC11/ 68HC12 A formerly very popular microcontroller. Complex instruction set. @@ Atmel AVR Used on the 'Arduino' open-source development boards INSTALLING TOOLS * install a tool chain for programming a pic 18f4520 in the c language >> sudo apt-get install piklab gputils sdcc sdcc-doc If you are running piklab in a gnome based desktop you may have to run piklab as the 'super-user' with "sudo piklab" CONFIGURING ICD2 PROGRAMMER The ICD2 Microchip Programmer requires firmware in order to run If the MPLAB software is installed on the same computer (in a Linux/Microsoft dual boot system), then the firmware for the ICD2 programmer can be found in the folder >> ../Program Files/Microchip/MPLAB IDE/ICD2 * mount the windows drive within linux first COMPILING SDCC .... Sdcc is the "Small Device C Compiler" which is an open source C language compiler for microcontrollers. * view the help file for the sdcc c language compiler >> evince /usr/share/doc/sdcc-doc/sdccman.pdf * view the included header file (for port aliases etc) >> less /usr/share/sdcc/include/pic16/pic18f4520.h * compile 'first.c' in the current folder to object code for an 18F4520 >> sdcc -mpic16 -p18f4520 -V --debug -c first.c * compile 'first.c' in this folder to object code with no debugging info >> sdcc -mpic16 -p18f4520 -V -c first.c * compile 'first.c' to object code for a pic18f4520 >> sdcc -mpic16 -p18f4520 -V --debug -I/home/matth3wbishop/pic/proj/ -c first.c * compile an object file to a hex executable file >> sdcc -mpic16 -p18f4520 -V --debug -Wl-c -Wl-m -I/home/matth3wbishop/pic/proj/ -ofirst.hex first.o * compile ./first.o to an executable (.hex) file with no debugging info >> sdcc -mpic16 -p18f4520 -V -ofirst.hex first.o * a bash function to compile an object file to an executable >> pcc() { f=$1; sdcc -mpic16 -p18f4520 -V -o ${f/.o/.hex} $f; } * a function to compile from source (.c) to an executable (.hex) ---------------------------------------------------------------- pcc() { f=$1; sdcc -mpic16 -p18f4520 -c $f || return 1 sdcc -mpic16 -p18f4520 -V -o ${f/.c/.hex} ${f/.c/.o}; } ,,, The bash function above will exit (return) if the process of compiling to object code was not successful. * a bash function to compile a pic 18F c file and upload to the mcu -------------------------------------------------------------- pup() { f=$1; sdcc -mpic16 -p18f4520 -c $f || return 1 sdcc -mpic16 -p18f4520 -V -o ${f/.c/.hex} ${f/.c/.o} || return 1 sudo piklab-prog -c program -p icd2 -t usb -d 18F4520 ${f/.c/.hex} } ,,, * a function to remove the files generated by the 'sdcc' small c compiler >> pclean() { rm *.hex *.o *.lst *.asm *.adb *.cod *.cof *.map; } This could be dangerous if you have actual assembler programs in you folder. GPASM The gpasm tool (part of the gputils package) is an assembler which used a syntax compatible with the mpasm assembler from the MPLAB software suite from Microchip. The mplab software is free but only runs on Microsoft operating systems. * compile a program >> gpasm prog.asm * simulate >> gpsim * similate for the '16f627' chip >> gpsim -pp16f627 -s toggle_led.cod toggle_led.hex UPLOADING TO THE MICROCONTROLLER Uploading a program to an MCU is known as 'programming' the chip. In order to do this you need a special 'programmer' harware device. In this case I am using the ICD2 device from microchip * show all the Microchip Pic chips which the piklab-prog tool supports >> piklab-prog --device-list | less CONNECTING .... * connect to the icd2 programmer >> sudo piklab-prog -t usb -c connect -p icd2 -d 18F4520 * make a bash 'alias' to connect to the ICD2 programmer >> alias pcon='sudo piklab-prog -t usb -c connect -p icd2 -d 18F4520' UPLOADING .... * upload the executable 'first.hex' to the icd2 using the usb port >> sudo piklab-prog -c program -p icd2 -t usb -d 18F4520 first.hex If this worked, you should see the message 'Programming successful' as the very last message in the terminal window. To actually see the program running you have to take out the connection between the ICD2 programmer and the pic demo board. The 'busy' led on the ICD2 programmer should flash as the device is being programmed. * a bash function to upload (program) a pic 18F4520 via ICD2 and usb >> pprog() { sudo piklab-prog -c program -p icd2 -t usb -d 18F4520 $1; } The commands seem to have to run as the super-user in order to gain permission to reset the usb port. But I dont know why exactly ERRORS WHEN USING PIKLAB PROG * the icd2 is probably not plugged in to the usb port >> Error: USB Port: Could not find USB device (vendor=0x04D8 ...) Solution: Plug the usb cable from the computer to the Microchip ICD2 programmer in at both ends * the demo board with the pic18f chip has no power >> Warning: Self-test failed: Target Vdd=Target ... Solution: plug the battery into the 'picdem' demo board (which should be connected to the ICD2 programmer via a sort of telephone cable LCD == configuration of the lcd on the PICDEM 2 Plus demo board .. RD0 - DB4 .. RD1 - DB5 .. RD2 - DB6 .. RD3: DB7, Busy Flag, 1: Busy .. RD4: RS, Register Select 0: Control, 1: Data .. RD5: R/W, 0: Write, 1: Read .. RD6: Strobe, low to high to low .. RD7: LCD Power, 0: Off, 1: On .. * a trick to write 4 bits of data to the lcd >> PORTD = (dat & 0xF0) >> 4 The code above wont work on a lcd board where the control and data lines are on the same port (portd for the picdem 2 plus demo board for example) C LANGUAGE Input ports are refered to as LATA LATB LATC etc. This stands for latch a etc. Output ports are refered to as PORTA PORTB etc. Each latch and port has 8 bits (except A?). BIT MANIPULATION * read a single bit from input port 'D' >> busy = LATDbits.LATD3; * assign a value to a single output pin of port D >> PORTDbits.RD6 = 0; * put the most significant bit (bit 8) of 'dat' into bit 4 of port d >> PORTDbits.RD3 = (dat & 0b10000000) >> 7; * put upper 4 bits of the character 'dat' into lower 4 bits of Port D >> PORTD = (PORTD & 0xF0) | (dat >> 4); The upper 4 bits of port d are unchanged SIMULATION Simulation allows the programmer to work without an actual pic chip or board. Gpsim can be used for this purpose. It appears not to work for the pic 18F4520 INPUT AND OUTPUT The pic 18F4520 uses 'ports' and 'latches' for input and output * set all pins on Port D to output >> TRISD = 0x00 * set pins 0 and 2 on Port D to output, the rest to input >> TRISD = 0b11111010 * set Port D to all output and write 10 to it --------------------------------------------- TRISD = 0x00 PORTD = 0x0A ,,, * strangely you can also write a value to 'LATD' etc >> LATD = 0x01 GOTCHAS * 'data' is a keyword for the sdcc compiler * sdcc requires that all _asm _endasm; end with a colon ; >> _asm nop _endasm; ##(correct) >> _asm nop _endasm ##(Wrong!) COMPILER ERROR MESSAGES * the function name is spelt wrong or not defined in the source code >> function 'ftest' implicit declaration TERMINOLOGY AND ACRONYMS @@ mcu microcontroller unit. A microprocessor which also includes certain 'periferal' functions such as analog to digital (A/D) timers, pulse width modulation etc @@ A/D Analog to digital. Converts a voltage level between 0 and 5 volts to a number. In the pic 18F4520 the conversion is 10 bit which means the scale is 2^10 (2^11?) @@ PWM pulse width modulation. Modulating the duty cycle and period of a digital square wave. Used for dimming lights, controlling stepper motor arms, dc motor velocity, producting tones on a synthesiser etc. @@ Duty Cycle The high part of a square wave @@ Period @@ Simulation simulate a pic chip in software ANALOG TO DIGITAL CONVERSION BOOKS * Pic Microcontroller: An introduction to Software and Hardware * interfacing. Author "Huang" This is an extremely good book concentrating on the Microchip Pic 18F family and covering both assembly language and C. The architecture of the family is well explained along with the on-chip 'peripheral' functions, such as the various timers, Analog to Digital Conversion etc.