为什么c51程序中总会出现 syntax error near 'int'
#include"reg51.h"#include"string.h"#include"intrins.h"#include"absacc.h"#include"stdl...
#include"reg51.h"
#include"string.h"
#include"intrins.h"
#include"absacc.h"
#include"stdlib.h"
#include"math.h"
typedef unsigned char uchar;
sbit BEEP=P1^1; //蜂鸣器
sbit KEY=P1^2;//LED
uchar table[17]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60,0x30,0x25,0xa9,0x26,0xa1,0xb1};
uchar disbuff;//显示缓存
uchar key;
void display(unsigned char disp);
void beep();
void keyscan(void);
void keydown(void);
void delay0(unsigned char x);
void delay(unsigned char x);
void main (void)
{
P0=0xff;
P1=0xff;
P2=0xff;
disbuff=0xbf;
while(1)
{
keyscan();
keydown();
display(disbuff);
delay(2);
}
}
void keyscan(void)
{
P2=0xFE;
key=P2;
unsigned int i;//总是在这里出错
for (i=0;i<4;i++)
{ if(P2<key)
{
if(P2.6==0)
key=i+i*3+2;
else if(P2.5==0)
key=i+1+i*3;
else key=i+i*3;
break;
}
P2=P2<<1;
}
disbuff=table[key];
} 展开
#include"string.h"
#include"intrins.h"
#include"absacc.h"
#include"stdlib.h"
#include"math.h"
typedef unsigned char uchar;
sbit BEEP=P1^1; //蜂鸣器
sbit KEY=P1^2;//LED
uchar table[17]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60,0x30,0x25,0xa9,0x26,0xa1,0xb1};
uchar disbuff;//显示缓存
uchar key;
void display(unsigned char disp);
void beep();
void keyscan(void);
void keydown(void);
void delay0(unsigned char x);
void delay(unsigned char x);
void main (void)
{
P0=0xff;
P1=0xff;
P2=0xff;
disbuff=0xbf;
while(1)
{
keyscan();
keydown();
display(disbuff);
delay(2);
}
}
void keyscan(void)
{
P2=0xFE;
key=P2;
unsigned int i;//总是在这里出错
for (i=0;i<4;i++)
{ if(P2<key)
{
if(P2.6==0)
key=i+i*3+2;
else if(P2.5==0)
key=i+1+i*3;
else key=i+i*3;
break;
}
P2=P2<<1;
}
disbuff=table[key];
} 展开
1个回答
展开全部
有两个问题:1、变量的定义要放到函数的开始位置;
2、单片机寄存器的位引用要使用^,比如P2^5;
改过后的程序:
#include"reg51.h"
#include"string.h"
#include"intrins.h"
#include"absacc.h"
#include"stdlib.h"
#include"math.h"
typedef unsigned char uchar;
sbit BEEP=P1^1; //蜂鸣器
sbit KEY=P1^2;//LED
uchar table[17]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60,0x30,0x25,0xa9,0x26,0xa1,0xb1};
uchar disbuff;//显示缓存
uchar key;
void display(unsigned char disp);
void beep();
void keyscan(void);
void keydown(void);
void delay0(unsigned char x);
void delay(unsigned char x);
void main (void)
{
P0=0xff;
P1=0xff;
P2=0xff;
disbuff=0xbf;
while(1)
{
keyscan();
keydown();
display(disbuff);
delay(2);
}
}
void keyscan(void)
{unsigned int i ;
P2=0xFE;
key=P2;
//unsigned int i ;//总是在这里出错
for (i=0;i<4;i++)
{ if(P2<key)
{
if(P2^6==0)
key=i+i*3+2;
else if(P2^5==0)
key=i+1+i*3;
else key=i+i*3;
break;
}
P2=P2<<1;
}
disbuff=table[key];
}
2、单片机寄存器的位引用要使用^,比如P2^5;
改过后的程序:
#include"reg51.h"
#include"string.h"
#include"intrins.h"
#include"absacc.h"
#include"stdlib.h"
#include"math.h"
typedef unsigned char uchar;
sbit BEEP=P1^1; //蜂鸣器
sbit KEY=P1^2;//LED
uchar table[17]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60,0x30,0x25,0xa9,0x26,0xa1,0xb1};
uchar disbuff;//显示缓存
uchar key;
void display(unsigned char disp);
void beep();
void keyscan(void);
void keydown(void);
void delay0(unsigned char x);
void delay(unsigned char x);
void main (void)
{
P0=0xff;
P1=0xff;
P2=0xff;
disbuff=0xbf;
while(1)
{
keyscan();
keydown();
display(disbuff);
delay(2);
}
}
void keyscan(void)
{unsigned int i ;
P2=0xFE;
key=P2;
//unsigned int i ;//总是在这里出错
for (i=0;i<4;i++)
{ if(P2<key)
{
if(P2^6==0)
key=i+i*3+2;
else if(P2^5==0)
key=i+1+i*3;
else key=i+i*3;
break;
}
P2=P2<<1;
}
disbuff=table[key];
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询