Monday 21 February 2011

Trying to make a simple program

So I have a circuit, a programmer... I've installed the PICkit software and MPLAB IDE from the Microchip site... Now its time to try and get a simple program to compile and run.

Since I have a different laptop from the time I last tried this, I'm starting from scratch.  Running the MPLAB IDE software, creating a project seems to work quite well although I have to admit I have a blank project and I don't know where to go...

I had some vague memory about there being a number of sample programs stored in the Microchip Program files folder... A little investigation found a base assembly file and include file for the 18F2455 chip, so copying those into my project folder and adding to the project all seemed ok, except I get some language plugin exception message and it fails to build still with no further message of any use.

I eventually found that I must have set the Project->"Select Language Toolkit" to a junk value at one point as the MPASMWIN.exe link which existed tried to point to a file in my project.  Putting it back to the file in the "C:\Program Files (x86)\Microchip\MPASM Suite\" folder fixed that problem and created a load of other errors...

Ahhh, errors fixed by finding a project option to generate code in "Relocatable Mode" instead of "Absolute mode".  I had received a number of dialog boxes asking which option I wanted, and not ahving a clue what each one was I clicked absolute... I guess I needed to choose relocatable!  The option was changable in the "Project -> Build Options -> Project" menu, then under the MPASM tab selecting the relocatable option.

Trying to program the chip using the debugger produces an error about being unable to find a Vdd of 5V... I wonder if that is because the board is currently powered by the USB...  Looking through the "Typical ICSP Application Circuit" information in the PICkit documentation explained the circuitry I have connected to the MCLR pin in my old circuit... However adding the resistors and capacitors didn't seem to make any difference to this message, although since the user guide mentions it for ICSP circuitry I think it's best to keep it in!  The 470 & 10K Ohm resistors, and the 100pF capacitor are seen in Figure 3-1 in the PICkit user guide.

While I was changing the bread board circuit, I've also added another LED and resistor to pin 2.  Pin 2 is marked as being "RA0", so it should be bit 0 of Port A... I'll be needing to use that later when I get the LED to flash on and off.

So here's the changes to the circuitry:


And here's a modified image, attempting to show the hidden connections which are being used within the breadboard.  Hopefully this can help understand how the holes are all electronically linked.


The blue line shows the output from Pin 2 carried accross to the LED on the left.  This passes under the (blue) capacitor which connects to the first and third row.

So back to the program... I have a base program, but that is expecting a crystal oscillator which I don't think I have spare, (although I thought I bought two of them a few years ago, maybe I'll find it soon enough, or borrow the one from my other circuit).  Looking through the base sample program, the setting line which sets the Oscilliator is currently "CONFIG FOSC = XT_XT".

Not knowing what options are available for this configuration setting I checked the include file added earlier.  That has the following lines talking about internal Oscilliator's:

;     FOSC = INTOSCIO_EC   Internal oscillator, port function on RA6, EC used by USB (INTIO)
;     FOSC = INTOSC_EC     Internal oscillator, CLKO function on RA6, EC used by USB (INTCKO)
;     FOSC = INTOSC_XT     Internal oscillator, XT used by USB (INTXT)
;     FOSC = INTOSC_HS     Internal oscillator, HS oscillator used by USB (INTHS)

I have no idea which one (if any!) I can use, so I'll start by setting the config value to "INTOSCIO_EC".  Of course I've not actually got a program which does anything yet...  So lets at first try to turn on the LED pin to determine if anything is going to happen.

After a bit of playing, I managed to get the LED to light up using the following code in the section which starts "Main:":

Main:
    clrf    WREG
    movwf   TRISA  ; Configure PORTA as all outputs
    movlw   0xFF
    movwf   PORTA  ; Set Port A to all values as true (on)

    goto $

This code first clears the W-register, then uses that empty value to clear the value of TRISA... Now based on the data sheet, the TRISA register controls what the Port A pins are to be used as.  Setting each bit to 1 results in the pin being an analog input, (the default), and 0 as an output.  Obviously we need the pin to be an output as we want to light an LED...

Next we set the W-Register to have a value of 0xFF which is 8 bits set to 1/ON.  Technically we only need the one bit for RA0 to be set, but since it's late I cba to find out which way round it should be!  Moving the W-REG value to PORTA is the final bit of code, (well goto $ apparently sets the processor into an infinite loop doing nothing).

After a few fiddles, I found out I wanted to do the "Programmer->Program" option, followed by "Programmer->Release from Reset" to get the program running.  Lo and behold the LED does actually light up, and despite being clear, it is actually a red LED.

Woo there's a program and it works!

No comments:

Post a Comment