会51单片机编程的请进来
时间:10-02
整理:3721RD
点击:
请大侠看看这个程序有什么错误,数码管后两位显示有重影,
/*一共两按键,1按键选功能,2按键调节,调节顺序时,分,
秒,月,日,年,周,不在调节功能时按2键显示月日,两键
同时按初始化DS1302*/
#include <AT89X51.H>
#include "ds1302.h"
#define leddata P1
#define sec 0x80
#define min 0x82
#define hou 0x84
#define dat 0x86
#define mou 0x88
#define wek 0x8a
#define yer 0x8c
#define read 0x01
sbit SET=P3^1;
sbit UP=P3^2;
sbit led0=P3^4;
sbit led1=P3^3;
sbit led2=P3^5;
sbit led3=P3^7;
//**************函数声明*****************
void delays(unsigned char);
void display(void);
void Scan_Key(void);
void id_case1_key();
void Set_id(unsigned char ,unsigned char);
//*************变量定义******************
unsigned char id=0,timecount,re_disp=0,ahour=0,aminute=0,asecond=0,adate=0,amouth=0,aweek=0,ayear=0;
bit year,mouth,date,week,hour,minute,second,flag,tlamp;
unsigned char code tab[]={
0x05,/*0*/
0xDD,/*1*/
0x46,/*2*/
0x54,/*3*/
0x9C,/*4*/
0x34,/*5*/
0x24,/*6*/
0x5D,/*7*/
0x04,/*8*/
0x14,/*9*/
}; /*个位0~9的数码管段码*/
unsigned char inittime[7]={0x00,0x00,0x12,0x07,0x03,0x08,0x04};
// 秒 分钟 小时 日 月 年 星期
void t0(void) interrupt 1 using 0
{
TH0=(65535-59524)/256;
TL0=(65535-59524)%256;
timecount++;re_disp++;
if(timecount>9)
{
timecount=0;
flag=~flag;
}
if(re_disp>200){re_disp=0;if(id)id=0;}
}
void delays(unsigned char k)
{
unsigned char i,j;
for(i=0;i<k;i++)
for(j=0;j<50;j++);
}
void readtime(void)
{leddata=0xff;
adate=Read1302(dat|read);
amouth=Read1302(mou|read);
aweek=Read1302(wek|read);
ayear=Read1302(yer|read);
ahour=Read1302(hou|read);
aminute=Read1302(min|read);
asecond=Read1302(sec|read);
}
void display1(void)
{
readtime();
if(flag&hour)
{
led0=0;leddata=0xff;delays(10);led0=1;
led1=0;leddata=0xff;delays(10);led1=1;
}
else
{
leddata=tab[ahour/16];led0=0;delays(10);
delays(10);led0=1;
leddata=tab[ahour%16];led1=0;delays(10);
delays(10);led1=1;
}
if(flag&minute)
{
led2=0;leddata=0xff;delays(10);led2=1;
led3=0;leddata=0xff;delays(10);led3=1;
}
else
{
leddata=tab[aminute/16];led2=0;delays(10);
if(flag==1){if(led2==0)P1_2=0;else P1_2=1;};delays(10);led2=1;
leddata=tab[aminute%16];led3=0;delays(10);
if(flag==1){if(led3==0)P1_2=0;else P1_2=1;};delays(10);led3=1;
}
}
void display2(void)
{
readtime();
if(flag&second)
{
led2=0;leddata=0xff;delays(10);led2=1;
led3=0;leddata=0xff;delays(10);led3=1;
}
else
{
leddata=tab[asecond/16];led2=0;delays(10);
if(flag==1){if(led2==0)P1_2=0;else P1_2=1;};delays(10);led2=1;
leddata=tab[asecond%16];led3=0;delays(10);
if(flag==1){if(led3==0)P1_2=0;else P1_2=1;};delays(10);led3=1;
}
}
void display3(void)
{
readtime();
if(flag&mouth)
{
led0=0;leddata=0xff;delays(10);led0=1;
led1=0;leddata=0xff;delays(10);led1=1;
}
else
{
leddata=tab[amouth/16];led0=0;delays(10);
delays(10);led0=1;
leddata=tab[amouth%16];led1=0;delays(10);
delays(10);led1=1;
}
if(flag&date)
{
led2=0;leddata=0xff;delays(10);led2=1;
led3=0;leddata=0xff;delays(10);led3=1;
}
else
{
leddata=tab[adate/16];led2=0;delays(10);
if(flag==1){if(led2==0)P1_2=0;else P1_2=1;};delays(10);led2=1;
leddata=tab[adate%16];led3=0;delays(10);led3=1;
}
}
void display4(void)
{
readtime();
if(flag&year)
{
led0=0;leddata=0xff;delays(10);led0=1;
led1=0;leddata=0xff;delays(10);led1=1;
led2=0;leddata=0xff;delays(10);led2=1;
led3=0;leddata=0xff;delays(10);led3=1;
}
else
{
leddata=tab[2];led0=0;delays(10);delays(10);led0=1;
leddata=tab[0];led1=0;delays(10);delays(10);led1=1;
leddata=tab[ayear/16];led2=0;delays(10);delays(10);led2=1;
leddata=tab[ayear%16];led3=0;delays(10);delays(10);led3=1;
}
}
void display5(void)
{
readtime();
if(flag&week)
{
led0=0;leddata=0xff;delays(10);led0=1;
led1=0;leddata=0xff;delays(10);led1=1;
led2=0;leddata=0xff;delays(10);led2=1;
led3=0;leddata=0xff;delays(10);led3=1;
}
else
{
leddata=0xfe;led0=0;delays(10);delays(10);led0=1;
leddata=0xfe;led1=0;delays(10);delays(10);led1=1;
leddata=tab[aweek%16];led2=0;delays(10);delays(10);led2=1;
leddata=0xfe;led3=0;delays(10);delays(10);led3=1;
}
}
void display(void)
{ switch(id)
{case 0: display1();break;
case 1: display1();break;
case 2: display1();break;
case 3: display2();break;
case 4: display3();break;
case 5: display3();break;
case 6: display4();break;
case 7: display5();break;
default:break;
}
}
void Scan_Key(void)
{
display();
if(id==0)
{if (!UP)
{
while(!UP)display3();
re_disp=0;
}
}
if(!SET)
{
while(!SET) display();
re_disp=0;
id++;if(id>7)id=0;
}
if(id==0){year=0;mouth=0;date=0;week=0;hour=0;minute=0;second=0;}
if(id==1){hour=1;id_case1_key();}
if(id==2){hour=0;minute=1;id_case1_key();}
if(id==3){minute=0;second=1;id_case1_key();}
if(id==4){second=0;mouth=1;id_case1_key();}
if(id==5){mouth=0;date=1;id_case1_key();}
if(id==6){date=0;year=1;id_case1_key();}
if(id==7){year=0;week=1;id_case1_key();}
}
void id_case1_key(void)
{
if (!UP)
{
while(!UP)display();
re_disp=0;
Set_id(id,1);
}
}
void Set_id(unsigned char sel,unsigned char sel_1)
{
signed char max,mini,address,item;
if(sel==1) {address=hou; max=23;mini=0;}
if(sel==2) {address=min; max=59;mini=0;}
if(sel==3) {address=sec; max=0;mini=0;}
if(sel==4) {address=mou; max=12;mini=1;}
if(sel==5) {address=dat; max=31;mini=1;}
if(sel==6) {address=yer; max=99;mini=0;}
if(sel==7) {address=wek; max=7;mini=1;}
leddata=0xff;
item=Read1302(address|read)/16*10+Read1302(address|read)%16;
if (sel_1==1) item++;
if(item>max) item=mini;
leddata=0xff;
Write1302(0x8e,0x00);
Write1302(address,item/10*16+item%10);
Write1302(0x8e,0x80);
}
void main()
{
TMOD=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;
ET0=1;
TR0=1;
Write1302(0x90,0xa0);
if(!SET&!UP)Set1302(inittime);
Write1302(0x8e,0x80);
while(1)
{
Scan_Key();
}
}
/*一共两按键,1按键选功能,2按键调节,调节顺序时,分,
秒,月,日,年,周,不在调节功能时按2键显示月日,两键
同时按初始化DS1302*/
#include <AT89X51.H>
#include "ds1302.h"
#define leddata P1
#define sec 0x80
#define min 0x82
#define hou 0x84
#define dat 0x86
#define mou 0x88
#define wek 0x8a
#define yer 0x8c
#define read 0x01
sbit SET=P3^1;
sbit UP=P3^2;
sbit led0=P3^4;
sbit led1=P3^3;
sbit led2=P3^5;
sbit led3=P3^7;
//**************函数声明*****************
void delays(unsigned char);
void display(void);
void Scan_Key(void);
void id_case1_key();
void Set_id(unsigned char ,unsigned char);
//*************变量定义******************
unsigned char id=0,timecount,re_disp=0,ahour=0,aminute=0,asecond=0,adate=0,amouth=0,aweek=0,ayear=0;
bit year,mouth,date,week,hour,minute,second,flag,tlamp;
unsigned char code tab[]={
0x05,/*0*/
0xDD,/*1*/
0x46,/*2*/
0x54,/*3*/
0x9C,/*4*/
0x34,/*5*/
0x24,/*6*/
0x5D,/*7*/
0x04,/*8*/
0x14,/*9*/
}; /*个位0~9的数码管段码*/
unsigned char inittime[7]={0x00,0x00,0x12,0x07,0x03,0x08,0x04};
// 秒 分钟 小时 日 月 年 星期
void t0(void) interrupt 1 using 0
{
TH0=(65535-59524)/256;
TL0=(65535-59524)%256;
timecount++;re_disp++;
if(timecount>9)
{
timecount=0;
flag=~flag;
}
if(re_disp>200){re_disp=0;if(id)id=0;}
}
void delays(unsigned char k)
{
unsigned char i,j;
for(i=0;i<k;i++)
for(j=0;j<50;j++);
}
void readtime(void)
{leddata=0xff;
adate=Read1302(dat|read);
amouth=Read1302(mou|read);
aweek=Read1302(wek|read);
ayear=Read1302(yer|read);
ahour=Read1302(hou|read);
aminute=Read1302(min|read);
asecond=Read1302(sec|read);
}
void display1(void)
{
readtime();
if(flag&hour)
{
led0=0;leddata=0xff;delays(10);led0=1;
led1=0;leddata=0xff;delays(10);led1=1;
}
else
{
leddata=tab[ahour/16];led0=0;delays(10);
delays(10);led0=1;
leddata=tab[ahour%16];led1=0;delays(10);
delays(10);led1=1;
}
if(flag&minute)
{
led2=0;leddata=0xff;delays(10);led2=1;
led3=0;leddata=0xff;delays(10);led3=1;
}
else
{
leddata=tab[aminute/16];led2=0;delays(10);
if(flag==1){if(led2==0)P1_2=0;else P1_2=1;};delays(10);led2=1;
leddata=tab[aminute%16];led3=0;delays(10);
if(flag==1){if(led3==0)P1_2=0;else P1_2=1;};delays(10);led3=1;
}
}
void display2(void)
{
readtime();
if(flag&second)
{
led2=0;leddata=0xff;delays(10);led2=1;
led3=0;leddata=0xff;delays(10);led3=1;
}
else
{
leddata=tab[asecond/16];led2=0;delays(10);
if(flag==1){if(led2==0)P1_2=0;else P1_2=1;};delays(10);led2=1;
leddata=tab[asecond%16];led3=0;delays(10);
if(flag==1){if(led3==0)P1_2=0;else P1_2=1;};delays(10);led3=1;
}
}
void display3(void)
{
readtime();
if(flag&mouth)
{
led0=0;leddata=0xff;delays(10);led0=1;
led1=0;leddata=0xff;delays(10);led1=1;
}
else
{
leddata=tab[amouth/16];led0=0;delays(10);
delays(10);led0=1;
leddata=tab[amouth%16];led1=0;delays(10);
delays(10);led1=1;
}
if(flag&date)
{
led2=0;leddata=0xff;delays(10);led2=1;
led3=0;leddata=0xff;delays(10);led3=1;
}
else
{
leddata=tab[adate/16];led2=0;delays(10);
if(flag==1){if(led2==0)P1_2=0;else P1_2=1;};delays(10);led2=1;
leddata=tab[adate%16];led3=0;delays(10);led3=1;
}
}
void display4(void)
{
readtime();
if(flag&year)
{
led0=0;leddata=0xff;delays(10);led0=1;
led1=0;leddata=0xff;delays(10);led1=1;
led2=0;leddata=0xff;delays(10);led2=1;
led3=0;leddata=0xff;delays(10);led3=1;
}
else
{
leddata=tab[2];led0=0;delays(10);delays(10);led0=1;
leddata=tab[0];led1=0;delays(10);delays(10);led1=1;
leddata=tab[ayear/16];led2=0;delays(10);delays(10);led2=1;
leddata=tab[ayear%16];led3=0;delays(10);delays(10);led3=1;
}
}
void display5(void)
{
readtime();
if(flag&week)
{
led0=0;leddata=0xff;delays(10);led0=1;
led1=0;leddata=0xff;delays(10);led1=1;
led2=0;leddata=0xff;delays(10);led2=1;
led3=0;leddata=0xff;delays(10);led3=1;
}
else
{
leddata=0xfe;led0=0;delays(10);delays(10);led0=1;
leddata=0xfe;led1=0;delays(10);delays(10);led1=1;
leddata=tab[aweek%16];led2=0;delays(10);delays(10);led2=1;
leddata=0xfe;led3=0;delays(10);delays(10);led3=1;
}
}
void display(void)
{ switch(id)
{case 0: display1();break;
case 1: display1();break;
case 2: display1();break;
case 3: display2();break;
case 4: display3();break;
case 5: display3();break;
case 6: display4();break;
case 7: display5();break;
default:break;
}
}
void Scan_Key(void)
{
display();
if(id==0)
{if (!UP)
{
while(!UP)display3();
re_disp=0;
}
}
if(!SET)
{
while(!SET) display();
re_disp=0;
id++;if(id>7)id=0;
}
if(id==0){year=0;mouth=0;date=0;week=0;hour=0;minute=0;second=0;}
if(id==1){hour=1;id_case1_key();}
if(id==2){hour=0;minute=1;id_case1_key();}
if(id==3){minute=0;second=1;id_case1_key();}
if(id==4){second=0;mouth=1;id_case1_key();}
if(id==5){mouth=0;date=1;id_case1_key();}
if(id==6){date=0;year=1;id_case1_key();}
if(id==7){year=0;week=1;id_case1_key();}
}
void id_case1_key(void)
{
if (!UP)
{
while(!UP)display();
re_disp=0;
Set_id(id,1);
}
}
void Set_id(unsigned char sel,unsigned char sel_1)
{
signed char max,mini,address,item;
if(sel==1) {address=hou; max=23;mini=0;}
if(sel==2) {address=min; max=59;mini=0;}
if(sel==3) {address=sec; max=0;mini=0;}
if(sel==4) {address=mou; max=12;mini=1;}
if(sel==5) {address=dat; max=31;mini=1;}
if(sel==6) {address=yer; max=99;mini=0;}
if(sel==7) {address=wek; max=7;mini=1;}
leddata=0xff;
item=Read1302(address|read)/16*10+Read1302(address|read)%16;
if (sel_1==1) item++;
if(item>max) item=mini;
leddata=0xff;
Write1302(0x8e,0x00);
Write1302(address,item/10*16+item%10);
Write1302(0x8e,0x80);
}
void main()
{
TMOD=0x01;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
EA=1;
ET0=1;
TR0=1;
Write1302(0x90,0xa0);
if(!SET&!UP)Set1302(inittime);
Write1302(0x8e,0x80);
while(1)
{
Scan_Key();
}
}
好像没看到有什么报酬
改好了,送100积分。
...消影了吗
不懂什么意思
小编使用延时做数码管位数多了会遇到问题的,及时解决了也是治标不治本。我提供一个我自己的思路,仅供参考。如下:
- void Display(void)
- {
- //关闭所有位选
- if(step == 1)
- {
- //打开该位使能
- P2=table[buffer[0]];//小时高
- }
- if(step == 2)
- {
- //打开该位使能
- P2=table[buffer[10]]; //小时低
- }
- if(step == 3)
- {
- //打开该位使能
- P2=table[buffer[2]]; //分钟高
- }
- if(step == 4)
- {
- //打开该位使能
- P2=table[buffer[3]]; //分钟低
- }
- //类似的继续拓展
- }
- //ReadTime函数每200ms执行一次就行了
- void ReadTime(void)
- {
- //从1302中读取数据,将
- //数据放在buffer数组中。
- }
- void t0(void) interrupt 1 using 0
- {//10ms或者更久
- //重载计数值
- step++;
- Display();
- }
对了。中断我复制的你的名字,把那个using 0也去掉
你好,有空我把电子钟全套资料发给你看看,我不会编程,改好了我送你结分,多少你说了算
小编你好啊,刚看到,说实话不是很想调,最近手头项目搞的也很头大,小编谅解哈。提供个思路还是没问题的。
好的,谢谢你