EEVblog Electronics Community Forum

Electronics => Microcontrollers => Topic started by: sci4me on July 10, 2013, 01:04:07 am

Title: PIC18F4550 Programming
Post by: sci4me on July 10, 2013, 01:04:07 am
Hey guys! I am pretty new to PIC's and I am trying to blink an led on RD3. This is the code I have right now:
Code: [Select]
#include <p18cxxx.h>

void delay(unsigned long cntr)
{
    while (--cntr != 0);
}

void main(void)
{
    TRISDbits.TRISD3 = 0; //output led

    while(1)
    {
        PORTDbits.RD3 = 1;
delay(25000);
        PORTDbits.RD3 = 0;
        delay(25000);
    }
}
Can someone tell me what I am missing to blink the led on RD3?
Title: Re: PIC18F4550 Programming
Post by: SebG on July 10, 2013, 01:33:02 am
Hi.  First do you have a resistor between the port and the led.  Since an LED will usually have 2 or 3 volts across it when it is on.  So when port is read it will read 0 even when it is set.  But since you are just setting and clearing a pin it should work unless you are writing to the other pins on the port.  Every time PORTD or other ports are written to it is first read, modified and then written back.  Consider using LATD register to set the pins as so: LATDbits.LATD3 = 1;  since that is the actual output register that drives the pins.  Not all PIC have access to the latch port registers but the PIC18 do.
Title: Re: PIC18F4550 Programming
Post by: sci4me on July 10, 2013, 01:42:29 am
Yes, the led is connected properly and working. The LATD idea didnt seem to work...
Title: Re: PIC18F4550 Programming
Post by: SebG on July 10, 2013, 01:52:51 am
Couple of other points i can suggest are:

Is the delay long enough for you to see the led being turned on.
Check to make sure the port is not used by other peripherals that would give priority to the peripheral.
Remove the line PORTDbits.RD3 = 0;  and Dose the LED stay turned on? (assuming that driving the pin high turns the led on)
Title: Re: PIC18F4550 Programming
Post by: JTR on July 10, 2013, 01:55:27 am
Show us your config bits!
Title: Re: PIC18F4550 Programming
Post by: sci4me on July 10, 2013, 01:56:28 am
Config bits? Thats probably the problem. Elaborate?

As far as other peripherals, its just the led.
The led doesnt seem to come on at all with just RD3 = 1 or RD3 = 0... hmm
Title: Re: PIC18F4550 Programming
Post by: SebG on July 10, 2013, 01:56:50 am
Also is the oscillator configured correctly or if the /MCLR pin enabled is it driven high.  Make sure it is not a hardware problem if you are not using some development board and are just doing it on a bread board.
Title: PIC18F4550 Programming
Post by: WarSim on July 10, 2013, 01:57:41 am
Where are your config bits ? 
If I remember correctly default is external osc, fail safe clock had to be enabled. 
Have you confirmed that the program clock is counting. 

It is a good habit to enable clock out on the fires programming, then turn it off when you know you have a running mode. 
Title: Re: PIC18F4550 Programming
Post by: sci4me on July 10, 2013, 01:58:17 am
Im using a development board. Im pretty sure its not a hardware issue. The program downloads properly...
Title: Re: PIC18F4550 Programming
Post by: sci4me on July 10, 2013, 01:59:07 am
Where are your config bits ? 
If I remember correctly default is external osc, fail safe clock had to be enabled. 
Have you confirmed that the program clock is counting. 

It is a good habit to enable clock out on the fires programming, then turn it off when you know you have a running mode.

I am very new to pic and mplab so.. can you explain how do do these things?
Title: PIC18F4550 Programming
Post by: WarSim on July 10, 2013, 02:02:03 am
If your programmer is still hooked up, software can also hold the pic in reset. 
There is a button on the toolbar in MPLabX if this is the case. (If you are using it of course)
Title: Re: PIC18F4550 Programming
Post by: SebG on July 10, 2013, 02:02:36 am
if you are using MPLAB X IDE and you specified the pic type when creating a project then under the menu windows >> PIC memory views >> configuration bits  you can set the config bits and generate code that you paste in your main file. 
Title: PIC18F4550 Programming
Post by: WarSim on July 10, 2013, 02:03:01 am
When you program the programer takes control.
This does not mean you are in running mode.
Title: Re: PIC18F4550 Programming
Post by: sci4me on July 10, 2013, 02:05:18 am
Okay so if I unplug my programmer and press reset, will it go back to run mode?
Also, what config bits do I want to be using?
Title: Re: PIC18F4550 Programming
Post by: JTR on July 10, 2013, 02:05:26 am
If you want help with the config bit settings you will need to tell us what you are using for an oscillator. Do you have a crystal fitted to OSC1 & 2? Are you relying on the internal oscillator?
Title: Re: PIC18F4550 Programming
Post by: sci4me on July 10, 2013, 02:06:32 am
Ive got a crystal. Here is the link to the actual schematic:
https://www.olimex.com/Products/PIC/Proto/PIC-USB-4550/resources/PIC-USB-4550-sch.gif (https://www.olimex.com/Products/PIC/Proto/PIC-USB-4550/resources/PIC-USB-4550-sch.gif)
Title: PIC18F4550 Programming
Post by: WarSim on July 10, 2013, 02:08:44 am
I never use the config code generator like SebG suggested.
It's a good idea for you because you new to this. 
Hopefully SebG can run you through it. 
If not when I come back I will. 
Title: Re: PIC18F4550 Programming
Post by: JTR on July 10, 2013, 02:12:26 am
Paste this into the source file that contains main.c. Put it under any #include directives.

                #pragma config PLLDIV   = 5         
                #pragma config CPUDIV   = OSC1_PLL2   
                #pragma config USBDIV   = 2         
                #pragma config FOSC     = HSPLL_HS
                #pragma config FCMEN    = OFF
                #pragma config IESO     = OFF
                #pragma config PWRT     = OFF
                #pragma config BOR      = ON
                #pragma config BORV     = 3
                #pragma config VREGEN   = ON     
                #pragma config WDT      = OFF
                #pragma config WDTPS    = 32768
                #pragma config MCLRE    = ON
                #pragma config LPT1OSC  = OFF
                #pragma config PBADEN   = OFF
                #pragma config STVREN   = ON
                #pragma config LVP      = OFF
                #pragma config XINST    = OFF       
                #pragma config CP0      = OFF
                #pragma config CP1      = OFF
                #pragma config CPB      = OFF
                #pragma config WRT0     = OFF
                #pragma config WRT1     = OFF
                #pragma config WRTB     = OFF   
                #pragma config WRTC     = OFF
                #pragma config EBTR0    = OFF
                #pragma config EBTR1    = OFF
                #pragma config EBTRB    = OFF


Edit: Of course you are going to read up on what these settings are and do, right? Otherwise me pasting them here is a disservice.
Title: Re: PIC18F4550 Programming
Post by: SebG on July 10, 2013, 02:12:36 am
If you have a project created in MPLAB X IDE then you can use the ability to generate code that sets configuration bits.  It will also have some descriptions of what they are.  When you choose your settings click on the "generate source code to output"

Then copy it to somewhere at the top of your file that includes main() function
Title: PIC18F4550 Programming
Post by: WarSim on July 10, 2013, 02:13:33 am
Config bits depend on what you want to do. 
How fast you want the processor to go, also dependant on speed compatibility of some modules.
If you want watchdog timer. Etc.
Title: Re: PIC18F4550 Programming
Post by: sci4me on July 10, 2013, 02:14:23 am
JTR: IT WORKS!!! :D Thank you all for the help! I appreciate it! SebG: I tried doing that but it didnt work... and JTR's did.. hmm
Title: Re: PIC18F4550 Programming
Post by: SebG on July 10, 2013, 02:22:35 am
windows >> PIC Memory View >> Configuration bits  only shows the bits for the device the project was configured to.  So if you project is not associated with any particular device or the PIC18F4550 device then the wrong configuration bits will be shown.  When you generate the code, copy the code to a header file (*.h) (make sure to include the header file) or your main file.  The configuration bits need to be correctly set in order for the device to work. 

Alternatively you can just manual type them out if you know the correct config bit labels.
Title: Re: PIC18F4550 Programming
Post by: AlfBaz on July 10, 2013, 02:32:18 am
Have you tried a bigger delay number? at full speed (48Mhz) mplab stopwatch recons that delay you have is only 54ms.
Aside from that you need to know that it is actually running.
What IDE are you using, what dev board are you using, what compiler are you using, do you have access to the dev boards schematics.
All of this information will be helpful in narrowing down your problem

EDIT: argh... like five posts since I started to type this one. Man I'm slow :palm:
Title: Re: PIC18F4550 Programming
Post by: Rufus on July 10, 2013, 02:38:31 am
windows >> PIC Memory View >> Configuration bits  only shows the bits for the device the project was configured to.

Alternatively you can just manual type them out if you know the correct config bit labels.

Or you can include a file from the attachment and uncomment the lines you need. Simpler than dicking around with the Configuration bits view and it documents what you haven't selected as well as what you have.

It is a 7zip archive because it compresses 20 times better than zip.
Title: Re: PIC18F4550 Programming
Post by: AlfBaz on July 10, 2013, 02:45:17 am
Or you can include a file from the attachment and uncomment the lines you need. Simpler than dicking around with the Configuration bits view and it documents what you haven't selected as well as what you have.

It is a 7zip archive because it compresses 20 times better than zip.
Awesome!! Where did you find that?
 That's precisely what I do if I'm going to use the one MCU for several different projects. As such I've only created these sorts of files for 1 or 2 MCU's. That attachment seems to contain all of them!
Title: Re: PIC18F4550 Programming
Post by: Rufus on July 10, 2013, 03:23:50 am
Awesome!! Where did you find that?

I wrote a script to generate them from the .cfgdata files that ship with XC8.
Title: Re: PIC18F4550 Programming
Post by: JTR on July 10, 2013, 03:43:15 am
I did a similar thing but with the .DEV files that come with MPLAB. Was a great way to mine for all the programming data etc. I used VB6 to parse the files and write out the applicable programming data to VB6 code. Sort of a twist on self modifying code.
Title: Re: PIC18F4550 Programming
Post by: AlfBaz on July 10, 2013, 04:39:43 am
Ahh OK.
The only place I could find that info in text format, years ago was in the MPLAB help files. Copy, paste and then ran quick and dirty ultraedit macros on it