本人临时做个单片机的小仿真,位操作总报错,在网上也找不到解决方法,引用<reg51.h>或<reg52.h>或<8051.h>头文件时,写指令P2^3=0;这样的位操作语句总是报错,提示等号两边不相等、等式不成立等等。但在<at89x51.h>头文件中,位被定义为P2_3的形式,则上述位操作可行。在读到一个小程序时终于明白了报错的原因,整理分享给初学者。
关键词:8051单片机;位操作;报错;解决方法
1, 问题描述
在单片机仿真和实验的程序编写过程中,编译经常会报这样一种错误,根本原因并不是头文件中不支持位操作,而是头文件中位定义的语句中借用了位运算符,在C语言下容易出现混淆,引起编译时的误判。比如当我引用<reg52.h>编写STC89C52(其余头文件中位定义的语句形如“P2^3”型号的单片机一样)的程序如下:
#include<reg52.h>
void delay(unsigned intxms) //延时 *ms 时间
{
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=112;j>0;j--)
;
}
void main()
{
while(1)
{
P2^3=0;
delay(1000); //1000ms即1s
P2^3=1;
delay(1000);
}
}
打开头文件reg52.h(见附后)发现头文件中P2.3确实是写作P2^3(在at89x51.h中写作P2_3,可以进行位操作,即可写作P2_3=0;),在keil_V4中编译报错信息如下:
compiling avery4.c...
AVERY4.C(17): errorC141: syntax error near '='
AVERY4.C(21): errorC141: syntax error near '='
avery4.c - 2 Error(s), 0 Warning(s).
即等式附近的语法错误,等式右边的0、1代表0、1(假、真),符号“^”是位运算符,表示“异或”的运算,因而左边P2^3不是P2_3(reg52.h不支持P2_3的写法),而是“P2异或3”的意思,表示一个确定的数值(但P2和3的位数不同,该运算本身错误),因此无法将一个数赋值给另一个数而报错,即使写成(P2^3)=0;也依然是这个问题。
2, 解决方法
解决方法的思想是想办法把P2^3确定地表示成一个端口位的形式,用到位定义指令,程序如下:
#include<reg52.h>
sbit fm=P2^3; // sbit是位定义指令
void delay(unsigned intxms) //延时 *ms 时间
{
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=112;j>0;j--)
; //什么也不做,但该分号不可省,为for循环的必要结构
}
void main()
{
while(1)
{
fm=0;
delay(1000); //1000ms即1s
fm=1;
delay(1000);
}
}
这样,fm就表示真正的位P2^3了。如果把“sbit fm=P2^3;”改成“#define fm P2^3”,其结果也是报错,问题相同,“#definefm P2^3”的实际意思是fm是P2和3取异或的结果值,并不是相要的位P2.3。
3, 附录头文件<reg52.h>
/*--------------------------------------------------------------------------
REG52.H
Header file for generic80C52 and 80C32 microcontroller.
Copyright (c) 1988-2002Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/
#ifndef __REG52_H__
#define __REG52_H__
/* BYTE Registers */
sfr P0 = 0x80;
sfr P1 = 0x90;
sfr P2 = 0xA0;
sfr P3 = 0xB0;
sfr PSW = 0xD0;
sfr ACC = 0xE0;
sfr B = 0xF0;
sfr SP = 0x81;
sfr DPL = 0x82;
sfr DPH = 0x83;
sfr PCON = 0x87;
sfr TCON = 0x88;
sfr TMOD = 0x89;
sfr TL0 = 0x8A;
sfr TL1 = 0x8B;
sfr TH0 = 0x8C;
sfr TH1 = 0x8D;
sfr IE = 0xA8;
sfr IP = 0xB8;
sfr SCON = 0x98;
sfr SBUF = 0x99;
/* 8052 Extensions */
sfr T2CON = 0xC8;
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr TL2 = 0xCC;
sfr TH2 = 0xCD;
/* BIT Registers */
/* PSW */
sbit CY = PSW^7;
sbit AC = PSW^6;
sbit F0 = PSW^5;
sbit RS1 = PSW^4;
sbit RS0 = PSW^3;
sbit OV = PSW^2;
sbit P = PSW^0; //8052 only
/* TCON */
sbit TF1 = TCON^7;
sbit TR1 = TCON^6;
sbit TF0 = TCON^5;
sbit TR0 = TCON^4;
sbit IE1 = TCON^3;
sbit IT1 = TCON^2;
sbit IE0 = TCON^1;
sbit IT0 = TCON^0;
/* IE */
sbit EA = IE^7;
sbit ET2 = IE^5; //8052 only
sbit ES = IE^4;
sbit ET1 = IE^3;
sbit EX1 = IE^2;
sbit ET0 = IE^1;
sbit EX0 = IE^0;
/* IP */
sbit PT2 = IP^5;
sbit PS = IP^4;
sbit PT1 = IP^3;
sbit PX1 = IP^2;
sbit PT0 = IP^1;
sbit PX0 = IP^0;
/* P3 */
sbit RD = P3^7;
sbit WR = P3^6;
sbit T1 = P3^5;
sbit T0 = P3^4;
sbit INT1 = P3^3;
sbit INT0 = P3^2;
sbit TXD = P3^1;
sbit RXD = P3^0;
/* SCON */
sbit SM0 = SCON^7;
sbit SM1 = SCON^6;
sbit SM2 = SCON^5;
sbit REN = SCON^4;
sbit TB8 = SCON^3;
sbit RB8 = SCON^2;
sbit TI = SCON^1;
sbit RI = SCON^0;
/* P1 */
sbit T2EX = P1^1; // 8052 only
sbit T2 = P1^0; // 8052 only
/* T2CON */
sbit TF2 = T2CON^7;
sbit EXF2 = T2CON^6;
sbit RCLK = T2CON^5;
sbit TCLK = T2CON^4;
sbit EXEN2 = T2CON^3;
sbit TR2 = T2CON^2;
sbit C_T2 = T2CON^1;
sbit CP_RL2 = T2CON^0;
#endif