本帖最后由 陈艺萍 于 2018-4-17 13:29 编辑
求助大神,为什么keil编译的时候出现这种错误:error 202 ‘lcden’ :undefine identifier
我改完P口之后:
下面是完整程序:我要实现的是可以负数运算的简易计算器
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
//LCD1602
sbit rs=P2^5
sbit wela=P2^6
sbit lcden=P2^7
uchar code table[]=" ";
long int data_a,data_b
long int data_c
uchar display[10];
void LCD_Delay_us(unsigned int t)
{
while(t--);//t=0íË3ö
}
void LCD_Delay_ms(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<113;j++);
}
void write_com(uchar com)
{
rs=0;
lcden=0;
P0=com;
LCD_Delay_ms(1);
lcden=1;
LCD_Delay_ms(2);
lcden=0;
}
//1602D′êy¾Y
void write_date(uchar date)
{
rs=1;
lcden=0;
P0=date;
LCD_Delay_ms(1);
lcden=1;
LCD_Delay_ms(2);
lcden=0;
}
void W_lcd(unsigned char x,unsigned char y,unsigned char Data)
{
if (y==0){write_com(0x80+x);}
else{write_com(0xc0+x);}
write_data(Data);
}
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char*s)
{
if (y==0){write_com(0x80+x);}
else{write_com(0xc0+x);}
while (*s)
{write_data(*s);s++;}
}
void init_lcd(void)
{
wela=0;
lcden=0;
write_com(0x38);
LCD_Delay_us(100);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x08);
}
short keycheckdown()
{
short temp1,temp2,temp,a=0xff;
P1=0xf0;
LCD_Delay_ms(20);
temp1=P1;
P1=0xff;
LCD_Delay_ms(20);
P1=0x0f;
LCD_Delay_ms(20);
temp2=P1;
P1=0xff;
temp=(temp1&0xf0)|(temp2&0x0f);
switch(temp)
{
case 0x77:a=0x0d;break;
case 0x7b:a=0x0e;break;
case 0x7d:a=0x00;break;
case 0x7e:a=0x0f;break;
case 0xb7:a=0x0c;break;//*
case 0xbb:a=0x9;break;//9
case 0xbd:a=0x8;break;//8
case 0xbe:a=0x7;break;//7
case 0xd7:a=0x0b;break;//-
case 0xdb:a=0x06;break;//6
case 0xdd:a=0x05;break;//5
case 0xde:a=0x04;break;//4
case 0xe7:a=0x0a;break;//+
case 0xeb:a=0x03;break;//3
case 0xed:a=0x02;break;//2
case 0xee:a=0x01;break;//1
default:a=0xff;
}
return a;
}
void display_a()
{
display[3]=data_a%10000/1000;
display[2]=data_a%1000/100;
display[1]=data_a%100/10;
display[0]=data_a%10;
write_com(0x80+0);
if(data_a>999){write_data('0'+display[3]);}
if(data_a>99){write_data('0'+display[2]);}
if(data_a>9){write_data('0'+display[1]);}
write_data('0'+display[0]);
}
void display_b()
{
write_com(0x80+7);
display[3]=data_b%10000/1000;
display[2]=data_b%1000/100;
display[1]=data_b%100/10;
display[0]=data_b%10;
if(data_b>999){write_data('0'+display[3]);}
if(data_b>99) {write_data('0'+display[2]);}
if(data_b>9) {write_data('0'+display[1]);}
write_data('0'+display[0]);
}
void display_c(x)
{
if(data_c<100000000&&data_c>-1)
display[8]=data_c%1000000000/100000000;
display[7]=data_c%100000000/10000000;
display[6]=data_c%10000000/1000000;
display[5]=data_c%1000000/100000;
display[4]=data_c%100000/10000;
display[3]=data_c%10000/1000;
display[2]=data_c%1000/100;
display[1]=data_c%100/10;
display[0]=data_c%10;
write_com(0x80+0x06+0x40);
if(x==4)
{
if(data_c>99999999) {write_data('0'+display[8]);}
if(data_c>9999999) {write_data('0'+display[7]);}
if(data_c>999999) {write_data('0'+display[6]);}
if(data_c>99999) {write_data('0'+display[5]);}
write_data('0'+display[4]);
write_data('.');
write_data('0'+display[3]);
write_data('0'+display[2]);
write_data('0'+display[1]);
write_data('0'+display[0]);
}
else{
if(data_c>99999999) {write_date('0'+display[8]);}
if(data_c>9999999) {write_date('0'+display[7]);}
if(data_c>999999) {write_date('0'+display[6]);}
if(data_c>99999) {write_date('0'+display[5]);}
if(data_c>9999) {write_date('0'+display[4]);}
if(data_c>999) {write_date('0'+display[3]);}
if(data_c>999) {write_date('0'+display[2]);}
if(data_c>999) {write_date('0'+display[1]);}
write_date('0'+display[0]);
}
else {
write_com(0x80+11+0x40);
write_date('E');
write_date('r');
write_date('r');
write_date('o');
write_date('r');
}
}
void eql(uchar x)
{
switch(x)
{
case 1:data_c=data_a+data_b;break;
case 2:if(data_a>=data_b){data_c=data_a-data_b;}
else{data_c=data_b-data_a;W_lcd(5,1,'-');} ;
break;
case 3:data_c=(data_a*data_b);break;
case 4:if(data_b==0){LCD_Write_String(0,1,"Error!";}
else{data_c=(data_a*10000)/data_b;};break;
case 0:break;
}
}
void main()
{
uchar key=0xff;
uchar n=0;
uchar m=5;
uchar x=0;
data_a=0;
data_b=0;
data_c=0;
init_lcd();
display_a();
while(1)
{
key=keycheckdown();
if(0xff!=key)
{
if(key<10)
{
if(n<4){data_a=data_a*10+key;m=5;display_a();}n++;
if(m<4){data_b=data_b*10+key;n=5;display_b();}m++;
}
else
{switch(key)
{
case 0x0a:n=5;m=0;x=1;W_lcd(5,0,'+');break;
case 0x0b:n=5;m=0;x=2;W_lcd(5,0,'-');break;
case 0x0c:n=5;m=0;x=3;W_lcd(5,0,'*');break;
case 0x0d:n=5;m=0;x=4;W_lcd(5,0,'/');break;
case 0x0e:n=5;m=5;eql(x);W_lcd(12,0,'=');display_c(x);break;
case 0x0f:n=0;x=0;m=5;data_a=0;data_b=0;data_c=0;LCD_Write_String(0,0,table);LCD_Write_String(0,1,table);W_lcd(0,0,'0');break;
}
}
do{P1=0xf0;}while(P1!=0xf0);
}
}
}
1.四则运算,包括负数运算
2.开机显示0,当输入的数值或计算得到的结果大于计算器的表示范围时,计算器会在LCD上提示OV;当除数为0时,计算器会在LCD上提示ERR。
|