Thursday, November 12, 2009

My Usbpicprog flashes LED's!!!

I want to learn to program the PIC18F2550 and simmelar PIC's. I have two choices - either I complete my Usbpicprog (I am building it step-wise on stripboard) and use it to program another PIC on a second board, or I just use the Usbpicprog PC software and bootloader to self-program the PIC. The last one is the option I chose.

OK, so what is the first exercise to show I'm getting control of the PIC? Blinking the LED's on and off of course! I took one of the examples that came with MPLAB C18, main.c from C:\MCC18\example\getting_started\program3:

#include

#pragma config WDT = OFF

void delay (void)
{
int i;

for (i = 0; i <>
;
}

void main (void)
{
/* Make all bits on the Port B (LEDs) output bits.
* If bit is cleared, then the bit is an output bit.
*/
TRISB = 0;

while (1)
{
/* Reset the LEDs */
PORTB = 0;

/* Delay so human eye can see change */
delay ();

/* Light the LEDs */
PORTB = 0x5A;

/* Delay so human eye can see change */
delay ();
}
}



There are a few changes I needed to make to the code:
  • Created and set up the project as for the Usbpicprog firmware, as explained in my previous post.
  • Take care that the code gets linked at 0x800 since 0-0x7FF is for the boot loader and that the boot loader will jump into it at the right point. I'm not sure yet exactly how it works, but I included the linker script rm18f4550.lkr from the Usbpicprog firmware in the project. (Also included io_cfg.h and typedefs.h.)
  • The LED's for the board they are using is different than for the Usbpicprog, so changed PORTB to PORTC and the numbers that are written to it

#include
#include "typedefs.h" // Required
#include "io_cfg.h" // Required

#pragma config WDT = OFF

extern void _startup (void); // See c018i.c in your C18 compiler dir
#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code

void delay (void);

void main (void)
{
/* Make all bits on the Port C (LEDs) output bits.
* If bit is cleared, then the bit is an output bit.
*/
TRISC = 0;

while (1)
{
/* Reset the LEDs */
PORTC = 0;

/* Delay so human eye can see change */
delay ();

/* Light the LEDs */
PORTC = 0x07;

/* Delay so human eye can see change */
delay ();
}
}

void delay (void)
{
int i;

for (i = 0; i < 10000; i++) ;

}


When the program was successfully compiled, I removed the second jumper from the Usbpicprog and connected to the PC so it boots up in Bootloader mode. Then connected with the Usbpicprog PC software, loaded the little hex file and programmed it. Then unplugged the Usbpicprog, placed the second jumper again to send it to the "firmware", then plugged it to the USB port again. It does not talk USB of course, but it flashes the LED's!

Sunday, November 8, 2009

Problems compiling Uspicprog firmware, or not?

As I have said before, my interest in Usbpicprog is not in it as Pic programmer as such, but to learn how to communicate using Usb. What I left unsaid was that I also see the self-programming capability of the Pic in this setting as a development environment.

For me to start replacing the Usbpicprog firmware with my own, the logical step is first to learn how to compile the given firmware myself. I have written before how I tried to do this using Piklab in Linux, but that proves difficult and for reasons beyond my control, I am bound to Windows at the moment.

So I tried to rebuild the project in MPLAB IDE. The ideal was to be able to compile it so that the hex file I get and the one downloaded from Sourceforge are identical when I compare them. I could not achieve that, although I'd say they are more than 90% identical. I use open source Notepad++ and the Compare plug-in, works like a charm!

I can think of a the following reasons why they are not the same:
  • Possibly different versions of C18 used by myself and the project admins?
  • In Piklab it seems you can put the C files in the order that you want and I suppose the linker will use them in that order. MPLAB IDE arranges them alphabetically and they get linked in that order.
  • At the moment my free version of MPLAB C18 is still within the 60-day trial period, after that optimization will not work any more.
Anyway, it seems that the code I am compiling is also working. At first I had my doubts - did I really program the pic with the file that I compiled etc? Then I changed the SVN_REVISION in in svnrevision.h from "0.3.0" to "0.3.1". The Usbpicprog still connected to the Windows software, but reporting that the firmware is too new.

I will try to remember all the steps I took to set up the project, here goes:
  • Create a folder for the project, e.g. c:\MCC18\Projects\usbpicprogfw and copy all .c, .h and the .lkr file from the downloaded Usbpicprog firmware into this folder.
  • In MPLAB IDE, from the Project menu, use Project Wizard to create a new project, save the project e.g. as usbpicprog (it will do the extension automatically.) Do not create any .c files at this point.
  • Add all the .c, .h and the .lkr file to the project.
  • In Directories, add the Library Search Path c:\mcc18\lib
  • In MPLAB C18 Memory Model, make sure you have: Code Model = Small, Data Model = Large and Stack Model = single bank.
  • Change the Build Configuration from Debug to Release.
  • Set the MPLAB C18 Optimization to Enable All.
I hope I didn't miss anything, if you are still having problems compiling, leave a comment with your e-mail address (it will not be disclosed) and I can zip up the project and send it to you.

A handy feature in MPLAB IDE is View Disassembly Listing (and also View Program Memory). Use the after you have built the project successfully. Note that 0 - 0x7ff is reserved for the Boot Loader and the firmware starts at 0x800. In this project you will notice that code is generated for address 0 etc, but I think somewhere in Usbpicprog it will ignore code for that area. However, if you plug in the Usbpicprog with both jumpers on, the boot loader will when it senses the 2nd jumper, transfer execution to whatever code you have programmed at 0x800.