문제

I can't get my pic24f04kl100 to turn on an LED at all. The below code is as simple as possible and it still doesn't turn on the LED on pin 6.

Code

#include <xc.h>

#define LED LATBbits.LATB4
#define LEDans ANSBbits.ANSB4
#define LEDtris TRISBbits.TRISB4

/* Setting up configuration bits */
_FOSCSEL(FNOSC_FRCPLL & IESO_OFF);  // FRC w/PLL and int./ext. switch disabled
_FOSC(POSCMD_XT & FCKSM_CSECMD);    // Pri. OSC XT mode and clk. switch on, fail-safe off
_FWDT(FWDTEN_OFF);                  // Watchdog timer off

void initialise();
void delay(int i);

void main() {                      // Main program loop
    initialise();                  // Intialise PIC
    while (1) {                    // Infinite loop
        LED = 1;                   // Set LED high
        LED = 0;                   // Set LED low
    }
}

void initialise() {                // Configures the PIC
    OSCCONbits.NOSC = 0b111;        // Fast RC Oscillator with Postscaler and PLL module
    delay(100);
    CLKDIVbits.RCDIV = 0b000;       // Set clock div 1:1
    delay(100);
    LEDans = 0;
    delay(100);
    LEDtris = 0;                    // Make LED an output
    delay(100);
    LED = 0;                        // Set LED low
}

void delay(int i) {
    while(i--);
}

PICkit 3 Output

*****************************************************

Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.27.04
Firmware type..............dsPIC33F/24F/24H

Target detected
Device ID Revision = 0

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x3ff
configuration memory

Programming...
Programming/Verify complete
도움이 되었습니까?

해결책

By default B4 pin is analog. Configure it as digital by clearing the ANSB register, bit4

NOTE: Although clearing the bit DID NOT fix the problem. Moving to another pin (with less features) did. So I (fossum) made the assumption that this was at least on some level the correct answer.

다른 팁

The LED is blinking, but it's blinking very fast, try to put some delay between LED turn on and LED turn off.

Try this:

void main() { // Main program loop initialise(); // Intialise PIC while (1) { // Infinite loop LED = 1; // Set LED high delay(50000); //wait LED on time LED = 0; // Set LED low delay(50000); //wait LED off time } }

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top