3

I am trying to get a LED blinking on my pic18f25k80. The LED is connected to pin: RC1.

I think that I might be missing some configuration.

BR Fredrik

#include <p18f25k80.h>

void delay1s(void);

void main(void) {
    TRISC = 0;
    OSCCON = 0xF2;
    while (1) {
        delay1s();
        LATC = 0xFF;
        delay1s();
        LATC = 0x00;
    }
}

void delay1s(void) {
    int m,n;
    for (m=0;m<200;m++) {
        for (n=0;n<200;n++);
    }
}


update: Found the actual problem, I needed this configuration:

# pragma config SOSCSEL = 0x2
Fredrik
  • 31
  • 2
  • This question may possibly also be asked here: http://electronics.stackexchange.com/ - although see how your answers here go first. – rhughes Dec 09 '14 at 00:29
  • 1
    Those nested loops look a little iffy to me... an optimizing compiler will probably see that nothing happens and eliminate it. It can just assign the values **200** to `m` and `n` without doing the loop. – Andon M. Coleman Dec 09 '14 at 00:31
  • Code seems fine to me. Have you tried only switching them on and not off? Is it an evaluation board or what are you using? – PhilMasteG Dec 09 '14 at 00:48
  • @AndonM.Coleman You can trick the compiler sometimes by actually incrementing a value inside the inner for loop, then returning it. This is still a very inexact, flaky way to make a delay. C++ (I'm assuming?) must have a more legitimate delay mechanism; probably in its thread library. – Carcigenicate Dec 09 '14 at 02:05
  • What behavior do you see? You have no mention of what actually happens. Does the LED turn on and stay on? Does it never come on? Does it seem to be half on? – Mathieu L. Dec 09 '14 at 17:26
  • Check the pins of the led. Since it is a diode, you have the chance of connecting it the wrong way. Errare humanum est. – Volkan Dec 09 '14 at 19:40
  • @Carcigenicate - when you're on an embedded device with a few kB of program ROM, bringing in a full threading library to do a delay will kill you ;). A better way to do it is to poll the system timer if there is one, but spinning on an (ideally `volatile`) variable like this also works well for quick tests/small inaccurate timings. – slugonamission Dec 18 '14 at 10:07
  • @slugonamission I've never done anything like this, so I was unaware. I can see that getting annoying. – Carcigenicate Dec 18 '14 at 15:37

0 Answers0