一部分用C语言编写的C51单片机时钟程序,求加注释

由于字数限制无法全部粘上来,只粘了些看不太懂的,那个DelayMS()的作用和Display()中的内容重点说明一下,谢谢!ucharcodeseven_seg[]={0... 由于字数限制无法全部粘上来,只粘了些看不太懂的,那个DelayMS( )的作用和Display( )中的内容重点说明一下,谢谢!
uchar code seven_seg[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7C,0x39,0x5e,0x79,0x71};

uchar keyState = 0 ; // 0表示没有操作 1 修改秒 2 修改分钟 3 修改小时 4 定闹 小时 5定闹分钟
uint ledState =0;
uchar ledStateNum=0;
uint num = 0 ;
sbit KEY1=P3^0;
sbit KEY2=P3^1;
sbit KEY3=P3^2;
sbit KEY4=P3^3;

char timer[] ={ 0 , 0 , 0} ; //时,分,秒
char n_timer[2]={11,0};

void DelayMS(uint ms)
{
while(ms--);
}

void Display()
{
uchar i = 0,flags;
uchar count = 0;
uchar temp [6] ={ 0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
if( 4 == keyState || 5 == keyState )
{
P2 = temp[0];
P0 = 0x00;
P0 = 0x40;
DelayMS(1);
P2 = temp[1];
P0 = 0x00;
P0 = 0x40;
DelayMS(1);
P2 = temp[2];
P0 = 0x00;
if(ledState && 4 == keyState)
P0 = seven_seg[ n_timer[0] / 10 ];
else
P0 = seven_seg[ n_timer[0] / 10 ] | 0x80;
DelayMS(1);
P2 = temp[3];
P0 = 0x00;
if(ledState && 4 == keyState)
P0 = seven_seg[ n_timer[0] % 10 ];
else
P0 = seven_seg[ n_timer[0] % 10 ] | 0x80;
DelayMS(1);
P2 = temp[4];
P0 = 0x00;
if(ledState && 5 == keyState)
P0 = seven_seg[ n_timer[1] / 10 ];
else
P0 = seven_seg[ n_timer[1] / 10 ] | 0x80;
DelayMS(1);
P2 = temp[5];
P0 = 0x00;
if(ledState && 5 == keyState)
P0 = seven_seg[ n_timer[1] % 10 ];
else
P0 = seven_seg[ n_timer[1] % 10 ] | 0x80;
DelayMS(1);
P2 = 0XFF;
return ;
}

if( 0 == keyState)
{
P2 = temp[0];
P0 = 0x00;
P0 = seven_seg[ timer[0] / 10 ];
DelayMS(1);
P2 = temp[1];
P0 = 0x00;
P0=seven_seg[ timer[0] % 10 ];
DelayMS(1);
P2 = temp[2];
P0 = 0x00;
P0 = seven_seg[ timer[1] / 10 ];
DelayMS(1);
P2 = temp[3];
P0 = 0x00;
P0 = seven_seg[ timer[1] % 10 ];
DelayMS(1);
P2 = temp[4];
P0 = 0x00;
P0 = seven_seg[ timer[2] / 10 ];
DelayMS(1);
P2 = temp[5];
P0 = 0x00;
P0 = seven_seg[ timer[2] % 10 ];
DelayMS(1);
P2 = 0XFF;

return ;
}
while( i < 6 )
{

if(3- keyState == i/2 && ledState == 0)
{
flags = 0;
}
else
{
flags = 1;
}
if( flags)
{
P2 = temp[i];
P0=0x00;
if(0 == i % 2)
{
P0 = seven_seg[ timer[i/2] / 10 ];
}
else
{
P0 = seven_seg[ timer[i/2] % 10 ];
}
}

i++;
}

}
展开
 我来答
萧然沁芳
2012-06-06 · TA获得超过118个赞
知道答主
回答量:77
采纳率:0%
帮助的人:104万
展开全部
//定义共阴极数码管,对应显示0-F
uchar code seven_seg[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7C,0x39,0x5e,0x79,0x71};

uchar keyState = 0 ; // 0表示没有操作 1 修改秒 2 修改分钟 3 修改小时 4 定闹 小时 5定闹分钟
uint ledState =0;
uchar ledStateNum=0;
uint num = 0 ;
sbit KEY1=P3^0; //定义IO口
sbit KEY2=P3^1;
sbit KEY3=P3^2;
sbit KEY4=P3^3;

char timer[] ={ 0 , 0 , 0} ; //时,分,秒
char n_timer[2]={11,0};

//延时程序 ,当调用此函数时,需要传递参数ms,用于设置延时时间
//若为12M晶振,每条指令对应1us的时间 ,要对应1ms的时间,就要选择
//0.012M的晶振,也就是12K的晶振(有吗?)
// 若要用12M的晶振,实现1ms的延时,程序需要做修改。如下面注释的语句
void DelayMS(uint ms)
{
ms *= 1000; //加上此语句,实现延时1ms
while(ms--);
}

void Display()
{
uchar i = 0,flags;
uchar count = 0;
uchar temp [6] ={ 0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
if( 4 == keyState || 5 == keyState )
//从程序看,应该有外部中断处理函数用于设置keystate参数
//这个if写得比较另类,正常应把参数放在前面,
//判断按键是否按下,两个有一个按下即执行
{
P2 = temp[0]; //向P2口写数据
P0 = 0x00; //有了下面一条指令,这条语句没有用
P0 = 0x40; //向P0口写数据
DelayMS(1); //延时1ms
P2 = temp[1];
P0 = 0x00;
P0 = 0x40;
DelayMS(1);
P2 = temp[2];
P0 = 0x00;
if(ledState && 4 == keyState)
P0 = seven_seg[ n_timer[0] / 10 ];
else
P0 = seven_seg[ n_timer[0] / 10 ] | 0x80;
DelayMS(1);
P2 = temp[3];
P0 = 0x00;
if(ledState && 4 == keyState)
P0 = seven_seg[ n_timer[0] % 10 ];
else
P0 = seven_seg[ n_timer[0] % 10 ] | 0x80;
DelayMS(1);
P2 = temp[4];
P0 = 0x00;
if(ledState && 5 == keyState)
P0 = seven_seg[ n_timer[1] / 10 ];
else
P0 = seven_seg[ n_timer[1] / 10 ] | 0x80;
DelayMS(1);
P2 = temp[5];
P0 = 0x00;
if(ledState && 5 == keyState)
P0 = seven_seg[ n_timer[1] % 10 ];
else
P0 = seven_seg[ n_timer[1] % 10 ] | 0x80;
DelayMS(1);
P2 = 0XFF;
return ;
}
//没有主程序和外部中断处理函数,这段程序读起来不太好读懂
//应该是根据ledstate的状态,也就是4或5来进行时间的校对。

if( 0 == keyState)
{
P2 = temp[0];
P0 = 0x00;
P0 = seven_seg[ timer[0] / 10 ];
DelayMS(1);
P2 = temp[1];
P0 = 0x00;
P0=seven_seg[ timer[0] % 10 ];
DelayMS(1);
P2 = temp[2];
P0 = 0x00;
P0 = seven_seg[ timer[1] / 10 ];
DelayMS(1);
P2 = temp[3];
P0 = 0x00;
P0 = seven_seg[ timer[1] % 10 ];
DelayMS(1);
P2 = temp[4];
P0 = 0x00;
P0 = seven_seg[ timer[2] / 10 ];
DelayMS(1);
P2 = temp[5];
P0 = 0x00;
P0 = seven_seg[ timer[2] % 10 ];
DelayMS(1);
P2 = 0XFF;

return ;
}
//若不需要设置时间,显示时间
while( i < 6 )
{

if(3- keyState == i/2 && ledState == 0)
{
flags = 0;
}
else
{
flags = 1;
}
if( flags)
{
P2 = temp[i];
P0=0x00;
if(0 == i % 2)
{
P0 = seven_seg[ timer[i/2] / 10 ];
}
else
{
P0 = seven_seg[ timer[i/2] % 10 ];
}
}

i++;
}
//这个程序不全,没有看懂
}

这个程序不复杂的。如果懂单片机和C语言很容易读懂的。
有不懂的地方,用Keil或Proteus仿真一下。就读懂了
自己动手操作一下,总比在这里等强。
再说你给的悬赏分也不高啊
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式