PICC支持下的C程序代碼中一定要包含pic.h頭文件,,該文件安裝在HT-PIC\include目錄下,。它是很多頭文件的集合,,C編譯器在pic.h中根據(jù)用戶選擇的芯片自動載入相應的其它頭文件,例如用戶選擇的芯片是PIC16F877,,則pic.h會把pic1687x.h載入,;例如用戶選擇的芯片是PIC16F877A,則pic.h會把pic168xa.h載入,。載入的頭文件中其實是聲明芯片的寄存器和一些函數(shù),。例如以下程序片段:
static volatile unsigned char PORTA@ 0x05;
static volatile unsigned char PORTB@ 0x06;
static unsigned char bank1 OPTION@ 0x81;
static unsigned char bank1 TRISA@0x85;
static unsigned char bank1 TRISB @0x86;
@符號表示地址的意思。這些聲明和PIC匯編中的定義寄存器是差不多的,。
例如:
TMR0 EQU 0x01;
PCL EQU 0x02;
STATUS EQU 0X03;
對于特殊功能寄存器的位地址在頭文件中也有定義,,比如PORTB端口的位地址定義如下:
/* PORTB bits */
static volatile bit RB7 @ (unsigned)&PORTB*8+7;
static volatile bit RB6 @ (unsigned)&PORTB*8+6;
static volatile bit RB5 @ (unsigned)&PORTB*8+5;
static volatile bit RB4 @ (unsigned)&PORTB*8+4;
static volatile bit RB3 @ (unsigned)&PORTB*8+3;
static volatile bit RB2 @ (unsigned)&PORTB*8+2;
static volatile bit RB1 @ (unsigned)&PORTB*8+l;
static volatile bit RB0 @ (unsigned)&PORTB*8+0;
其中RB7代表PORTB的bit7。
我們可以模仿pic.h文件的定義方法定義RAM中文件寄存器的方法,,定義bank0的20h,,21h,22h為文件寄存器以及他們的位地址,。
假設我們把
20h取名RAM20H,,其位地址分別是RAM20H7~RAM20H0
21h取名RAM21H,其位地址分別是RAM21H7~RAM21H0
22h取名RAM22H,,其位地址分別是RAM22H7~RAM22H0
定義如下:
static volatile unsigned char RAM20H @0x20;
static volatile unsigned char RAM21H @0x21;
static volatile unsigned char RAM22H @0x22;
RAM20H的位地址定義如下:
static volatile bit RAM20H7 @(unsigned)&RAM20H*8+7;
static volatile bit RAM20H6 @(unsigned)&RAM20H*8+6;
static volatile bit RAM20H5 @(unsigned)&RAM20H*8+5;
static volatile bit RAM20H4 @(unsigned)&RAM20H*8+4;
static volatile bit RAM20H3 @(unsigned)&RAM20H*8+3;
static volatile bit RAM20H2 @(unsigned)&RAM20H*8+2;
static volatile bit RAM20H1 @(unsigned)&RAM20H*8+1;
static volatile bit RAM20H0 @(unsigned)&RAM20H*8+0;
RAH21H和RAH22H的位地址也可以類似定義:
static volatile bit RAM21H7 @(unsigned)&RAM21H*8+7;
static volatile bit RAM21H6 @(unsigned)&RAM21H*8+6;
static volatile bit RAM21H5 @(unsigned)&RAM21H*8+5;
static volatile bit RAM21H4 @(unsigned)&RAM21H*8+4;
static volatile bit RAM21H3 @(unsigned)&RAM21H*8+3;
static volatile bit RAM21H2 @(unsigned)&RAM21H*8+2;
static volatile bit RAM21H1 @(unsigned)&RAM21H*8+1;
static volatile bit RAM21H0 @(unsigned)&RAM21H*8+0;
static volatile bit RAM22H7 @(unsigned)&RAM22H*8+7;
static volatile bit RAM22H6 @(unsigned)&RAM22H*8+6;
static volatile bit RAM22H5 @(unsigned)&RAM22H*8+5;
static volatile bit RAM22H4 @(unsigned)&RAM22H*8+4;
static volatile bit RAM22H3 @(unsigned)&RAM22H*8+3;
static volatile bit RAM22H2 @(unsigned)&RAM22H*8+2;
static volatile bit RAM22H1 @(unsigned)&RAM22H*8+1;
static volatile bit RAM22H0 @(unsigned)&RAM22H*8+0;
將這些定義加入到相應的頭文件中,,或者直接寫在源程序中,這樣就可以直接對通過變量RAM20H對bank0的20h單元進行字節(jié)操作,,可以通過RAM20H7~RAM20H0對bank0的20h單元進行位操作,。例如:
#include
main()
{
RAM20H=0x20; //把0x20賦值給20h
RAM21H=0x21; //把0x21賦值給21h
RAM22H= RAM20H+ RAM21H;
while(1) { ; }
}
以上程序假設RAM20H、RAM21H和RAM22H的聲明已經(jīng)加入到相應的頭文件中,,那么在源程序中就可以直接使用,。