PICC支持下的C程序代码中一定要包含pic.h头文件,该文件安装在HT-PICinclude目录下。它是很多头文件的集合,C编译器在pic.h中根据用户选择的芯片自动载入相应的其它头文件,例如用户选择的芯片是PIC16F877,则pic.h会把pic1687x.h载入;例如用户选择的芯片是PIC16F877A,则pic.h会把pic168xa.h载入。载入的头文件中其实是声明芯片的寄存器和一些函数。例如以下程序片段:
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单元进行字节操作,可以通过RAM20H7~RAM20H0对bank0的20h单元进行位操作。例如:
#include
main()
{
RAM20H=0x20; //把0x20赋值给20h
RAM21H=0x21; //把0x21赋值给21h
RAM22H= RAM20H+ RAM21H;
while(1) { ; }
}
以上程序假设RAM20H、RAM21H和RAM22H的声明已经加入到相应的头文件中,那么在源程序中就可以直接使用。