Intro to Programming PIC Microcontrollers in Linux

From OnnoWiki
Revision as of 06:32, 7 September 2010 by Onnowpurbo (talk | contribs) (New page: Sumber: http://www.micahcarrick.com/pic-programming-linux.html I first tried switching to Linux back in 1996-- however, grew frustrated after 2 agonizing weeks of trying to configure ppp ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Sumber: http://www.micahcarrick.com/pic-programming-linux.html

I first tried switching to Linux back in 1996-- however, grew frustrated after 2 agonizing weeks of trying to configure ppp to connect to the internet on my speedy 28.8 modem. I continued using, and developting software for, Windows. I grew more and more frustrated with Windows until I reached the breaking point with the new spyware/virus epidemic. I once again attempted the switch to Linux, and to my delight, found it quite painless. I found the install to be VERYeasy and all the drivers and applications I would need were free and readily available-- if not already built into my Linux distribution (at the time that was Fedora Core 1, and is now Fedora Core 3). Everything was working. I was developing my web applications with Oracle and JSP on my local system, I had office, all the messaging clients, an image editor with all the functionality I could ever want-- and this was all on a laptop! But there was one, small thing missing. I didn't have MPLAB and all my great software for electronics and PIC programming. Eh! However, after quite some time I was able to get up an running with my PIC programming. I haven't booted into my Windows XP system in over 6 months. Now I pass on the information to you. Before going any further, I would like to point out that PIC developement on Linux is, as of yet, nowhere near as intuitive and thorough as it is on Windows using MPLAB. However, for the hobbyist such as myself, it's more than sufficient. And you don't have to rely on Windows! Target Audience This article is inteded for people who are familiar with PIC programming. It's not a beginners guide to programming PICs, but not far from it. It's intention is to get you setup and started in programming, compiling, and simulating your PICs in a Linux environment. Ideally you have a basic knowledge of PIC programming. Ideally you have a basic understanding of using Linux and installing from source-- but if not, I do go over it step-by-step. You shouldn't be afraid of issuing commands on a command-line. But then again, if you're someone who's reading an article on PIC programmin in Linux, I doubt you'll have any problems with that :) I am no expert in Linux nor PIC programming. I just wrote this article to compile what I've learned in the last few months as a result of my own endevours in PIC programming and Linux. I hope it helps. I have done all the steps of this article on a Fedora Core 2 installation with KDE and also with a Fedora Core 3 installation and Gnome. Resources Here are some of the sources from which I obtained my information, and I reccomend you get familiar with if you're going to be developing your PIC applications in Linux.:

   * GNUPIC Project - Project dedicated to conolidating PIC Linux websites. Most of the other sites are derived from this one site. I'm not going to list all the PIC Linux projects separately as they can all be accesed through the GNUPIC Project. The mailing list is also a great resource for PIC and Linux information.
   * LinuxQuestions.org - Forums for Linux. I ask TONS of questions here, especially when getting strange errors. Great resource when you're stumped on installation and/or configuration issues.
   * Programming & Customizing PICmicro Microcontrollers - This is a book about programming PIC micro's written by Myke Predko. It is truely a great book on programming the PICmicros. I've read about 4 different books and nobody handles the subject better than Mr. Predko.

predko_book.jpg Installing GPUTILS - The Linux PIC Assembler and Linker GPUTILS is a collection of cross-platform PIC utilities designed to emulate the syntax and functionality of Microchip's MPLAB software for Windows. It includes gpasm, gplink, and gplib which are compatable with mpasm, mplink, and mplib respectively. These utilities are the essentials for programming PICs on Linux. You should download and read through the manual. Many readers are already quite familiar with installing software on Linux and can therefore skip down to the section where we write our first PIC program, toggle_led.asm. However, if you have never installed linux software by "compiling from source", then read on for step-by-step instructions on installing the software. Downloading GPUTILS You can download the latest version of GPUTILS at from Sourceforge. At the time of writing, the current file was gputils-0.13.1.tar.gz. Note that I am using the tar.gz file and not the .rpm file. Make sure you know where you downloaded the file to. Extract the Tarball You'll need to extract the files in the archive. To do this, open up a terminal, navigate to the directory where you downloaded the file to, and issue the following commands (assuming your file was version 0.13.1):

tar -xzf gputils-0.13.1.tar.gz

cd gputils-0.13.1

That will extract the files into a new directory named gputils-0.13.1 and then set gputils-0.13.1 as the current working directory. Configure, Make, and Make Install As with most software for Linux, it's installed by performing a configure, a make, and a make install. This is pretty much the same for all the software packages we'll be installing. Each of these steps can take a few minutes, during which a bunch of information scrolls across the screen. Unless you see some error messages you should be okay. If you do see some error messages, either paste those messages into google or post a message on Linuxquestions.org for quick answers and help. The installation will install the GPUTILS to /usr/local/share/gputils. Also, when issuing the su command, you will need to know the root password. To perform the installation, issue the following commands (assuming your file was version 0.12.4):

./configure

make

su

make install

exit

Test the InstallationBefore we go an delete the files we no longer need, let's test that the installation went okay. Issue the following commands and ensure each one outputs the version:

gpasm -v

gplink -v

gplib -v

If each of those commands echo the version of the utility as opposed to outputing an error about a command not being found, then the installation was most likely a success. You can then return to the direcory where the original file was downloaded and remove both it and the make files by issuing the following commands:

cd ..

rm -dfr gputils*

Installing GPSIM - A PIC Simulator for Linux GPSIM is a simulator for the PIC microcontrollers. That means, you can debug the programs you write, and actually "step" through the code one instruction at a time. This allows you to view the changes in registers, ports, memory locations, etc. after each instruction is executed. GPSIM also includes a realtime stopwatch and a breadboard window so you can watch the output pins change based on the code you write. Most of this is very similar to what you get with MPLAB, the IDE from Microchip for Windows. Downloading GPSIMYou can download gpsim from the gpsim website. At the time of writing, the current version was gpsim-0.21.2 and the current version of gtk+extra was 0.99.17. The two files are gpsim-0.21.2.tar.gz and gtk+extra-0.99.17.tar.gz respectively. Download both of these files. The gtk+extra is needed for the GUI for the simulator. Once the files are downloaded, you'll need to open up your terminal program to navigate to the directory where the files were downloaded. Extract the Tarball, Configure, Make, Make Install. Make sure you install gtk+extra first! Since we already stepped through the installation process in the last section, I'm going to combine the steps in this section. Also, keep in mind we're installing both gtk+extra and then gpsim. For gtk+extra, issue the commands:

tar -xzf gtk+extra-0.99.17.tar.gz

cd gtk+extra-0.99.17

./configure

make

su

make install

exit

cd ..

And for gpsim issue these commands:

tar -xvzf gpsim-0.21.2.tar.gz

cd gpsim-0.21.2

./configure

make

su

make install

exit

cd ..

Test the InstallationWe can test the installation by executing the command:

gpsim

The GUI should launch and echo a bunch of feedback to the command prompt window. You can ignore that feedback for now, however, you should get the GUI window which looks like the one below.

GPSIM Main Window If you see that window, the installation was a success and we can close it by clicking the 'Quit gpsim' button. We can then remove the make files by issuing the following two commands (assumes you're in the directory where the files were installed):

rm -dfr gtk+extra*

rm -dfr gpsim*

Compiling a Simple PIC Program Now, as with any linux command, executing the command 'gpasm' without any parameters is going to give you a listing of the options available. There are quite a few. I'm only going to talk about a couple of basic ones. You'll have to resort to the manual if you need more details. What we're going to do is compile a simple little PIC program. This program just toggles a pin on Port B, Pin 1. There is no delay, so in actuality it would be blinking at a rate too quickly to detect. However, for our purposes, and to see a simultion working, it'll do just fine. The file is for a PIC16F627. If you do not have this PIC, don't worry. We're just going to simulate it. Actually programming the PIC and testing it physically is beyond the scope of this article. So, first download my little sample PIC program: toggle_led.tar.gz Make sure you download the above file. Do not copy and paste from this webpage or your code may not compile! In a command prompt, much like we did when installing the utilities, navigate to the folder where you downloaded the file and execute:

tar -xzf toggle_led.tar.gz

cd toggle_led

That will put you in the directory containing toggle_led.asm. To be sure, issue the ls command and verify that toggle_led.asm is there:

[user@localhost toggle_led]$ ls

toggle_led.asm

[user@localhost toggle_led]$

Below is the listing for toggle_led.asm. I'm not going to explain this code, as that's another article at another time. It's assumed you know the very basics of PIC programs.


*
Filename
toggle_led.asm *
Date
04.03.2004 *
File Version
1.0.0 *
*
Author
Micah Carrick *
*
NOTES
*
*
*
CHANGE LOG
*
*
*
                                                                                                                                                            • list p=16f627 ; list directive to define processor
  1. include
; processor specific variable definitions__CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _ER_OSC_CLKOUT & _MCLRE_ON & _LVP_ON
'__CONFIG' directive is used to embed configuration data within .asm file.
The lables following the directive are located in the respective .inc file.
See respective data sheet for additional information on configuration word.
VARIABLE DEFINITIONS *

w_temp EQU 0x70 ; variable used for context saving

status_temp EQU 0x71 ; variable used for context saving

RESET VECTOR *

ORG 0x000 ; processor reset vector

goto main ; go to beginning of program

INTERRUPT VECTOR *

ORG 0x004 ; interrupt vector location

movwf w_temp ; save off current W register contents

movf STATUS,w ; move status register into W register

movwf status_temp ; save off contents of STATUS register

TODO
Place ISR (Interrupt Service Routine) here...

movf status_temp,w ; retrieve copy of STATUS register

movwf STATUS ; restore pre-isr STATUS register contents

swapf w_temp,f

swapf w_temp,w ; restore pre-isr W register contents

retfie ; return from interrupt

MAINLINE CODE *

main

bsf STATUS, RP0 ; Select Bank 1

bcf TRISB ^ 0x080, 1 ; Enable RB1 for Output

bcf STATUS, RP0 ; Select Bank 0

toggle

bsf PORTB, 1

bcf PORTB, 1

goto toggle

END ; directive 'end of program'

Now execute the following command to complie the ASM file:

gpasm toggle_led.asm

After compiling, type ls again to ensure you have 3 new files:

[user@localhost toggle_led]$ ls

toggle_led.asm toggle_led.cod toggle_led.hex toggle_led.lst

The toggle_led.hex is our actual program which would be "burned" into our PIC. This is the file we are going to have GPSIM simulate. However, toggle_led.cod is also important. We're going to pass that to GPSIM so that we can have a nice "Source Window" where we can step through our code line-by-line. We'll go over that in the next section. Simulating a Simple PIC Program Now we're going to use GPSIM to "Simulate" the program running on the PIC. This is the cool part. This is an incredibly valuable tool in debugging your PIC programs. It alows you to slow it all down and actually step through your program and watch it run. So, using our toggle_led.asm program, let's get simulating. Execute the command:

gpsim -pp16f627 -s toggle_led.cod toggle_led.hex

This command has a couple of important arguments. First, -pp16f627 tells gpsim what PIC it's simulating. Next, -s toggle_led.cod tells gpsim which file to use for the debug info. This is extremely important as it gives us our Source Window. Not only will the gpsim GUI show up, but you'll also get a bunch of text output to your command window. You can just ignore that text for now. We're going to use the GUI. What we want to do is arrange some windows on our desktop so we can watch things happen as we step through the code. Make sure you have the 'Source Browser', 'Symbol Viewer', and 'Breadboard' windows in addition to the main window and arrange them on your desktop as best you can so that you can see them all (may not be possible depending on your screen resolution). You can hide/display these windows from the 'Windows' menu item in the main windows (The main window is titled '0.21.2' which is the version). My layout looked like the display below (click the image to see full size):

gpsim window layout on Linux desktop I'm not going to talk about the Symbol Viewer in this article, I just had you open it so that you can see the various symbols change as we step through the code. The main thing to notice, when we first start gpsim, is the little green arrow in the Source Browser window. This little arrow shows us the next instruction in our source code that will be executed when we press the 'Step' button in the main window. Press the 'Step' button in the main window 3 times. This advances the green arrow to the line:

bcf STATUS,RP0 ; Select Bank 0

However, this is the next instruction to be executed. The actual current state of the simulation is a result of the prior instruction:

bcf TRISB ^ 0x080, 1 ; Enable RB1 for Output

gpsim Source Window That's important. What that instruction does (did) is set pin1 on portb as an output pin. This is reflected in the Breadboard window the direction of the arrow changing to point away from the chip.

gpsim breadboard window showing state of pin Now, press the 'Step' button in the main window again 2 times. This will advance the arrow in the Source Window down into the main loop. The last instruction that was executed was:

bsf PORTB, 1

That instruction sets bit 1 of portb. Again, this is going to be notated in our Breadboard window. This time, to denote the pin being set, the pin goes red:

Breadboard Window with red arrow notating pin being high or set If we press 'Step' one more time, the bcf instruction is executed and the pin goes green again. If we keep pressing 'Step', we'll just continue in this loop and the pin will just toggle from high to low over and over. If you press the 'Run' button instead of step, the code will just run. You'll probably just see the pin flicker red and green and the arrow in the Source window will VERY rapidly jump to each position in the loop. From Here... Well, from here you're on your own. Read the documentation, experiment, and ask questions. You may want to join the GNUPIC mailing list. Once you've got your PIC program working, and you're ready to program the chip, you're going to have to find the software for Linux that works for your particular programmer. If you're using the KDE Desktop, there's a great IDE (Integrated Development Environment) for the PIC including a generic programmer called PikDev. Personally, I'm now using Gnome exclusively, but I have used PikDev in the past and it's built in programmer worked with my Epic Plus programmer after making the modification discussed on the PikDev website.


Referensi

Pranala Menarik