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.

Thursday, October 29, 2009

Some problems, some progress

I ran into a few unexpected problems and also made a little bit of progress.

First, on the JDM programmer, I asked some questions and got a few answers on the Usbpicprog Sourceforge forums. The guy who answered said that he could not get his JDM to work at all, also that it could not detect his PIC at all. This is exactly what I experienced as well. I had another look and realized that the CTS line of the RS232 port will never be pulled below 0V by this circuit, thus the computer cannot read any information from the PIC. It also seems that the ciruit I referred to at http://www.jdm.homepage.dk/newpics.htm is actually called the JDM2. I'm not exactly sure, but I think that makes this one the JDM1. Maybe it is better to build that one. Maybe the best position to be in is to have somebody else who can program in the first boot loader for you!

Secondly, I am really having problems getting into programming this PIC. It seems a lot more diffucult than I expected. The admins of the usbpicprog project seem to use Linux only, using Piklab to drive the MPLAB C18 compiler. It took me a long time to find out that some command line utilities of Piklab runs on Windows, but not the main GUI application you need. So I downloaded the MPLAB IDE and I'm trying to set up a project with that. I finally succeeded to the point that it builds, but the hex file I'm getting looks a lot different from the one distributed by the usbpicprog project. There is a lot I don't understand, I'll just have to read, read read!

I also continued working on my usbpicprog veroboard a little bit. I fitted the 3 LED's. Only after started I discovered that there is now a Hardware version 3, so I must be sure to work from this diagram from now on. I did not see a parts list (suppose it must be somewhere so I made it like this:
LED1 = Red
LED2 = Green
LED3 = Yellow

I found that LED1 comes on when you remove the one jumper to make it boot in boot mode. The new circuit says 470 ohm resistors. I did not have, so I used 560 ohms, the LED's are bright enough and it saves milliamps!

Thursday, October 22, 2009

Usbpicprog Howto (1) - Building a JDM Programmer

It seems the JDM programmer was designed and is named after Jens Dyekjær Madsen, diagram at http://www.jdm.homepage.dk/newpics.htm. I have however built the one given at http://usbpicprog.org looking like this:

http://usbpicprog.org/images/PIC-ICSP.jpg

If you look at the original JDM schematic, you will see it is totally different. I think the original one has the disadvantage that is uses positive and negative voltages from the RS232 port to get a total of 12V. This means that the Vss of the PIC being programmed will not be at 0V w.r.t. the PC, which means it should be floating and not connected to earth in any way. This may be problematic for a programmer claiming to be an In Circuit Programmer. The other disadvantages of that programmer is that it needs 2 Zener diodes which could be more difficult to obtain than the bunch of resistors in this one. This one on the other hand has the disadvantage that it needs an external 12V supply. I did not have a BC547 transistor. I had a BC517 which looked like it is a Darlington. I tried that first, thinking that when a Darlington switches on, it swithses on harder. It would not work, I think probably because the biasing with the resistors should be different for a Darlington. I ended up using an old NPN transistor without a proper number on it which worked. So it seems it is not very sensitive to the type of transistor, as long as it is an NPN switching transistor and not a Darlington. Finally I was able to program the boot loader into my PIC 16F2550, the only problem is that it does not read back from the PIC. I am still trying to find out why.

Below is my JDM programmer from the top. Note that I soldered the 5 pins on the 9-pin D connector which is in one row to 5 tracks at the bottom of the verobord. Note that as I have build it, the numbering of pins 1-5 on the connector is the opposit as the numbering of the ribbon calble on the other side. Aslo note that you need a female connector, as the RS232 connector on a PC is male. I used an old 10-way ribbon cable from a very old RS232 card. As you can see, I soldered it on the JDM board, it plugs int on the header of the Usbpicprog board. the red wire and the black/white wires are for the 12 V supply. I had a 6.5 Ah battery that dit not work properly for my house alarm any more, but still measured over 12V, that worked for me.


Here is the veroboard seen from the bottom. Ir you really want to, you might be able to copy mine. I do not like planning these things ahead. I just start building, cutting tracks as needed. (You know of course that you use a drill bit turned by hand to cut the tracks? If you have a handle in which you can mount the drill bit, even better.) Working in this unplanned fassion does not allways work out - can you see I had to run one wiret through a cut out hole!


Winpic800 installation:

I used an old PC running Windows 2000. (Better to use an old PC in case you blow up the RS232 port - however that is not likely to happen.) Every time I installed Winpic800, installation of it's IO driver failed. I found somewhere that they said you must install it manually, but not how. I don't remember the exact procedure now, but start from the hardware wizard or somthing device, install a device manually and say you want to install it manually. You will find the .inf file of the driver somewhere in c:\Program Files where Winpic800 is installed. If you can't figure it out, let me know, I'll try to help.

Testing:

If you have your Usbpicprog board already built, do not plug the PIC in it's socket yet - I assume you are using a socket!.

Do not plug the JDM to the PC yet or connect the 12V supply. Use your multimeter on kOhms scales. Measure from pin 4 of the 9-pin connector to pin 28 of the PIC socket. You should read the 2k2 resistor. Then measure from pin 7 of the connector to pin 27 of the PIC socket, you should read the 22k resistor.

Plug the JDM to P2 on the Usbpicprog the JDM to the serial port and connect your 12V supply. Now use your multimeter to measure between ground and pin 1 of the PIC socket. Now run Winpic800, be sure to select the correct COM port you are using. Now tell Winpic800 to program the PIC. If you can manage to sometimes see 0V or very close to 0 V and at other times 12V you could be reasonably sure that your transistor is switching OK.

Now you should be ready, disconnect the 12V and RS232. Plug in the PIC and after connecting everything again, you can program your PIC. You can use a 9-pin D straight cable to connect the JDM to the port.

Final Note:

In Winpic800 I had to select to invert MCLR. You might also need to tell Winpic800 to first erase the PIC before you can program it correctly.

Tuesday, October 20, 2009

The Usbpicprog Project

When I heard a year or so ago that some of MicroChip's PIC controllers now support a USB connection. I was interested, especially since I have worked with some of the PIC's a number of years ago, notably the 16C84. Starting to search for information on the internet, I found the usbpicprog project on http://sourceforge.net/projects/usbpicprog. The web site of the project is at http://usbpicprog.org/

I don't particularly like pc board design and making, and have not really aquired those skills. I do like embedded programming and electronic design at schematic level. So I like to build things up stepwise on veroboard. Below is my usbpicprog, not complete, but doing the most important thing already - talking to the PC via USB! Actually, my interest in the project is not really for it as a PIC programmer (yes, as a side product), but to learn how to communicate with a device via usb.


usbpicprog on veroboard with bare necesities for usb comms

In general, the documentation on http://usbpicprog.org is very good, but as always with documentation written by programmers/designers, what is obvious to them is not always obvious to the reader. (Or put it down to me not reading properly!) The first thing not completely clear to me was, what the usbpigprog can do and what is the purpose of the different headers. The way I see it is:

1. USB Comms and self programming: The PIC can communicate with the software on the PC, you can program the firmware in the PIC, using only the 5V supplied via the USB cable, the voltage pump is not required for that.

2. Programming Header P1: The usbpicprog is an in ciruit PIC programmer, thus it can program another PIC on another board via Header P1. To do this, you need 12V, that is what the voltage pump is for. So if you just want to use the knowledge to build some other usb device, not a programmer, you might not need P1 or the voltage pump.

3. Self Programming Header P2: Note the firmware programming in (1) assumes the boot loader is already loaded in the 18F2550. If not, you need Header P2 and something like a JDM programmer to program in the boot loader. Otherwise, if say you have another PIC programmer in which you can plug the 18F2550 to program the boot loader, you might ommit Header P2, but note that you will then have to tie Pin 1 of the PIC Vpp to Vdd (5V) and put at least 2 pins for a jumper between ground and PGC_SELF, pin 27 of the PIC, to select between boot mode and normal mode.

I will try to explain here, in more details, everything I needed to do for the things I have already achieved and will hopefully achieve in future. I think he steps I take will be
  1. Building the JDM programmer, circuit shown on http://usbpicprog.org
  2. Building the bare necessities of the usbpicprog.
  3. Programming the boot loader using Winpic800.
  4. Driver installation and software installation on PC.
  5. Firmware programming.
  6. Completing the usbpicprog board.
  7. Building an external PIC board to use P1, fully testing the usbpicprog.