MPLAB上的LED指示灯是否闪烁不工作?

时间:2011-11-07 03:21:59

标签: pic microchip mplab

我正在使用MPLAB对我的新微芯片板进行编程,并使用pickit3编程对其进行编程

代码:

// Include the necessary device header file
#include <p18f8722.h>



#pragma config OSC = HSPLL, //OSCS = OFF // HS-PLL Enabled, Internal External Osc. Switch Over OFF Disabled
#pragma config PWRT = OFF // Power Up Timer: OFF Disabled
//#pragma config BOR = OFF, BORV = 25 // Brown Out Reset: OFF, Brown Out Voltage: OFF Disabled
#pragma config WDT = OFF, WDTPS = 128 // Watchdog Timer: OFF Disabled, Watchdog Postscaler: 1:128
//#pragma config CCP2MUX = OFF // CCP2 Mux: OFF Disabled (RB3)
//#pragma config STVR = OFF // Stack Overflow Reset: OFF Disabled
#pragma config LVP = OFF // Low Voltage ICSP:OFF Disabled
#pragma config DEBUG = ON // Background Debugger Enable: OFF Disabled
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF // Code Protection Block 0-3: OFF Disabled
#pragma config CPB = OFF // Boot Block Code Protection: OFF Disabled
#pragma config CPD = OFF // Data EEPROM Code Protection: OFF Disabled
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF // Write Protection Block 0-3: OFF Disabled
#pragma config WRTB = OFF // Boot Block Write Protection: OFF Disabled
#pragma config WRTC = ON // Configuration Register Write Protection: OFF Disabled
#pragma config WRTD = OFF // Data EEPROM Write Protection: OFF Disabled
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF // Table Read Protection Block 0-3: OFF Disabled
#pragma config EBTRB = OFF // Boot Block Table Read Protection: OFF Disabled




// Function prototypes
void delay1(void);

// Main code section. Execution starts here.
void main(void){
 // First some setup code for the LED
 // The LED will be driven by port D, bit 0, driving the anode, cathode to ground

 // First we should clear the port D, bit 0 data latch
 LATDbits.LATD0=0;

 // We need to set port D, bit 0 as an output
 // Using TRISDbits instead of TRISD allows isolating a single bit leaving the other bits unchanged
 TRISDbits.TRISD1=0; // 0 = output, 1 = input

 // Set port D, bit 0 to off (driving the LED anode, cathode to ground)
 PORTDbits.RD1=0;

 // LED blinking loop that never ends since '1' never changes
 while(1){
 PORTDbits.RD1=1; // turn the LED on
 delay1(); // call the delay function
 PORTDbits.RD1=0; // turn the LED off
 delay1(); // call the delay function
 }
 // end of main, but we will never get this far (endless loop)
}

// Start of our functions
void delay1(void){
 /*
 It is important to note that all variable declarations need to be placed before any code in
 a function or the build will fail. 
 */
 // declare a long integer and set it to zero
 long int loop1=0;

 // count from zero to 30,000 then continue on
 // Lower than 30000 for a faster blink, higher for a slower blink.
 for(loop1=0;loop1<=30000;loop1++){


 }
 // The loop is done and execution has moved past the loop
}

这段代码没有做任何事情,它在高科技C编译器上完美编译,但它没有像预期的那样进行锻炼, 也许问题出在配置位上?任何想法如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

根据控制器和端口判断,我猜您正在使用PIC18 Explorer板。如果是这样,请确认跳线JP1已就位。使用板载LED时必须使用此功能。

如果不是这个主板,那你可以使用错误的端口吗?例如,我用pickit收到的“低引脚数”演示板的端口C上有LED,而不是D.

答案 1 :(得分:0)

检查该引脚是否也具有模拟功能(ANx)。

如果是这种情况,模拟是默认设置,您无法驱动引脚。您应该首先将其设置为数字模式。