Professional Documents
Culture Documents
Programming A PIC
Programming A PIC
Table of content 1. Introduction 2. Materials a. Hardware b. Software 3. Hardware connections 4. Hardware configuration 5. Software installation 6. Software configuration 7. Programming the microcontroller 8. Errors and solutions
Introduction.
The aim of this tutorial is to explain how to program a PIC microcontroller used for the second generation of electronic cards designed for IOCards Project. IOCards project is based in some electronic cards developed by Manuel Vlez (http://www.opencockpits.com) and the related software that make posible to control the flight simulator with external elements as switches, buttons The second generation of these cards uses PIC microcontrollers. This tutorial will show how to program the PIC 16F876 used by the IOCards servomotor II and by the IOCards encoders II. I would like to thank to Manuel Vlez and everybody inside the cockpit builders group for their achievements.
Materials.
Hardware: The PIC 16F876 controller
Sold in electronics shops. There are another models as T-20 II, el TE-20 X. If you cant find them, the best is to ask for a PIC 16F876 programmer and the personnel in the shop will advice us. Price can be from 8 to 10 NOTE : TE-21 programmer is NOT valid. A serial cable
Sold in electronics shops. It depends on which connectors our PC and the programmer have. Nowadays, most of PCs have a male DB9 connector and most of programmers have a female DB9 connector. NOTE : we DONT need a crossed cable; inner connections are made pin to pin.
Downlodable from http://www.ic-prog.com Here you can download the last version of ic-prog.exe file and the helpfile. If you use Windows NT/2000/XP youll have to download the IC-Prog NT/2000 driver too (icprog.sys). Code file (IOCard_Servo.hex)
Available at:
http://www.opencockpits.com/modules.php?name=Downloads&d_op=viewdownload&cid=1&orderby=dateD
We will download the file corresponding with the circuit we are building. If you have any doubt, you can ask in our forum.
Hardware connections.
Plug the cable in the PC serial port. Theres no problem in connecting it while the PC is running because this port is protected against short circuits. Remember the port number (1 or 2). Well need to define this number later in the software.
Insert the PIC in the programmer socket, taking into account the correct position.
Importatnt NOTES: 1. Insert the pic with the programmer disconnected from the cable to avoid electrical damages to the PIC. 2. Dont press the PIC very much. Only a good electric contact is needed. 3. To take out the PIC from its socket, use a screwdiver and try to maintain the PIC parallel to the socket (to avoid the pins from breaking).
Hardware configuration.
Lets configure the serial port in our PC: Windows 9x / ME Start -> Configuration -> Control panel -> System icon -> Devices administrator tab -> Ports (Com & LPT) -> Double click on communications port (Com 1 or 2 ). A similar properties window as the one for Windows XP will be shown. Windows XP Start -> Control panel -> System icon -> Hardware tab -> Devices administrator tab -> Ports (Com & LPT) -> Double click on communications port (Com 1 or 2 ) -> Port configuration tab
Verify the following values: Bits per second: 9600 Data bits: 8 Parity: None Stop bits: 1 Flow control: none With this we have finished configuring our hardware.
Software installation.
Software installation is very easy. Just extract the downloaded files into a folder: icprog105C.zip help_spanish.zip icprog_driver.zip -> icprog.exe (the main program) -> icprog.chm (the help file) -> icprog.sys (the driver for Win NT/2000/XP)
Note: if we use Windows XP, an error message will be shown the first time we run the program.
Windows NT/2000/XP doesnt let any program to use the hardware directly. Only through windows APIs or through specific drivers. Icprog tries to access to the serial port so the error is shown. To solve this well have two options: 1- Configure Icprog for using the windows APIs or 2- Install the specific drivers. First, well accept the error message, and the software will run. 1 .- Use the Windows APIs: Open Menu -> Settings -> Hardware and check Windows Api. Press OK.
Close the program and start it again. Now the error message is not shown. 2 .- Using specific drivers for NT/2000/XP Open Men -> Settings -> Hardware and select Direct I/O. Press OK.
Then: Men -> Settings -> Options -> Mics tab and check Enable NT/2000/XP Driver. Press OK. The following message will be shown:
Answer Yes. A new mesagge for installing the driver will be shown:
Answer Yes and the driver will be installed. The program is now ready to work.
Software configuration.
Start IcProg.exe Hardware settings (mandatory): Men > Settings -> Type of Hardware
Programmer -> JDM Programer Ports -> 1 or 2, depending on where we connected the serial cable. I/O retard -> 10 (we will decrease this value if we have problems). Interface -> we set it before. Communication -> All disabled. Click Ok. Other settings (optional): Menu > Settings -> Options -> Confirmation tab We will check the desired options. Menu > Settings -> Options -> Programming tab We will check Verify.
Code is stored in the selected buffer. Oscillator and Configuration bits are changed as indicated by the *.hex file. We can maintain these values, or change them later if we have problems during the programming proccess.
The file code that we want to write in the microcontroller is stored in the buffer. The programmer is connected to the proper serial port. 4. Programming the microcontroller
Clic the Write all button. Some messages will be shown: Device writing confirmation (if enabled) Writing code progress bar Writing data progress bar Writing configuration Verifying code (if enabled) Verifying data (if enabled)
We will only know if the writing procces has been successsfully finished if we have enabled the verification option. Anyway, we can click the verification button after the writing proccess.
and when the Verification completed message is shown, the PIC is successfully programmed. If we obtain a message like this:
it means that theres an error in the first position. Most of times this is caused by a communications error.
Errors solutions:
General v Only one chip must be inserted in the programmer sockets v Erase the PIC before writing it v Close all other applications running on the PC Try again after this advices. If you still have problems try this: Hardware v v v v Check the cable connections and the number of the serial port Check the model of PIC that we are programming Check the PIC position on the socket Check that all the pins are correctly inserted on the socket
Try again after this advices. If you still have problems try this: Software v v v v Check the type of PIC. Check Menu -> Settings -> Type of hardware. Check the number of serial port selected. Check that the file code (*.hex) is correct. Its no good writing a *.txt file in the PIC.
Try again after this advices. If you still have problems try this: v Decrease the value of Bits per second (Software configuration). Use the values 4800, 2400, 1200 y 300 trying with each of them. Try again after this advices. If you still have problems try this: v Menu -> Settings -> Hardware type, decrease two points the value of I/O retard and try after each decrease. Try again after this advices. If you still have problems try this: v v v v Change the port Use another PC Try another programmer Change the PIC
If everything fails, dont worry. Look for your friends; at least one of them will be able to program your PIC.