一、宏定义不同:
16系列
__CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_ON&MCLRE_OFF&CP_ON&CPD_OFF&BOREN_ON&CLKOUTEN_OFF&IESO_ON&FCMEN_ON);//这个要放到上一行去 __CONFIG(PLLEN_ON&LVP_OFF) ;//将FOSC<2:0>:振荡器选择位设置成 INTOSC,使能PLLEN
18系列
#pragma config WDT = OFF, OSC = HS #pragma config PBADEN = OFF //forbidden PORTB adc function
二、中断方式不同:
16系列
/*--------------中断处理函数--------------------*/ u8 Count1; void interrupt ISR(void) { if(TMR0IF) { Count1 ++; if(Count1 ==50)//1ms { Count1 = 0; Event1ms =1; } TMR0 = 105; //20us TMR0IF=0; } }
18系列
#pragma code Low_Vector222 = 0x18 void Low_Vector333(void) { _asm goto InterruptHandlerLow _endasm } #pragma code #pragma interruptlow InterruptHandlerLow void InterruptHandlerLow(void) { //Add the interrupt processing } /*-------------------------------------------------*/ #pragma code InterruptVector222 = 0x08 //Name can be arbitrarily set void InterruptVectorHigh333(void) //Name can be arbitrarily set { _asm goto InterruptHandlerHigh _endasm } #pragma code #pragma interrupt InterruptHandlerHigh void InterruptHandlerHigh (void) { //Add the interrupt processing if(INTCONbits.TMR0IF) { INTCONbits.TMR0IF=0; TMR0L = 0xC0; //0xC0,0x80 LED0=!LED0; } if(INTCONbits.INT0IF) { INTCONbits.INT0IF = 0; // clear INT0 flag LED0 = !LED0; } }
三、不同的原因
那么是什么原因导致上面两种代码格式不同的呢?是不是因为选用的编译工具不同导致的。
PIC16 使用的是HI_TECH_PICC9.8编译器
PIC18 使用的是xc16编译器
答案是NO,两种代码格式明显不同,不是因为选用的编译工具不一样,而是由头文件里的定义决定的。
#ifndef _PIC_H_ #define _PIC_H_ #ifndef _HTC_H_ #include <htc.h> #endif #include <pic_chip_select.h> /* MPLAB REAL-ICE related macros & includes (currently enhanced PICs only) */ #define CLRWDT() asm("clrwdt") #define SLEEP() asm("sleep") // function version of nop #pragma intrinsic(__nop) extern void __nop(void); #define NOP() __nop() // // Legacy Programming Macro Functions // #define __CONFIG(x) __config(___mkstr(__CONFIG), ___mkstr(pic), ___mkstr(x))
所以源文件文件都要包含htc.h或pic.h头文件,才能使用 __CONFIG() 配置语句。