DS1302 可以正常显示时间,但是不能调整时间(调整后的时钟写入DS1302的程序还没写),看不出来时钟调整那块出了什么问题,大家帮忙看看吧 !谢谢#include"reg52.h"#include"intrins.h"#define uchar unsigned char #define uint unsigned int
uchar ,flag;uint hour,min,sec,;uchar m[3]; //秒,分,时寄存器初始值
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}
;
sbit DIO=P3^4;sbit SCL=P3^6;sbit CE=P3^5;sbit k1=P3^0;sbit k2=P3^1;sbit k3=P3^2;sbit k4=P3^3;sbit ACC_7=ACC^7;
//最好用ACC,不然有问题/*----------宏定义读写 秒,分,时,日期的寄存器地址和控制字-----*/#define write_sec 0x80#define write_min 0x82#define
write_hour 0x84#define write_day 0x86#define write_month 0x88#define write_week 0x8a#define write_year 0x8c#define read_second 0x81#define read_minute 0x83#define read_hour 0x85#define read_day 0x87#define read_month 0x89#define read_week 0x8B#define read_year 0x8D#define write_protect 0x8Evoid delay(){ uchar i,j; for(i=0;i<10;i++) for(j=0;j<10;j++);}
void writebyte(uchar dat){
uchar i,tmp;
tmp=dat;
SCL=0;
for(i=8;i>0;i--)
{
DIO=tmp&0x01;
SCL=0;
SCL=1;
tmp>>=1;
}
}void writedata(uchar add,uchar dat){
CE=0;
SCL=0;
CE=1;
writebyte(add);
writebyte(dat);
SCL=1;
CE=0;}uchar readdata(uchar add){
uchar j,dat;
CE=0; //终止传输
SCL=0;
CE=1; //开始传输
writebyte(add);
for(j=8;j>0;j--)
{
dat>>=1;
SCL=1;
SCL=0;
if(DIO)dat|=0x80;
}
SCL=1;
CE=0;
return dat;}
void dis(){
uint a,b,c;
if(flag==0)
{
hour=m[2];
min=m[1];
sec=m[0];
a=hour=(hour/16)*10+hour%16;
b=min=(min/16)*10+min%16;
c=sec=(sec/16)*10+sec%16;
}
if(flag==1)
{
a=hour;
b=min;
c=0;
}
P0=table[a/10];
P2=0;
delay();
P0=table[a%10];
P2=1;
delay();
P0=0x40;
P2=2;
delay();
P0=table[b/10];
P2=3;
delay();
P0=table[b%10];
P2=4;
delay();
P0=0x40;
P2=5;
delay();
P0=table[c/10];
P2=6;
delay();
P0=table[c%10];
P2=7;
delay();}void delay1(){
uchar i,j; for(i=0;i<10;i++) for(j=0;j<20;j++)dis();}void key() {
dis();
if(k1==0)
delay1();
if(k1==0)flag++;
if(flag==1)
{
if(k2==0)
delay1();
if(k2==0)hour++ ;
if(hour>24)hour=0;
if(k3==0)
delay1();
if(k3==0)hour-- ;
if(hour<0)hour=23;
dis();
}
if(flag==2)
{
if(k2==0)
delay1();
if(k2==0)min++ ;
if(hour>59)min=0;
if(k3==0)
delay1();
if(k3==0)min-- ;
if(hour<0)hour=59;
dis();
}
if(flag=3)flag=0;
}
void init(){
writedata(0x8e,0x00); //WP=0 写操作
writedata(0x80,0x00);//0x80是写秒数据此处写进"00"秒
writedata(0x82,0x59);//0x82是写分数据
writedata(0x84,0x23);//0x84是写时数据
writedata(0x8e,0x80); //WP=1 写保护 }void main(){ uchar i,add; init();
while(1)
{
add=0x81;
for(i=0;i<3;i++)
{
m
=readdata(add); add+=2; } key(); } }
此帖出自小平头技术问答
一周热门 更多>