PIC18F2220 LED闪烁程序错误

PIC18F2220 LED blinking program error

我是PIC编程的初学者。 This video 我曾经编写过我的第一个程序。

这是我写的代码:

#include <stdio.h>
#include <stdlib.h>
#include "XC8.h"

void main(void)
{
   TRISBbits.RB0 = 0;
   OSCCON = 0x76;

   while(1)
   {
      LATBbits.LATB0 = ~LATBbits.LATB0;

      for (int countDelay=0; countDelay<20; countDelay++) __delay_ms(50);
   } 
}

我的 XC8.h 头文件是:

// PIC18F2220 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1H
#pragma config OSC = RC         // Oscillator Selection bits (External RC oscillator, CLKO function on RA6)
#pragma config FSCM = OFF       // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Switchover bit (Internal/External Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer enable bit (PWRT disabled)
#pragma config BOR = OFF        // Brown-out Reset enable bit (Brown-out Reset disabled)
#pragma config BORV = 20        // Brown-out Reset Voltage bits (VBOR set to 2.0V)

// CONFIG2H
#pragma config WDT = ON         // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBAD = ANA       // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (MCLR disabled; RE3 input is enabled in 40-pin devices only (PIC18F4X20))

// CONFIG4L
#pragma config STVR = ON        // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000200-0007FFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (000800-000FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0001FFh) is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000200-0007FFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (000800-000FFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0001FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000200-0007FFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (000800-000FFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0001FFh) is not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#define _XTAL_FREQ 8000000

我已按照教程中提供的所有步骤进行操作,但在尝试构建程序时出现以下错误。

make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory 'E:/Projects/Coding/MPLABX/Programming/XC8/XC8.X'
make  -f nbproject/Makefile-default.mk dist/default/production/XC8.X.production.hex
make[2]: Entering directory 'E:/Projects/Coding/MPLABX/Programming/XC8/XC8.X'
"C:\Program Files (x86)\Microchip\xc8\v1.42\bin\xc8.exe" --pass1  --chip=18F2220 -Q -G  --double=24 --float=24 --emi=wordwrite --opt=+asm,+asmfile,-speed,+space,-debug --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default  --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib   --output=-mcof,+elf:multilocs --stack=compiled:auto:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s"    -obuild/default/production/XC8.p1  XC8.c 
XC8.c:15: error: (192) undefined identifier "TRISBbits"
XC8.c:15: error: (196) struct/union required
XC8.c:16: error: (192) undefined identifier "OSCCON"
XC8.c:20: error: (192) undefined identifier "LATBbits"
XC8.c:20: error: (196) struct/union required
XC8.c:20: error: (196) struct/union required
XC8.c:22: warning: (361) function declared implicit int
(908) exit status = 1
nbproject/Makefile-default.mk:100: recipe for target 'build/default/production/XC8.p1' failed
make[2]: Leaving directory 'E:/Projects/Coding/MPLABX/Programming/XC8/XC8.X'
nbproject/Makefile-default.mk:84: recipe for target '.build-conf' failed
make[1]: Leaving directory 'E:/Projects/Coding/MPLABX/Programming/XC8/XC8.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make[2]: *** [build/default/production/XC8.p1] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 467ms)

希望我已经为查询添加了足够的输入。由于我是这部分的新手,请随时询问更多信息。

您没有包含 #include <xc.h>,而只是设置您的配置位。 xc.h 是主要的头文件,它将包含许多其他头文件,最终指向您的特殊控制器头文件 #include <pic18f2220.h>。它是使用 MPLAB IDE 应该提供的 #ifdef 结构完成的,因为您最初是在创建新项目时设置控制器的。

这个guide from Microchip作为开始非常有用,因为它解释了很多。

如果不包含 <xc.h> 头文件,编译器将不知道控制器的寄存器名称。这就是您出错的原因。