PICuC 18F2550 flash LED - Page 1
Forum section: electronics / general

author: Edwin Noorlander
author_rank: Contributor
timestamp: December 14, 2013, 02:24:58 pm
content: Hi I am a beginner with MPLAB XC and the PICuC. I want to start with a simple flashing LED on a PIC18F2550. This is my code,Code:#include <./p18f2550.h>#include "xc.h"#pragma config FOSC = INTOSC_EC // Internal oscillator, CLKOUT on RA6, EC used by USB//Function Prototypesvoid tamad(){int t, z;for(z=0x001; z<=0x007; z++){for(t=0x001; t<=0x1388; t++);}}int main(void);int main(void) {while (1){PORTCbits.RC0 = ~PORTCbits.RC0;PORTBbits.RB4 = PORTCbits.RC0;tamad();};}I use the MPLAB XC and the PICKit3 on OSX. Compile and upload goes well. Only flashes there is nothing. So am I doing something wrong but what?Who can help me?greeting Edwin.

author: PA0PBZ
author_rank: Super Contributor
timestamp: December 14, 2013, 02:35:03 pm
content: Remove (the first)int main(void);Change (the second)int main(void) {tovoid main(void) {because you are not passing an integer to main()

author: hans
author_rank: Super Contributor
timestamp: December 14, 2013, 02:58:28 pm
content: That is not the problem. Because there is a while(1) loop , it shouldn't matter at all. At most you get a warning or error if you have the wrong definition of main for the XC8 compiler (don't know the right one on the top of my head)The problem is there is no port configuration. Take a look at the TRISx and analog pin select registers. You need to configure the digital port to a digital output, and on PICs also disable the analog functions (if the pin has the capability of that).By default all pins are usually analog inputs.

author: PA0PBZ
author_rank: Super Contributor
timestamp: December 14, 2013, 03:02:59 pm
content: Quote from: hans on December 14, 2013, 02:58:28 pmNot sure about the compiler but if it takes the first main(); as the main loop it is just an empty loop and nothing will happen.I agree that the second comment I made is just to clean up the code, it is not the problem.Ans yes you are right, an output will help a lot too

author: dannyf
author_rank: Super Contributor
timestamp: December 14, 2013, 03:09:07 pm
content: Quote1. set all the fuses correctly;2. set the pins into gpio mode;QuoteNone of that matters here.

author: Edwin Noorlander
author_rank: Contributor
timestamp: December 14, 2013, 04:19:36 pm
content: Thanks all, where can I find how I set the fuses setting and which one should I set?And how I can get a simple flashing LED.Thanks.

author: Edwin Noorlander
author_rank: Contributor
timestamp: December 14, 2013, 04:36:38 pm
content: Oke, my code works.the code is nou;Code:#include <./p18f2550.h>#include "xc.h"#pragma config PLLDIV = 5 , CPUDIV = OSC1_PLL2 , USBDIV = 2 , FOSC = INTOSCIO_EC#pragma config FCMEN = OFF#pragma config IESO = OFF#pragma config PWRT = OFF#pragma config BOR = OFF#pragma config BORV = 3#pragma config VREGEN = OFF#pragma config WDT = OFF#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF#pragma config CPB = OFF#pragma config CPD = OFF#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFFvoid delay(void){int i, j;for(i=0;i<5000;i++){for(j=0;j<2;j++){ /* Well its Just a Timer */ }}}void main(void){TRISB = 0xF0 ; // PORT B Setting: Set all the pins in port B to Output.while(1){LATBbits.LATB0 = 1; // RB-0 to HighLATBbits.LATB1 = 1; // RB-1 to Highdelay();LATBbits.LATB0 = 0; // RB-0 to LOWLATBbits.LATB1 = 0; // RB-1 to LOWdelay();}}where can I find more example code. Such as using buttons, PWM, SPI ENS.. I do have experience with AVR, Adruino IDE.

author: dannyf
author_rank: Super Contributor
timestamp: December 14, 2013, 05:07:06 pm
content: QuoteNot exactly.QuoteThe worst place would be the internet;2nd best place would be plib, if you want to program the 18f devices;The best place would be the datasheet of your device.I think you will find in the end that the only sure way to go fast is to go slow: learn to do things the right way the first time. It is painful initially but it will pay off handsomely in the end.

author: popamp
author_rank: Newbie
timestamp: December 16, 2013, 11:18:22 pm
content: Quote from: dannyf on December 14, 2013, 05:07:06 pmI would agree with this 100%. Read the device datasheet front to back the first time you use one of these things and everything becomes so much clearer.