Home     Contact     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS     Terms of services     Privacy policy  
   
 Home      Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science   



Compile a program and transfer to the PIC

If you have succesfully finished the previous pages, then right now you must have in front of you the MPLAB IDE with the Tutor-1 project loaded, and an assembly source file with some lines that absolutely nothing. If so, then you are ready to go on with the next step: The compile and upload!




The compilation

That's the whole idea after all. You need to upload the program to the PIC to make it work. But first, you need to compile it. The compilation is the procedure where the compiler will read the source file and all the included header files, it will check for errors and if all are found normal, it will make a long hexadecimal file. This file will consists of hexadecimal values only. For a human those values does not really mean nothing, but for the PIC this is the language to talk to. When you upload a program to the PIC, all labels and names are lost. Instead, funny hexadecimal values will be used. If you happen sometime to download a program from a PIC to the computer, the only thing you will get are those values. Therefore, you should keep safe backups of your projects and do not rely to the PIC code, because it can only be copied. A maniac could also read and disassembly this bit-yard, but it should be really big deal to worth doing it.

The compilation is simply done if you go to the menu Project -> Make. You can click the F10 button as well. When you do so, the results of the compilation shall be shown in the output window. Before you make the next step of uploading something to the PIC, make sure that the output window does not hide any surprises. First of all, there must be the word "BUILD SUCCEEDED" with bold black letters at the bottom of the window. But this is not all. To be sure that the compilation is done successfully, you should watch also for warnings.

The compiler has errors and warnings. The errors will stop the compilation procedure and a red error message will appear to the output window. You just cannot miss it. Those errors are mainly syntactical errors that makes the execution of the code impossible. The warnings on the other hand warns you about strange situations where care should be taken. The code is able to be executed no matter how many warnings exists. Just make sure that those warnings are caused in purpose. For example, on an 8-bit micro (like the PIC16F88), if you give a value longer than 8 bits, you will get an error that only the 8 lowest bits shall be used. The compilation shall not be halted. But this usually happens due to mistyping of the value. There is no obvious reason why someone would provide a 9-bit value in an 8-bit register. The execution of the program may cause funny results. Just make sure that there is no warning message in the output window. You need to take a closer look as warnings have no distinctive colors or fonts and can easily be missed!




Connecting the programmer to the computer for the first time

If you have just bought a brand new MPLAB IDE compatible programmer, then you need to do some preparations before you go on. First of all, you need to set the drivers. For this, the MPLAB IDE MUST be already installed. it carries all the drivers for the programmers and it will make your installation easier.

Step 1:

Connect the USB cable to the PC. The 'New hardware wizard' will come up. On the answer select 'not this time' and click 'Next'

Step 2:

Select 'Install the software automatically' and click 'Next'

Step 2:

The device is ready to be used.


Now you need to inform the MPLAB about your new hardware:

Step 1:

Open MPLAB IDE and with the device connected, go to menu Programmer -> Select Programmer and select your product.

Step 2:

You should get a message that says that the Auto connect is not enabled and the device is not conencted. This is normal.

Step 3:

Go to the menu Programmer and select your product's wizard.

Step 4:

The wizard appears. Click 'Next' to begin

Step 5:

Select the connection port. I use the USB port. If you use a com port, select the baud rate as well. Click 'Next' to continue.

Step 6:

Slect the option 'Power target from the MPLAB xxxx' and click 'Next'.

Step 7:

Make sure the 'MPLAB automatically connection' checkbox is unchecked and click 'Next'

Step 8:

Check the option 'MPLAB automatically downloads the required operating system' and click 'Next'

Step 9:

Click 'Finish' to end. The device is now set.

Finally, you will need to download an operating system to the device for the PIC16F88. Every time you program a different chip, you need to download a different operating system. If during the previous procedure you had the according option set, then this will be done automatically when you connect to the programmer and you have select a new device. If a different device is detected, you will get a message box like the one on the left. Just click 'Ok' and wait the procedure to end. My USB ICD 2.5 programmer takes up to 8 second to finish this procedure. It may take significantly more if you have a parallel programmer so you need to be patient.




Upload the program to the PIC

If the compilation procedure ends with no error (and preferable with no warning), then you are almost ready to upload the compiled hexadecimal file to the PIC. If you have the same programmer with me (ICD 2.5) or another similar and compatible with the MPLAB IDE, then you should follow the next steps. Otherwise, you should advice the manual provided with your programmer for more details.




Selecting a programmer and connecting MPLAB with it
Step 1:

You need to select the programmer at first. You go to the menu Programmer -> Select Programmer and you select the programmer type you own. In my case, i select the MPLAB ICD 2.

Step 2:

If you do so, it is most possible that a message will appear to the output window saying that Auto connect is not enabled. That is normal because we have not enable the auto connect feature. Therefore you need to connect manually to the programmer. To do so, go to the menu Programmer -> Connect

Step 3:

When you do so, the MPLAB IDE will try to connect to the hardware. If the hardware is correctly installed with the drivers installed, then you should get a connection confirmation to the output window that would look like the one on the left.

The above procedures needs to be done only once as long as the project remains open. You do not need to select a programmer and connect every tome you compile the program and you want to upload it to the PIC. Instead, you can go to the following steps.




Uploading the program to the PIC

You are just a step away before you have successfully uploaded your first program to your PIC. You can see the images on the left and on the right side that indicates how the PIC is placed on the ZIF socket of the ICD 2.5 programmer. Also, you should have the slide switch to the '5V>Target' position. This position informs the programmer that the PIC is not self-powered (like if it was programed with ICSP) but it needs to be powered from the programmer. If you are sure that everything is as described, then you can go on programming the chip.

Step 1:

Go to the menu Programmer -> Program

Step 2:

If everything is ok, then within a second the program is sent to the PIC! Congratulations!







Confirm your knowledge

There is an online test to check your knowledge on this page. You may reveal the test with the following button:






Previous page ---- Next page



Go back to the book contents

Go to the discussion forum of this book





 

Comments

  Name

  Email (shall not be published)

  Website

Notify me of new posts via email


Write your comments below:
BEFORE you post a comment:You are welcome to comment for corrections and suggestions on this page. But if you have questions please use the forum instead to post it. Thank you.


      

  • At 16 March 2016, 1:28:22 user Manolo wrote:   [reply @ Manolo]
    • Simplest and most useful tutorial.


  • At 4 March 2014, 5:32:26 user shyamrajan wrote:   [reply @ shyamrajan]
    • Thank you very much for providing useful information for me which is really helpful for me in doing my project in microcontroller.....


  • At 8 November 2013, 8:34:08 user manuel wrote:   [reply @ manuel]
    • thanks for the discussion here. i learned something.


  • At 24 July 2013, 7:18:49 user Giorgos Lazaridis wrote:   [reply @ Giorgos Lazaridis]
    • @gaurav sinha They are similar but not the same. You need to change them


  • At 12 July 2013, 18:19:21 user gaurav sinha wrote:   [reply @ gaurav sinha]
    • i want to programme pic16f84a using MPLAB,is it possible n the codes for mpic16f84 n 16f84a are similar????


  • At 3 June 2013, 7:56:00 user Giorgos Lazaridis wrote:   [reply @ Giorgos Lazaridis]
    • @Angeltech Prefer "relocatable". In short, absolute code compiles your code exactly as you program it, in the defined positions that you used. Relocatable allows the compiler to decide how to sort your code. Frankly speaking, i haven't yet make something so complicated to require relocatable code in assembly, but it is a good practice to use it nevertheless.


  • At 31 May 2013, 16:07:54 user Angeltech wrote:   [reply @ Angeltech]
    • Sir:

      why when I build the code it display notification "Build absolute or relocatable" whats the purpose of this although if I click any of the two still it displays succeeded on output window.

      thanks,

      Angeltech


  • At 13 July 2011, 13:08:17 user Kammenos wrote:   [reply @ Kammenos]
    • @messi HEX file is the one that can be directly uploaded to the PIC. HEX (hexadecimal) is the languadge that PIC speaks, so when you write a program in assembly or basic or C or whatever, the compiler turns it into HEX and then it can be uploaded to the PIC


  • At 13 July 2011, 10:49:01 user messi wrote:   [reply @ messi]
    • wat does hex file used for...
      plz help i am new to uc


  • At 31 March 2011, 12:21:35 user Kammenos wrote:   [reply @ Kammenos]
    • @aaron
      1. MicroC is a C compiler, MPLAB is microchip's development environment. Of course you can chose one of them. Prefer MPLAB
      2. yes they do.
      3. yes, hex file is enough to program the pic
      4. a compiled code can be disassembled (and hacked) easily
      5. MPLAB is a development environment. You can chose your compiler (C or assembly). MicroC is not dev, it IS a compiler for C

      A hex file can be uploaded, regardless if it is compiled with C or asm compiler. If you have a hex file, you only need a PIC programmer and nothing more. MPLAB can do this also for you.


  • At 31 March 2011, 5:21:29 user aaron wrote:   [reply @ aaron]
    • if i am given a free hex file which was compiled by MikroC for a certain project, can I use any programmer to transfer or burn the hex file into the PIC?


  • At 31 March 2011, 2:08:18 user aaron wrote:   [reply @ aaron]
    • im new to PIC microcontroller. Please correct me for this:
      1. MPLAB and MikroC Pro are kinds source code. I can choose either one of them.
      2. MPLAB and MikroC Pro have features to compile the source code file.
      If the source code is done in MPLAB it can only be compiled using MPLAB, same thing with MIkroC Pro.
      3. If source code and hex file are both available for free for a particular project in the internet, then no need to download the source file (unless I need to modify the code) because the microcontroller to be programmed needs only the hex file.
      4. A compiled code cannot be hacked or pirated for modification. Hackers can only modify if they have the source code either made by MPLAB or MikroC Pro.
      5. MPLAB uses programming language different from MIkroC Pro.

      Can somebody please correct me with the above statement I have made. I'm new to this tutorial. As an electronic enthuasiast , I'm also interested to learn this nice stuff.


  • At 22 March 2011, 21:08:37 user Kammenos wrote:   [reply @ Kammenos]
    • the board you see in the photos above comes with the ICD 2 programmer


  • At 22 March 2011, 14:38:36 user Msndk wrote:   [reply @ Msndk]
    • does anyone know what sort of board (the one with the zif socket) is being used? and where could you perhaps find one?
     






    No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise without the prior written permission of the author.

    Read the Disclaimer


    All trademarks used are properties of their respective owners.
    Copyright © 2007-2009 Lazaridis Giorgos.
    All rights reserved.






     HOT in heaven!


  • Disclaimer
  • Book Contents
  • Discussion forum

  • Basics
  • What will you need
  • Choosing the right PIC
  • The MPLAB
  • Getting familiar with the MPLAB environment
  • Creating a new project
  • Open and close projects
  • Creating new files and including them in the project
  • Your very first assembly program
  • Compile a program and transfer to the PIC
  • Section 1: Beginner's theory
  • Memory Organization
  • The Data Memory Organization
  • The Program Memory Organization
  • The instructions
  • General knowledge about instructions
  • Value Loading Instructions
  • Program Flow Instructions
  • Mathematic Instructions
  • Logic Function Instructions
  • Bit Orientated Instructions
  • Byte Orientated Instructions
  • Miscellaneous Instructions
  • The Basic Special Function Registers
  • The Status Register
  • The Option_Reg Register
  • The TRIS and PORT registers
  • Beginner's PIC Tutorials
  • How to use our PIC Tutorials
  • A Pushbutton turning an LED on and off
  • A Simple LED Flasher
  • Interfacing Multiple Switches - The internal Pull-Up resistors
  • An LED Sequencer
  • Interface a Single 7seg Digit
  • Interface Multiple 7seg Digits
  • A 3-digits Decimal Counter
  • A Clever Button
  • Section 2: Intermediate theory
  • Instruction Cycle Duration and Calculated Delays
  • The Timer Modules - Timer0
  • The Timer Modules - Timer1
  • The Timer Modules-Timer2



  • NEW in heaven!



    New Theory: AC electric motor working principle






     Contact     Forum     Projects     Experiments     Circuits     Theory     BLOG     PIC Tutorials     Time for Science     RSS   

    Site design: Giorgos Lazaridis
    © Copyright 2008
    Please read the Terms of services and the Privacy policy