Author Topic: PIC18F4550 Programming  (Read 9573 times)

0 Members and 1 Guest are viewing this topic.

Offline sci4meTopic starter

  • Regular Contributor
  • *
  • Posts: 139
  • Country: us
    • sci4me.com
PIC18F4550 Programming
« 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?
 

Offline SebG

  • Regular Contributor
  • *
  • Posts: 66
  • Country: ca
  • Currently looking for an EE Job
Re: PIC18F4550 Programming
« Reply #1 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.
Sebastian
 

Offline sci4meTopic starter

  • Regular Contributor
  • *
  • Posts: 139
  • Country: us
    • sci4me.com
Re: PIC18F4550 Programming
« Reply #2 on: July 10, 2013, 01:42:29 am »
Yes, the led is connected properly and working. The LATD idea didnt seem to work...
 

Offline SebG

  • Regular Contributor
  • *
  • Posts: 66
  • Country: ca
  • Currently looking for an EE Job
Re: PIC18F4550 Programming
« Reply #3 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)
Sebastian
 

Offline JTR

  • Regular Contributor
  • *
  • Posts: 107
  • Country: au
Re: PIC18F4550 Programming
« Reply #4 on: July 10, 2013, 01:55:27 am »
Show us your config bits!
 

Offline sci4meTopic starter

  • Regular Contributor
  • *
  • Posts: 139
  • Country: us
    • sci4me.com
Re: PIC18F4550 Programming
« Reply #5 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
 

Offline SebG

  • Regular Contributor
  • *
  • Posts: 66
  • Country: ca
  • Currently looking for an EE Job
Re: PIC18F4550 Programming
« Reply #6 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.
Sebastian
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
PIC18F4550 Programming
« Reply #7 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. 
 

Offline sci4meTopic starter

  • Regular Contributor
  • *
  • Posts: 139
  • Country: us
    • sci4me.com
Re: PIC18F4550 Programming
« Reply #8 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...
 

Offline sci4meTopic starter

  • Regular Contributor
  • *
  • Posts: 139
  • Country: us
    • sci4me.com
Re: PIC18F4550 Programming
« Reply #9 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?
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
PIC18F4550 Programming
« Reply #10 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)
 

Offline SebG

  • Regular Contributor
  • *
  • Posts: 66
  • Country: ca
  • Currently looking for an EE Job
Re: PIC18F4550 Programming
« Reply #11 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. 
Sebastian
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
PIC18F4550 Programming
« Reply #12 on: July 10, 2013, 02:03:01 am »
When you program the programer takes control.
This does not mean you are in running mode.
 

Offline sci4meTopic starter

  • Regular Contributor
  • *
  • Posts: 139
  • Country: us
    • sci4me.com
Re: PIC18F4550 Programming
« Reply #13 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?
 

Offline JTR

  • Regular Contributor
  • *
  • Posts: 107
  • Country: au
Re: PIC18F4550 Programming
« Reply #14 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?
 

Offline sci4meTopic starter

  • Regular Contributor
  • *
  • Posts: 139
  • Country: us
    • sci4me.com
Re: PIC18F4550 Programming
« Reply #15 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
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
PIC18F4550 Programming
« Reply #16 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. 
 

Offline JTR

  • Regular Contributor
  • *
  • Posts: 107
  • Country: au
Re: PIC18F4550 Programming
« Reply #17 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.
« Last Edit: July 10, 2013, 02:14:52 am by JTR »
 

Offline SebG

  • Regular Contributor
  • *
  • Posts: 66
  • Country: ca
  • Currently looking for an EE Job
Re: PIC18F4550 Programming
« Reply #18 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
Sebastian
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
PIC18F4550 Programming
« Reply #19 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.
 

Offline sci4meTopic starter

  • Regular Contributor
  • *
  • Posts: 139
  • Country: us
    • sci4me.com
Re: PIC18F4550 Programming
« Reply #20 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
 

Offline SebG

  • Regular Contributor
  • *
  • Posts: 66
  • Country: ca
  • Currently looking for an EE Job
Re: PIC18F4550 Programming
« Reply #21 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.
Sebastian
 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2187
  • Country: au
Re: PIC18F4550 Programming
« Reply #22 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:
 

Offline Rufus

  • Super Contributor
  • ***
  • Posts: 2095
Re: PIC18F4550 Programming
« Reply #23 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.
 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2187
  • Country: au
Re: PIC18F4550 Programming
« Reply #24 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!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf