慧净电子吧 关注:448贴子:1,233
  • 0回复贴,共1

DS1032数码管显示,不知道错在哪里

只看楼主收藏回复

#include<reg52.h>
#include<intrins.h>
typedef unsigned char uchar ;
typedef unsigned int uint;
//void init_1302( );
void write_1302dat(uchar cmd,uchar dat);
uchar read_1302dat( uchar dat);
uchar octtobcd(uchar dat);
uchar bcdtooct(uchar dat);
//uchar hour(uchar sec,uchar min,uchar hou);
uchar duxianshi[8];
//uchar code duxianshi[]={0x06,0x4f,0x06,0x66,0x6d,0x5b,0x3f,0x3f};
//uchar code duxianshi[ ]={hou/10,hou%10,0x40,min/10,min%10,0x40,sec/10,sec%10 };
uchar code wexianshi[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//1111 1110 11111101
sbit du=P2^0;
sbit we=P2^1;
sbit cs88=P2^2;
sbit DS1302_SCK=P1^7;
sbit DS1302_IO=P2^7; //实时时钟线数据引脚
sbit DS1302_RST = P1^2;
//uint i,j,k,m;
void delay(k);
void main()
{
uchar sec,min,hou, i;
//init_1302( );
cs88=0;
we=1;
P0=0xff;
we=0;
write_1302dat(0x8e,0x00);
write_1302dat(0x80,octtobcd(30));//30秒
write_1302dat(0x82,octtobcd(48));
write_1302dat(0x84,octtobcd(12));
write_1302dat(0x8e,0x80);
while(1)
{
write_1302dat(0x8e,0x00);
sec=bcdtooct(read_1302dat(0x81));
min=bcdtooct(read_1302dat(0x83));
hou=bcdtooct(read_1302dat(0x85));
write_1302dat(0x8e,0x80);
duxianshi[7]=sec%10;
duxianshi[6]=sec/10;
duxianshi[5]=0x40;
duxianshi[4]=min%10;
duxianshi[3]=min/10;
duxianshi[2]=0x40;
duxianshi[1]=hou%10;
duxianshi[0]=hou/10;
delay(2);
for(i=0;i<8;i++)
{
du=1;
P0=duxianshi[i];
du=0;
P0=0xff; //不及时清除内存,下面的数据可能显示的是”或“的运算结果
we=1;
P0=wexianshi[i];
we=0;
delay(5);}
}
}
/******************************************************************************************/
/******************************延时语句*********************************************/
/******************************************************************************************/
/******************************延时语句*********************************************/
void delay(uchar k)//人的眼睛驻留时间小于20ms,所以这个数值不能太长
{
uchar m,j;
for(m=0;m<k;m++)
{for(j=0;j<20;j++);}
}
/******************************************************************************************/
/******************************写一个字节数据*********************************************/
void write_1302dat(uchar cmd,uchar dat)
{
uchar i;
DS1302_RST = 0;
DS1302_SCK=0;
DS1302_RST = 1;
for (i=0;i<8;i++)
{
DS1302_SCK=0;
DS1302_IO=cmd&0x01;
DS1302_SCK=1;
cmd>>=1;
}
for (i=0;i<8;i++)
{
DS1302_SCK=0;
DS1302_IO=dat&0x01;
DS1302_SCK=1;
dat>>=1;
}
}
/******************************************************************************************/
/******************************读一个字节数据*********************************************/
uchar read_1302dat( uchar cmd)
{
uchar i,dat;
DS1302_RST = 0;
DS1302_SCK=0;
DS1302_RST = 1;
for (i=0;i<8;i++)
{
DS1302_SCK=0;
DS1302_IO=cmd&0x01;
DS1302_SCK=1;
cmd>>=1;
}
for (i=0;i<8;i++)
{
//dat=(DS1302_IO&(dat<<=7));
DS1302_SCK=0;
dat>>=1;
if(DS1302_IO)dat|=0x80;
DS1302_SCK=1;
//dat>>=1;
}
//DS1302_SCK=0;
return dat;
}
/******************************************************************************************/
/******************************数据进制的转化过程*********************************************/
uchar octtobcd(uchar dat)
{
uchar dat1,dat2;
dat1=dat/10;
dat2=dat%10;
dat2=dat2+dat1*16;
return dat2;
}
uchar bcdtooct(uchar dat)
{
uchar dat1,dat2;
dat1=dat/16;
dat2=dat%16;
dat2=dat2+dat1*10;
return dat2;
}


IP属地:广东1楼2024-11-25 16:54回复