Author Topic: PICuC 18F2550 flash LED  (Read 3295 times)

0 Members and 1 Guest are viewing this topic.

Offline Edwin Noorlander

  • Contributor
  • Posts: 15
PICuC 18F2550 flash LED
« on: December 14, 2013, 02:24:58 pm »
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: [Select]
#include <./p18f2550.h>
#include "xc.h"

#pragma config FOSC = INTOSC_EC   // Internal oscillator, CLKOUT on RA6, EC used by USB
// Function Prototypes
void 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.
 

Online PA0PBZ

  • Super Contributor
  • ***
  • Posts: 4360
  • Country: nl
Re: PICuC 18F2550 flash LED
« Reply #1 on: December 14, 2013, 02:35:03 pm »
Remove (the first) int main(void);

Change (the second) int main(void) {
to void main(void) {
because you are not passing an integer to main()
Keyboard error: Press F1 to continue.
 

Offline hans

  • Super Contributor
  • ***
  • Posts: 1078
  • Country: nl
Re: PICuC 18F2550 flash LED
« Reply #2 on: December 14, 2013, 02:58:28 pm »
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.
« Last Edit: December 14, 2013, 03:00:12 pm by hans »
 

Online PA0PBZ

  • Super Contributor
  • ***
  • Posts: 4360
  • Country: nl
Re: PICuC 18F2550 flash LED
« Reply #3 on: December 14, 2013, 03:02:59 pm »
That is not the problem.

Not 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  :)
Keyboard error: Press F1 to continue.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8229
  • Country: 00
Re: PICuC 18F2550 flash LED
« Reply #4 on: December 14, 2013, 03:09:07 pm »
Quote
So am I doing something wrong but what?

1. set all the fuses correctly;
2. set the pins into gpio mode;

Quote
Remove (the first) int main(void);

None of that matters here.
================================
https://dannyelectronics.wordpress.com/
 

Offline Edwin Noorlander

  • Contributor
  • Posts: 15
Re: PICuC 18F2550 flash LED
« Reply #5 on: December 14, 2013, 04:19:36 pm »
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.
 

Offline Edwin Noorlander

  • Contributor
  • Posts: 15
Re: PICuC 18F2550 flash LED
« Reply #6 on: December 14, 2013, 04:36:38 pm »
Oke, my code works.
the code is nou;

Code: [Select]
#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 = OFF

void 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 High
        LATBbits.LATB1 = 1;   // RB-1 to High

        delay();

        LATBbits.LATB0 = 0;    // RB-0 to LOW
        LATBbits.LATB1 = 0;    // RB-1 to LOW

        delay();
    }
}



where can I find more example code. Such as using buttons, PWM, SPI ENS.. I do have experience with AVR, Adruino IDE.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8229
  • Country: 00
Re: PICuC 18F2550 flash LED
« Reply #7 on: December 14, 2013, 05:07:06 pm »
Quote
Oke, my code works.

Not exactly.

Quote
where can I find more example code. Such as using buttons, PWM, SPI ENS..

The 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.
================================
https://dannyelectronics.wordpress.com/
 

Offline popamp

  • Contributor
  • Posts: 8
Re: PICuC 18F2550 flash LED
« Reply #8 on: December 16, 2013, 11:18:22 pm »
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.

I 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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf