PIC MCU's - getting something to compile


> Home
> PIC Microchip Tutorials
> PIC Getting Started Kit
> Robotics
> Contact
> About me



This page is essentially a walkthrough of my initial frustrations trying to get a barebones project to $%^@$! compile. Hopefully it'll save you some time having to realize there are errors in the MPLAB tutorial, having to figure out how to fix them by googling for error codes, and having to sort through dozens of forum posts by others having the exact same problems.

Now, I like Microchip. In general, their documentation is pretty good, and who wouldn't like a company so generous with free samples?! But for heaven's sake, of all places to make errors, did it have to be the bloody tutorials?!

Ok, done whining. Onward!

This tutorial assumes you have downloaded and installed MPLAB IDE, free from Microchip.com. I'm using version 7.2. You don't even have to have a physical MCU or a programmer for this.

I'm using a PIC 16F876A for this particular tutorial (I needed the 2 PWM modules for Bob), but the general information here applies to any other mid-range PIC. I'll point out specifics that you might need to change if you're using a different PIC, as we go.

1. Open up MPLAB, go Project, New..., and type in a project name, and browse to/create a directory for it. Just to note - the project path plus the name of the main project file must be under 62 characters, for reasons that'll we'll see later on. If you want to put your project under My Documents, then you may have to map a drive to your programming directory.

2. After you OK out of the New Project dialog, go to Configure, Select Device..., and use the drop down to select whatever device you are programming for. In my case, it was the PIC16F876A. OK out of that.

3. Now we import a couple of files that Microchip has supplied with MPLAB. We are going to be using relocatable code. Why? We have two choices: relocatable code, or absolute code. I saw some super-genius programmer on the microchip.com forums say "absolute code is obsolete," and I don't like using obsolete stuff, so I will be using relocatable code. : ) Some day when I am a super-genius programmer like him, I will know why, but for now, I can't know EVERYthing all at once, soooooo... moving on!

Browse first to C:\Program Files\Microchip\MPASM Suite\Template\Object (obviously modify that path to your installation specifics), and select the .asm file for your particular PIC. For me it was f876atempo.asm. Copy that file into your project directory.

Next go to C:\Program Files\Microchip\MPASM Suite\LKR, and copy the appropriate file to your project directory. For me it was 16f876a.lkr. If we were writing absolute code, we wouldn't need this linking file. Alas, modern people like ourselves who write relocatable code need the .lkr file!

(Just to note: we could use the project Wizard to do all of this a little more prompted-ly, but I know that mighty 133t hAx0rs like yourself don't need wizards. No! You're repulsed by them!)

One more step, then we can try compiling.

4. Go back to MPLAB. You should have the Project window on your screen that shows all of the project files. Currently, there are none. (If you don't see the Project window, click View, then Project.) Right click on Source Files in the Project window, then Add Files..., and add the f876atempo.asm file to the project. If you're a good, organized programmer, unlike myself, you would probably have renamed it to something descriptive before this step.

Do the same for the .lkr file, under Linker Scripts in the Project window.

5. Almost there! Double click the .asm file, and it will open up in the IDE editor. Now the moment of truth!

The suspense!

"Build All" - Ctrl-F10!

The project... what's this?! doesn't compile?! AN ERROR?! YOU GOT ALL EXCITED FOR A @$^@#$%! ERROR?!

Not to mention a pretty scary lookin' error. Here's what I got:

MPLINK 3.94, Linker
Copyright (c) 2005 Microchip Technology Inc.
Error - section 'INT_VECTOR' can not fit the absolute section. Section 'INT_VECTOR' start=0x00000004, length=0x00000018
Errors : 1

So what does this cryptic message mean? I dunno. Good question. But I do know how to fix it! The answer lies in the shmear of commenting at the top of the template .asm file. This section in particular:

; If interrupts are used, as in this template file, the 16F876A.lkr *
; file will need to be modified as follows: Remove the lines *
; and *
; and change the start address of the page0 section from 0x5 to 0x0

Ok, that's not so bad. Interrupts are included in the template files by default, so we do indeed need to address this comment. Double click your .lkr file in the Project window to open it in the editor, and simply follow the comment instructions. Although, it's usually better to comment things out (use "//" to comment lines out in linker scripts) than erase them.

NOW you should be able to compile... IF you followed my advice at the beginning and kept the name of your file path under 62 characters! If your path IS over 62 characters, then you get this (bizarre) message:

MPLINK 3.94, Linker
Copyright (c) 2005 Microchip Technology Inc.
Errors : 0
MP2COD 3.94, COFF to COD File Converter
Copyright (c) 2005 Microchip Technology Inc.
Error - Source file 'C:\Program Files\Microchip\MPASM Suite\Template\Object\f876atempo.asm' name exceeds file format maximum of 62 characters.
Errors : 1

Why that step of the compilation process requires path names to be under 62 characters is, honestly, beyond me. Oh well.


ANYway, that concludes this tutorial. Hopefully it's working for you! If not, feel free to email me, or better yet... visit http://forum.microchip.com/ and ask the real super-genius programmers for help.

Good luck!



Copyright © 2005 Anthony Rogers