标题: error C141: syntax error near 'unsigned'”请帮忙看下这段程序有啥问题 [打印本页]

作者: bboroo    时间: 2019-12-15 11:15
标题: error C141: syntax error near 'unsigned'”请帮忙看下这段程序有啥问题
       刚学单片机没多久,看宋师傅的视频刚学到按键这一块。编译的时候,提示“main.c(30): error C141: syntax error near 'unsigned'”,是“u8 cnt = 0;”这一行定义变量出错,发现放在main函数前面就没问题。“cnt”这个不是局部变量吗?为啥放main函数里面编译会出错?另外,发现定义的bit变量必须放main函数的最上面,不然编译也会出错,是什么问题?
#include<reg52.h>                 //此文件中定义了单片机的一些特殊功能寄存器

sbit LED1=P2^2;
sbit LED2=P2^3;
sbit LED3=P2^4;
sbit G1=P1^7;
sbit S1=P1^3;
#define u16 unsigned int  //对数据类型进行声明定义
#define u8 unsigned char

u8 code ledchar[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};

void delay(u16 i);

void main()
{  
        bit backup = 1;
        bit keybuff = 1;
        
        LED1=0;
        LED2=0;
        LED3=0;
        
        G1=0;
        u8 cnt = 0;
        P0=ledchar[cnt];
        while(1)
        {        
                keybuff = S1;
                if(keybuff != backup)
                {
                        delay(1000);
                        if(keybuff == S1)
                        {
                                                if(backup==0)
                                                {
                                                                cnt++;
                                                                if(cnt>=10)
                                                                {
                                                                        cnt=0;
                                                                }
                                                                P0=ledchar[cnt];
                                                }
                                                backup = S1;
                        }
                        
                }
        }               
}

void delay(u16 i)
{
        while(i--);        
}



作者: xuyaqi    时间: 2019-12-15 15:21
这是编译器的要求,不要问为什么。
作者: zhy123654    时间: 2019-12-15 17:14
所有的定义放在函数的顶端,你的定义前有表达式了
作者: bboroo    时间: 2019-12-15 19:47
zhy123654 发表于 2019-12-15 17:14
所有的定义放在函数的顶端,你的定义前有表达式了

原来是这样,感谢回复。




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1