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!

No comments:

Post a Comment