写这篇文章主要是没有人写过,但是不排除有读者会和我一样遇到这个问题。
如果有其他报错无法解决的读者,可以私信我或或者评论,我会和你一起解决问题。C307是我在学习单片机的简单代码的时候遇到的问题,为同我一样遇到这个问题的单片机学习者提供该问题的解决思路。该代码根据欧浩源老师的数码管动态显示改编。基IAP15K61S2开发。
原本是0xbf的显示为-,但是显示成其他的了,所以改成0x3f才能显示-。如果没有问题的话,还是用0xbf。
#include <STC15F2K60S2.H>
#define uchar unsigned char
uchar miao=49;
uchar fen=59;
uchar shi=23;
uchar ma[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x3f,0xbf};
void Delay(unsigned int t)
{
while(t--);
}
void Delay2(unsigned int J)
{
while(J--)
{
smgstaic();
}
}
void inh(unsigned char n)
{
switch(n)
{
case 4:
P2=(P2=0x1f)|0x80;
break;
case 5:
P2=(P2=0x1f)|0xa0;
break;
case 6:
P2=(P2=0x1f)|0xc0;
break;
case 7:
P2=(P2=0x1f)|0xe0;
break;
}
}
void smg_display(unsigned char dat,unsigned char pos )
{
inh(6);
P0=0x01<<pos;
inh(7);
P0=dat;
}
void smgstaic()
{
smg_display(ma[shi/10],0);
Delay(500);
smg_display(ma[shi%10],1);
Delay(500);
smg_display(ma[17],2);
Delay(500);
smg_display(ma[fen/10],3);
Delay(500);
smg_display(ma[fen%10],4);
Delay(500);
smg_display(ma[17],5);
Delay(500);
smg_display(ma[miao/10],6);
Delay(500);
smg_display(ma[miao%10],7);
Delay(500);
}
void main()
{
while(1)
{
smgstaic();
miao++;
if(miao>59)
{
miao=0;
fen++;
if(fen>59)
{fen=0;
shi++;
if(shi>23)
{
shi=0;
}
}
}
Delay2(200);
}
}
以上代码会出现Delay2的C231的报错,常见的改错方式为添加一个宏定义或者将Delay2整体移动到调用Delay2的函数之后。
如果添加宏定义将会出现C231的报错以及C307的报错,仔细检查后原来是宏定义()里的unsigned char J没有写进去,写进去后,又出现了C304报错,最后将宏定义的部分写为以下形式
#define void Delay2( unsigned int J);
解决了报错问题,但是有警告。
如果采用整体后移那么就是以下形式
#include <STC15F2K60S2.H>
#define uchar unsigned char
uchar miao=49;
uchar fen=59;
uchar shi=23;
uchar ma[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0x3f,0xbf};
void Delay(unsigned int t)
{
while(t--);
}
void inh(unsigned char n)
{
switch(n)
{
case 4:
P2=(P2=0x1f)|0x80;
break;
case 5:
P2=(P2=0x1f)|0xa0;
break;
case 6:
P2=(P2=0x1f)|0xc0;
break;
case 7:
P2=(P2=0x1f)|0xe0;
break;
}
}
void smg_display(unsigned char dat,unsigned char pos )
{
inh(6);
P0=0x01<<pos;
inh(7);
P0=dat;
}
void smgstaic()
{
smg_display(ma[shi/10],0);
Delay(500);
smg_display(ma[shi%10],1);
Delay(500);
smg_display(ma[17],2);
Delay(500);
smg_display(ma[fen/10],3);
Delay(500);
smg_display(ma[fen%10],4);
Delay(500);
smg_display(ma[17],5);
Delay(500);
smg_display(ma[miao/10],6);
Delay(500);
smg_display(ma[miao%10],7);
Delay(500);
}
void Delay2(unsigned int J)
{
while(J--)
{
smgstaic();
}
}
void main()
{
while(1)
{
smgstaic();
miao++;
if(miao>59)
{
miao=0;
fen++;
if(fen>59)
{fen=0;
shi++;
if(shi>23)
{
shi=0;
}
}
}
Delay2(200);
}
0报错,0警告。该时钟显示每20min有1min误差(毕竟没有时钟,原理还是C语言计数程序)。有需要改正的地方请指出。