/***************************************************
本程序是要用單片機做一個計算器
*************************************************/
#include
#include
#define GPIO_key P2
#define GPIO_zu P0//位選
#define GPIO_led P1//段選
unsigned char zu[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char LED_data[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
unsigned char LED_num[9];
unsigned char flag;
void display_num();//用來顯示每次按鍵之後的key_value
void dispay_r(unsigned int r);//用來顯示結果
void delayms(unsigned int i);//延時函數
char keypress();//掃描矩陣鍵盤
int jisuan();
unsigned char data[17];//全局存儲數組 存儲鍵值
//MAIN.C(23): error C141: syntax error near '['
unsigned char p;//用於 計算函數 和 出棧顯示 的下標
//uchar p_stack;//用於顯示數字的下標
void main()
{
int i;
int res;
p=-1;
unsigned char key_value;
//MAIN.C(32): error C141: syntax error near 'unsigned'
for(i=0;i<10;i++)
{
LED_num[i]=LED_data[i];
}
flag=0;//標記是否有鍵值
while(1)
{
loop:
key_value=keypress();//修改flag,輸出鍵值
//MAIN.C(32): error C202: 'key_value': undefined identifier
if(flag==1)//當flag=1時,表示有鍵值輸入
{
if (p<=17)//入隊操作
{
p++;
data[p]=key_value;
}
if (key_value>-1&&key_value<10)
{
display_num();// display each num;
}
else if (key_value=='=')
{
res=jisuan();
dispay_r(res);
}
else goto loop;
//判斷key_value
}
flag=0;
}
}
int jisuan()
{
unsigned int num[2]={0,0};
unsigned char fuhao;
int i=0;
for (;p>-1;p--)
{
if (data[p]>-1&&data[p]<10)
//MAIN.C(71): error C141: syntax error near 'data'
{
num[i]=data[p]+num[i]*10;
}
else
{
i++;
fuhao=data[p];
}
}
switch(fuhao)
{
case '+':num[1]=num[0]+num[1];break;
case '-':num[1]=num[0]-num[1];break;
case '*':num[1]=num[0]*num[1];break;
case '/':num[1]=num[0]/num[1];break;
}
return num[1];
}
char keypress()
{ unsigned char temp,key;
GPIO_key=0xfe;
temp=temp&0xf0;
if (temp!=0xf0)
{
delayms(10);
temp=GPIO_key;
if (temp!=0xf0)
{
flag=1;
temp=GPIO_key;
switch(temp)
{
case 0xee:key=0;break;
case 0xde:key=1;break;
case 0xbe:key=2;break;
case 0x7e:key=3;break;
}
while(temp!=0xf0)
{
temp=GPIO_key;
temp=temp&0xf0;
}
}
}
GPIO_key=0xfd;
temp=temp&0xf0;
if (temp!=0xf0)
{
delayms(10);
temp=GPIO_key;
if (temp!=0xf0)
{
flag=1;
temp=GPIO_key;
switch(temp)
{
case 0xed:key=4;break;
case 0xdd:key=5;break;
case 0xbd:key=6;break;
case 0x7d:key=7;break;
}
while(temp!=0xf0)
{
temp=GPIO_key;
temp=temp&0xf0;
}
}
}
GPIO_key=0xfb;
temp=temp&0xf0;
if (temp!=0xf0)
{
delayms(10);
temp=GPIO_key;
if (temp!=0xf0)
{
flag=1;
temp=GPIO_key;
switch(temp)
{
case 0xeb:key=8;break;
case 0xdb:key=9;break;
case 0xbb:key='+';break;
case 0x7b:key='-';break;
}
while(temp!=0xf0)
{
temp=GPIO_key;
temp=temp&0xf0;
}
}
}
GPIO_key=0xf7;
temp=temp&0xf0;
if (temp!=0xf0)
{
delayms(10);
temp=GPIO_key;
if (temp!=0xf0)
{
flag=1;
temp=GPIO_key;
switch(temp)
{
case 0xe7:key='*';break;
case 0xd7:key='/';break;
case 0xb7:key='=';break;
case 0x77:key='.';break;
}
while(temp!=0xf0)
{
temp=GPIO_key;
temp=temp&0xf0;
}
}
}
return key;
}
void display_num()
{
int zhi=p;
int j=0;
while(j>-1)
{
if (data[zhi]>-1&&data[zhi]
{
GPIO_zu=zu[j];
GPIO_led=LED_num[data[zhi]];
zhi--;
j++;
}
}
}
void dispay_r(unsigned int r)
{
unsigned char temp_r[8];
int i;
for(i=0;i
{
temp_r[i]=(int)r%(int)pow(10,i+1);
}
for (i=0;i
{
GPIO_zu=zu[i];
GPIO_led=LED_num[temp_r[i]];
}
}
void delayms(unsigned int i)
{
unsigned char c;
for(;i>0;i--)
{
for(c=110;c>0;c--);
}
}
int i;
int res;
p=-1;
unsigned char key_value;
p = -1;已經是語句了 後面又有 key_value 的定義 編譯器 貌似會忽略,把語句順序調成
int i;
int res;
unsigned char key_value;
p = -1;
在編譯 就沒事了
data 是關鍵字 換掉