PIC18F4550 Programming - Page 1
Forum section: electronics / general

author: sci4me
author_rank: Regular Contributor
timestamp: July 10, 2013, 01:04:07 am
content: 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:#include<p18cxxx.h>void delay(unsigned long cntr){while (--cntr != 0);}void main(void){TRISDbits.TRISD3 = 0; //output ledwhile(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?

author: SebG
author_rank: Regular Contributor
timestamp: July 10, 2013, 01:33:02 am
content: 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.

author: sci4me
author_rank: Regular Contributor
timestamp: July 10, 2013, 01:42:29 am
content: Yes, the led is connected properly and working. The LATD idea didnt seem to work...

author: SebG
author_rank: Regular Contributor
timestamp: July 10, 2013, 01:52:51 am
content: 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)

author: JTR
author_rank: Regular Contributor
timestamp: July 10, 2013, 01:55:27 am
content: Show us your config bits!

author: sci4me
author_rank: Regular Contributor
timestamp: July 10, 2013, 01:56:28 am
content: 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

author: SebG
author_rank: Regular Contributor
timestamp: July 10, 2013, 01:56:50 am
content: 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.

author: WarSim
author_rank: Frequent Contributor
timestamp: July 10, 2013, 01:57:41 am
content: 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.

author: sci4me
author_rank: Regular Contributor
timestamp: July 10, 2013, 01:58:17 am
content: Im using a development board. Im pretty sure its not a hardware issue. The program downloads properly...

author: sci4me
author_rank: Regular Contributor
timestamp: July 10, 2013, 01:59:07 am
content: Quote from: WarSim on July 10, 2013, 01:57:41 amI am very new to pic and mplab so.. can you explain how do do these things?

author: WarSim
author_rank: Frequent Contributor
timestamp: July 10, 2013, 02:02:03 am
content: 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)

author: SebG
author_rank: Regular Contributor
timestamp: July 10, 2013, 02:02:36 am
content: 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.

author: WarSim
author_rank: Frequent Contributor
timestamp: July 10, 2013, 02:03:01 am
content: When you program the programer takes control.This does not mean you are in running mode.

author: sci4me
author_rank: Regular Contributor
timestamp: July 10, 2013, 02:05:18 am
content: 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?

author: JTR
author_rank: Regular Contributor
timestamp: July 10, 2013, 02:05:26 am
content: 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?

author: sci4me
author_rank: Regular Contributor
timestamp: July 10, 2013, 02:06:32 am
content: 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

author: WarSim
author_rank: Frequent Contributor
timestamp: July 10, 2013, 02:08:44 am
content: 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.

author: JTR
author_rank: Regular Contributor
timestamp: July 10, 2013, 02:12:26 am
content: 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 = OFFEdit: Of course you are going to read up on what these settings are and do, right? Otherwise me pasting them here is a disservice.

author: SebG
author_rank: Regular Contributor
timestamp: July 10, 2013, 02:12:36 am
content: 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

author: WarSim
author_rank: Frequent Contributor
timestamp: July 10, 2013, 02:13:33 am
content: 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.

author: sci4me
author_rank: Regular Contributor
timestamp: July 10, 2013, 02:14:23 am
content: JTR: IT WORKS!!!Thank you all for the help! I appreciate it! SebG: I tried doing that but it didnt work... and JTR's did.. hmm

author: SebG
author_rank: Regular Contributor
timestamp: July 10, 2013, 02:22:35 am
content: 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.

author: AlfBaz
author_rank: Super Contributor
timestamp: July 10, 2013, 02:32:18 am
content: 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 problemEDIT: argh... like five posts since I started to type this one. Man I'm slow

author: Rufus
author_rank: Super Contributor
timestamp: July 10, 2013, 02:38:31 am
content: Quote from: SebG on July 10, 2013, 02:22:35 amOr 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.

author: AlfBaz
author_rank: Super Contributor
timestamp: July 10, 2013, 02:45:17 am
content: Quote from: Rufus on July 10, 2013, 02:38:31 amAwesome!! 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!

author: Rufus
author_rank: Super Contributor
timestamp: July 10, 2013, 03:23:50 am
content: Quote from: AlfBaz on July 10, 2013, 02:45:17 amI wrote a script to generate them from the .cfgdata files that ship with XC8.

author: JTR
author_rank: Regular Contributor
timestamp: July 10, 2013, 03:43:15 am
content: 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.

author: AlfBaz
author_rank: Super Contributor
timestamp: July 10, 2013, 04:39:43 am
content: 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