|
最近剛學(xué)單片機(jī),今天試著對DS1302時(shí)鐘編輯,想初始化秒,然后實(shí)時(shí)用數(shù)碼管顯示,但是試了很久,一直沒有成功,大家?guī)兔匆幌履睦锍隽藛栴}。
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
sbit dat=P2^1;
sbit clock=P2^0;
sbit a1=P2^2;
sbit a2=P2^3;
sbit a3=P2^4;
sbit a4=P2^5;
sbit RST=P2^6;
//--定義全局變量--//
unsigned char code DIG_CODE[17]={
0xf9, 0xa4, 0xb0, 0x93, 0x92, 0x82,
0xf8, 0x00, 0x90, 0x88, 0x83, 0xc6,
0xa1, 0x86, 0x8e, 0xc0};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的顯示碼
void write(uchar a)//寫操作
{
uchar i;
for(i=0;i<8;i++)
{
clock=0;
_nop_();_nop_();
dat=a&0x01;
_nop_();_nop_();
clock=1;//發(fā)送數(shù)據(jù)
a>>=1;
}
}
//讀操作
uchar read(uchar a)
{
uchar i,dat1,m;
for(i=0;i<8;i++)
{
clock=0;
_nop_();_nop_();
dat=a&0x01;
_nop_();_nop_();
clock=1;//發(fā)送數(shù)據(jù)
a>>=1;
}
for(i=0;i<8;i++)
{
clock=0; //接收數(shù)據(jù)
_nop_();_nop_();
m=dat;
_nop_();_nop_();
dat1=(dat1>>1)|(m<<7);
clock=1;
}
return dat1;
}
void delay(uchar c)
{uchar i;
for(;c>0;c--)
{for(i=0;i<50;i++);}}
void main()
{
uchar k,h,l;
RST=1; //關(guān)閉寫保護(hù)
write(0x8e);
write(0x00);
RST=0;
delay(5);
RST=1; //初始化秒
write(0x80);
write(0x00);
RST=0;
delay(5);
while(1)
{
k=0;
RST=1; //讀取數(shù)據(jù)
k=read(0x81);
delay(5);
h=k/16;
l=k&0x0f;
a1=a2=a3=a4=0;
P1=DIG_CODE[h];a3=1;
delay(20);
a1=a2=a3=a4=0;
P1=DIG_CODE[l];a4=1;
delay(20);}
}
|
|