目录
解决报错
一般解决问题方法
技术精进
写这篇文章主要是没有人写过,但是不排除有读者会和我一样遇到这个问题。
如果有其他报错无法解决的读者,可以私信我或或者评论,我会和你一起解决问题。C307是我在学习单片机的简单代码的时候遇到的问题,为同我一样遇到这个问题的单片机学习者提供该问题的解决思路。该代码根据欧浩源老师的数码管动态显示改编。基IAP15K61S2开发。
原本是0xbf的显示为-,但是显示成其他的了,所以改成0x3f才能显示-。如果没有问题的话,还是用0xbf。
#include
#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<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
#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<59)
{
miao=0;
fen++;
if(fen>59)
{fen=0;
shi++;
if(shi>23)
{
shi=0;
}
}
}
Delay2(200);
}
0报错,0警告。该时钟显示每20min有1min误差(毕竟没有时钟,原理还是C语言计数程序)。有需要改正的地方请指出。
直接将Build Output 报错或者警告信息复制到浏览器搜索,如下图报错。
User\main.c(589): warning: #951-D: return type of function "main" must be "int"
按对应文章进行操作即可
遇到bug很迷惘,没自信了咋么办,改了半天还是不行,不如直接上官方网站去找视频学习。有论坛,大家踩坑一起跳,有优化方案一起分享。
sssxSTC8G-窖头-STC单片机教学视频 国芯技术交流网站 - AI32位8051交流社区