问题
1.是写入单片机仿真版的时候显示000000,但是重新接入电脑的时候就可以显示时间了,
2.在进入调分钟的时候调节不了
要求;显示时间,如果P3.1可以进入分调节(进入调节分所显示的时间是0.5秒闪烁,并在没按下p3.2进行增加1会在10s内退出调节状态),现在是写到增是出现了问题,想了很久都没想到是为什么?(p2是输出端)
#include<reg51.h>
#include<intrins.h>
/*----------------------------------------------------------------------*/
#define uchar unsigned char
sbit sck=P0^0;
sbit io=P0^2;
sbit rst=P0^1;
sbit smclk=P1^1;//锁存器
sbit smclk1=P1^2;//锁存器
sbit tiaotime_up=P3^2;
sbit tiaotime_down=P3^3;
sbit p3_0=P3^0;
sbit p3_1=P3^1;
bit flag0_5=0;
bit keyflag=0;
uchar code seat[]={0x20,0x10,0x08,0x04,0x02,0x01};
uchar sumg[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar time_data[]={23,50,21};
uchar write_add[]={0x80,0x82,0x84};
uchar read_add[]={0x81,0x83,0x85};
uchar disp[8],timetemp_m=0,timetemp_tenm=0;
unsigned char DisCnt=0,interm=0,temp;
unsigned int flag_10s=0;
unsigned char key=0,time_m;
/*----------------------------------------------------------------------*/
void write_ds1302_byte(uchar dat);
void write_ds1302(uchar add,uchar dat);
uchar read_ds1302(uchar add);
void set_rtc();
void read_rtc();
void time_pros();
void display();
void keycan();
/*----------------------------------------------------------------------*/
/*void keycan()
{
P3=0x0f;
_nop_();
_nop_();
temp=P3;
switch(temp)
{
case 0x0e:
keyflag=~keyflag;
key=1;
break;
case 0x0d:
keyflag=~keyflag;
key=2;
break;
}
}
*/
/*------------------在屏幕上显示时间----------------------------------------------------*/
void int_2ms() interrupt 1 using 0
{
TF0=0;
TR0 = 0;
TH0=0Xf8;
TL0=0Xc5;
TR0 = 1;
P2=0x00;
smclk1=1;
_nop_();
smclk1=0;
P2=0xff;
smclk=1;
_nop_();
smclk=0;
switch(DisCnt)
{
case 0:
if(keyflag==1&&key==1&&flag0_5==0)
{
P2=0x00;
smclk1=1;
_nop_();
smclk1=0;
P2=0xff;
smclk=1;
_nop_();
smclk=0;
}
else
{
P2=sumg[disp[0]];
smclk1=1;
_nop_();
smclk1=0;
P2=~seat[0];
smclk=1;
_nop_();
smclk=0;
}
break;
case 1:
if(keyflag==1&&key==1&&flag0_5==0)
{
P2=0x00;
smclk1=1;
_nop_();
smclk1=0;
P2=0xff;
smclk=1;
_nop_();
smclk=0;
}
else
{
P2=sumg[disp[DisCnt]];
smclk1=1;
_nop_();
smclk1=0;
P2=~seat[DisCnt];
smclk=1;
_nop_();
smclk=0;
}
break;
case 2:
if(keyflag==1&&key==2&&flag0_5==0)
{
P2=0x00;
smclk1=1;
_nop_();
smclk1=0;
P2=0xff;
smclk=1;
_nop_();
smclk=0;
}
else
{
P2=sumg[disp[DisCnt]]|0x80;
smclk1=1;
_nop_();
smclk1=0;
P2=~seat[DisCnt];
smclk=1;
_nop_();
smclk=0;
}
break;
case 3:
if(keyflag==1&&key==2&&flag0_5==0)
{
P2=0x00;
smclk1=1;
_nop_();
smclk1=0;
P2=0xff;
smclk=1;
_nop_();
smclk=0;
}
else
{
P2=sumg[disp[DisCnt]];
smclk1=1;
_nop_();
smclk1=0;
P2=~seat[DisCnt];
smclk=1;
_nop_();
smclk=0;
}
break;
case 4:
P2=sumg[disp[DisCnt]]|0x80;
smclk1=1;
_nop_();
smclk1=0;
P2=~seat[DisCnt];
smclk=1;
_nop_();
smclk=0;
break;
case 5:
P2=sumg[disp[DisCnt]];
smclk1=1;
_nop_();
smclk1=0;
P2=~seat[DisCnt];
smclk=1;
_nop_();
smclk=0;
break;
default:
DisCnt=0;
if(keyflag==1&&key==1&&flag0_5==0)
{
P2=0x00;
smclk1=1;
_nop_();
smclk1=0;
P2=0xff;
smclk=1;
_nop_();
smclk=0;
}
else
{
P2=sumg[disp[DisCnt]];
smclk1=1;
_nop_();
smclk1=0;
P2=~seat[DisCnt];
smclk=1;
_nop_();
smclk=0;
}
break;
}
DisCnt++;
interm++;
if(interm==250)
{
interm=0;
flag0_5=~flag0_5;
}
if(keyflag==1)
{
if( tiaotime_up==0|| tiaotime_down==0)
{
flag_10s=0;
}
else
{
flag_10s++;
if(flag_10s==5000)
{
flag_10s=0;
keyflag=~keyflag;
}
}
}
}
/*-----------------单字节的写入ds1302-----------------------------------------------------*/
void write_ds1302_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
sck=0;
_nop_();
_nop_();
io=dat&0x01;
dat=dat>>1;
_nop_();
sck=1;
}
}
/*--------------------写入数据--------------------------------------------------*/
void write_ds1302(uchar add,uchar dat)
{
rst=0;
_nop_();
_nop_();
sck=0;
_nop_();
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
write_ds1302_byte(dat);
rst=0;
_nop_();
_nop_();
io=1;
sck=1;
}
/*--------------------------读取数据--------------------------------------------*/
uchar read_ds1302(uchar add)
{
uchar i,value;
rst=0;
_nop_();
_nop_();
sck=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
for(i=0;i<8;i++)
{
value=value>>1;
sck=0;
_nop_();
_nop_();
if(io)
value=value|0x80;
sck=1;
}
rst=0;
_nop_();
_nop_();
sck=0;
_nop_();
_nop_();
sck=1;
io=1;
return value;
}
/*----------------------把设定的时间写入ds1302------------------------------------------------*/
void set_rtc()
{
uchar j,i;
for(i=0;i<3;i++)
{
j=time_data
/10;
time_data=time_data%10;
time_data=time_data+j*16;
}
write_ds1302(0x8e,0x00);
write_ds1302(write_add[0],time_data[0]);
write_ds1302(write_add[1],time_data[1]);
write_ds1302(write_add[2],time_data[2]);
write_ds1302(0x8e,0x80);
}
/*---------------------读入ds1302的时间数据-------------------------------------------------*/
void read_rtc()
{
time_data[0]= read_ds1302(read_add[0]);
time_data[1]= read_ds1302(read_add[1]);
time_data[2]= read_ds1302(read_add[2]);
}
/*------------------------输出数字处理----------------------------------------------*/
void time_pros()
{
disp[0]=time_data[0]%16;
disp[1]=time_data[0]/16;
disp[2]=time_data[1]%16;
disp[3]=time_data[1]/16;
disp[4]=time_data[2]%16;
disp[5]=time_data[2]/16;
}
/*----------------------------------------------------------------------*/
void main()
{
TMOD=0X01;
TH0=0Xf8;
TL0=0X30;
TR0=1;
EA=1;
ET0=1;
P1=0;
P2=0;
set_rtc();
while(1)
{
read_rtc();
time_pros();
/*---------------------------按键输入程序-------------------------------------------*/
P3=0x0f;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
temp=P3;
switch(temp)
{
case 0x0e:
keyflag=~keyflag;
key=1;
break;
case 0x0d:
keyflag=~keyflag;
key=2;
break;
case 0x0b:
if(keyflag)
{
if(key==2)
{
write_ds1302(0x8e,0x00);
time_m=read_ds1302(0x83);
timetemp_m=time_m&0x0f;
timetemp_tenm=time_m&0x70;
timetemp_tenm>>=1;
timetemp_tenm>>=1;
timetemp_tenm>>=1;
timetemp_tenm>>=1;
if(timetemp_m<9)
{
timetemp_m++;
}
else
{
timetemp_m=0;
timetemp_tenm++;
if(timetemp_tenm<6)
{
timetemp_tenm++;
}
else
{
timetemp_tenm=0;
}
}
timetemp_tenm<<=1;
timetemp_tenm<<=1;
timetemp_tenm<<=1;
timetemp_tenm<<=1;
time_m=timetemp_m|timetemp_tenm;
write_ds1302(0x82,time_m);
write_ds1302(0x8e,0x80);
}
}
break;
}
}
}
[ 本帖最后由 林发扬 于 2011-8-20 17:58 编辑 ]
此帖出自小平头技术问答
一周热门 更多>