关于时钟芯片1302的问题,欢迎各种神,门神也不拒!
时间:10-02
整理:3721RD
点击:
怎么我写的一个由时钟芯片控制的程序在液晶1602显示,秒竟然是到89分才进1,而且是从57一下调到64,偶的神啊,救救我吧!程序如下:
/************************************************************************/
// huaqinMCU DS1302 实验程序 数码管显示 时钟设置说明
// "0键"为时钟设置、时分切换、保存 "4键"为加 "8键"为减
/************************************************************************/
#include <reg52.h>
#include<timedelay.c>
extern void timedelay(int a);
#define uchar unsigned char
//===以下IO定义请根据您硬件的连接修改===
sbit T_RST=P3^5;//ds1302-5
sbit T_IO=P3^4;//ds1302-6
sbit T_CLK=P3^6;//ds1302-7
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;//累加器A 51单片机原理中有介绍
uchar code table1[7][3]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
sbit rs=P2^4;
sbit rw=P2^5;
sbit en=P2^6;
sbit key1=P3^0;//功能选择键
sbit key2=P3^1;//数值加一
sbit key3=P3^2;//数值减一
//sbit beep=P1^5;
uchar temp,a,b,year,k,i,mon,date,week,j,sec,min,hou;
uchar clk_time[7]; //秒,分,时寄存器初始值
void delay()
{;;}
/******************DS1302:写入操作(上升沿)*********************/
void write_byte11(uchar da)
{
uchar i;
ACC=da;//10000001
for(i=8;i>0;i--)
{
T_IO=ACC0;
T_CLK=0;
T_CLK=1;
ACC=ACC>>1;//01000000
}
}
/******************DS1302:读取操作(下降沿)*****************/
uchar read_byte11(void)
{
uchar i;
for(i=0;i<8;i++)//00000001 假设ACC=00000000
{
ACC=ACC>>1;//01000000
T_CLK = 1;
T_CLK = 0;
ACC7 = T_IO;//10000000
}
return(ACC);
}
/******************DS1302:写入数据(先送地址,再写数据)***************************/
void write_1302(uchar addr,uchar da)
{
T_RST=0; //停止工作
T_CLK=0;
T_RST=1; //重新工作
write_byte11(addr); //写入地址
write_byte11(da);
T_RST=0;
T_CLK=1;
}
/******************DS1302:读取数据(先送地址,再读数据)**************************/
uchar read_1302(uchar addr)
{
uchar temp1;
T_RST=0; //停止工作
T_CLK=0;
T_RST=1; //重新工作
write_byte11(addr); //写入地址
temp1=read_byte11();
T_RST=0;
T_CLK=1; //停止工作
return(temp1);
}
/******************1602液晶显示****************/
void write_com(uchar date1)
{
rs=0;//rs=0,写命令//
en=0;
P0=date1;//将数据送进总线//
timedelay(5);
en=1;//使能端给一高脉冲将数据送走//
timedelay(5);
en=0;//将使能端置低以完成高脉冲//
}
//写数据//
void write_byte1(uchar date2)
{
rs=1;//写数据命令//
en=0;
P0=date2;//将数据送往总线//
timedelay(5);
en=1;//将使能端至高,给一高脉冲,将数据送走//
timedelay(5);
en=0;//将使能端置低以完成高脉冲//
}
//将数字送进液晶显示//
void write_(uchar shu)
{
uchar sw,gw;
sw=shu/10;
gw=shu%10;
write_byte1(0x30+sw);//将十位送进液晶显示//
timedelay(5);
write_byte1(0x30+gw);//将个位送进液晶显示//
timedelay(5);
}
//初始化//
void init()
{
en=0;
rw=0;
write_com(0x38);//设置16*2显示,5*7点阵,8位数据接口//
write_com(0x0c);//设置开显示,不显示光标//
write_com(0x06);//写一个字符后地址指针加一//
write_com(0x01);//显示清零,数据指针清零//
}
void yejing()
{
write_com(0x80+0x45);//写显示“:”的地址//
write_byte1(':');//显示://
timedelay(5);
write_com(0x80+0x48);//写显示“:”的地址//
write_byte1(':'); //显示://
timedelay(5);
write_com(0x80+0x05);//显示——地址//
write_byte1('-');
timedelay(5);
write_com(0x80+0x08);
write_byte1('-');
timedelay(5);
}
void yejing2()
{
write_com(0x80+0x43);
write_(hou);
timedelay(5);
write_com(0x80+0x46);
write_(min);
timedelay(5);
write_com(0x80+0x49);
write_(sec);
timedelay(5);
write_com(0x80+0x0c);
for(i=0;i<3;i++)
{
write_byte1(table1[week]);//显示week//
timedelay(5);
}
write_com(0x80+0x01);
write_(year);
timedelay(5);
write_com(0x80+0x06);
write_(mon);
timedelay(5);
write_com(0x80+0x09);
write_(date);
timedelay(5);
}
/********************按键程序******************/
void keyscan()
{
if(key1==0)
{
timedelay(10);
if(key1==0)
{
k++;
while(!key1);
if(k==1)
{
write_com(0x80+0x49);//显示位置
write_com(0x0f);//闪烁光标
}
if(k==2)
{
write_com(0x80+0x46);
write_com(0x0f);
}
if(k==3)
{
write_com(0x80+0x43);
write_com(0x0f);
}
if(k==4)
{
write_com(0x80+0x0c);
write_com(0x0f);
}
if(k==5)
{
write_com(0x80+0x09);
write_com(0x0f);
}
if(k==6)
{
write_com(0x80+0x06);
write_com(0x0f);
}
if(k==7)
{
write_com(0x80+0x03);
write_com(0x0f);
}
if(k==8)
{
write_com(0x0c);
k=0;
}
}
}
if(k!=0)
{if(key2==0)
{
timedelay(10);
if(key2==0)
{
while(!key2);
write_1302(0x8e,0x00); //WP=0 写操作
if(k==1)
{
sec++;
if(sec==60) sec=0;
write_com(0x80+0x49);
write_(sec);
write_1302(0x80,sec);
}
if(k==2)
{
min++;
if(min==60) min=0;
write_com(0x80+0x46);
write_(min);
write_1302(0x82,min);
}
if(k==3)
{
hou++;
if(hou==24) hou=0;
write_com(0x80+0x43);
write_(hou);
write_1302(0x84,hou);
}
if(k==4)
{
week++;
if(week==7) week=0;
write_com(0x80+0x0c);
for(i=0;i<3;i++)
{
write_byte1(table1[week]);//显示week//
timedelay(5);
}
write_1302(0x8a,week);
}
if(k==5)
{
date++;
if(date==31) date=1;
write_com(0x80+0x09);
write_(date);
write_1302(0x86,date);
}
if(k==6)
{
mon++;
if(mon==13) mon=1;
write_com(0x80+0x06);
write_(mon);
write_1302(0x88,mon);
}
if(k==7)
{
year++;
write_com(0x80+0x03);
write_(year);
write_1302(0x8c,year);
}
}}
}
if(k!=0)
{if(key3==0)
{
timedelay(10);
if(key3==0)
{
while(!key3);
write_1302(0x8e,0x00); //WP=0 写操作
if(k==1)
{
sec--;
if(sec==0) sec=59;
write_com(0x80+0x49);
write_(sec);
write_1302(0x80,sec);
}
if(k==2)
{
min--;
if(min==0) min=59;
write_com(0x80+0x46);
write_(min);
write_1302(0x82,min);
}
if(k==3)
{
hou--;
if(hou==0) hou=23;
write_com(0x80+0x43);
write_(hou);
write_1302(0x84,hou);
}
if(k==4)
{
week--;
if(week==0) week=6;
write_com(0x80+0x0c);
for(i=0;i<3;i++)
{
write_byte1(table1[week]);//显示week//
timedelay(5);
}
write_1302(0x8a,week);
}
if(k==5)
{
date--;
if(date<1) date=30;
write_com(0x80+0x09);
write_(date);
write_1302(0x86,date);
}
if(k==6)
{
mon--;
if(mon<1) mon=12;
write_com(0x80+0x06);
write_(mon);
write_1302(0x88,mon);
}
if(k==7)
{
year--;
write_com(0x80+0x03);
write_(year);
write_1302(0x8c,year);
}
}
}
}
}
//////////////////////////////////////
void main()
{
init();
temp=0x80;
b=0;
k=0;
P3=0xff;
yejing();
yejing2();
while(1)
{
temp=0x81;//读的初始地址
for(i=0;i<7;i++)//分别把秒分时数据读出分3次读好一次地址加2" temp+=2;"
{ write_1302(0x8f,0x00);
clk_time=read_1302(temp);
write_1302(0x8e,0x00); //WP=0 写操作
if(i!=5)
{
switch(i)
{case 0:
write_com(0x80+0x49);
write_(clk_time);
break;
case 1:
write_com(0x80+0x46);
write_(clk_time);
break;
case 3:
write_com(0x80+0x09);
write_(clk_time);
break;
case 2:
write_com(0x80+0x43);
write_(clk_time);
break;
case 4:
write_com(0x80+0x06);
write_(clk_time);
break;
case 6:
write_com(0x80+0x03);
write_(clk_time);
break;
}
}
if(i==5)
{
write_com(0x80+0x0c);
for(j=0;j<3;j++)
{
write_byte1(table1[week][j]);//显示week//
timedelay(5);
}
}
temp+=2;
}
keyscan();
}
}
贴程序啊
贴程序啊上图啊
奉上程序,图就不上啦,麻烦!呵呵
增加一个数据转换宏定义:#define TRANSFORM_TO_HEX(val) ((val/10*16)+(val%10)),把需要写的数据转换后写进去,读取的时候相反,对十六进制转换,转成十进制 ((val/16*10)+(val%16))
在DS1302中存储的数据是BCD码,所以读出的数据要处理,看手册
还不够呀,,,