打印

请各位大虾指点一下小弟的DS1302程序

[复制链接]
1871|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
gxst123|  楼主 | 2010-3-15 18:13 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
TE, RS, AC, ST, WRITE
本程序小弟已经看了很久,没有发现什么错误(学识浅薄,不要见怪)
请各位大虾们指点指点,小弟先行谢过~~
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit T_SCLK=P3^6;       //ds1302芯片相应位声明
sbit T_IO=P3^7;
sbit T_RST=P3^5;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
uchar second,hour;
uchar code table[10]={
0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90
};//共阳极数码管编码,显示时间用
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}
void singlebyte_write(uchar date)
{
uchar i;
ACC=date;           //将要写入的数据放入ACC
T_RST=0;
T_RST=1;            //启动数据传送
for(i=0;i<8;i++)    //循环8次写入数据
{
  T_IO=ACC0;
  T_SCLK=0;
  T_SCLK=1;      //在时钟线的上升沿写入一位数据
  ACC=ACC>>1;     //将高1位数据移至ACC^0
}
}
uchar singlebyte_read(void)               //从DS1302读取1Byte数据
{
  uchar i;
//  T_RST=1;            //启动数据传送
  for(i=8; i>0; i--)  //读出8位数据
   {
    ACC = ACC>>1; //将前一下降沿读出的数据右移1位,从而该次读出的数放入ACC^7
ACC7 = T_IO;
// T_IO=1;          //P1口输入前置1
    T_SCLK = 1;     //时钟线下降沿读出1位数据   
    T_SCLK = 0;
   }   
  return(ACC);
}
//往DS1302写入数据
void write_ds1302(uchar add,uchar date)         //往DS1302写入数据
{
T_RST = 0;
T_SCLK = 0;
T_RST = 1;
singlebyte_write(add); // 地址,命令
singlebyte_write(date); //写1Byte数据
T_SCLK = 1;
T_RST =0;
}
//从DS1302读取数据
uchar read_ds1302(uchar add)
{
uchar date;
T_RST=0;
T_SCLK=0;
T_RST=1;
singlebyte_write(add);
date=singlebyte_read();
T_SCLK=1;
T_RST=0;
return(date);
}
void main()
{
uchar date,a,b,x,k;
write_ds1302(0x8e,0x00);
write_ds1302(0x80,0x05);
write_ds1302(0x8e,0x80);
// init1302();
while(1)
{
  date=read_ds1302(0x81);
  x=date;
  a=x%16;
  b=x/16;
  for(k=0;k<25;k++)
  {
    P1=0x08;
    P2=table[a];
    delay(5);
    P2=0xff;

    P1=0x04;
    P2=table[b];
    delay(5);
    P2=0xff;
  }  
}
}

仿真电路图.jpg (260.82 KB )

仿真电路图.jpg

相关帖子

沙发
gxst123|  楼主 | 2010-3-15 18:18 | 只看该作者
自己先顶起~~:)

使用特权

评论回复
板凳
j-ason| | 2010-3-15 22:00 | 只看该作者
楼主,你这个能在protues仿真出来吗?本人也写过大概这样的。但还不能仿真出来。下载到IC里也没有达到想要的效果。

使用特权

评论回复
地板
j-ason| | 2010-3-15 22:03 | 只看该作者
#include<reg52.h>
#include <INTRINS.H>
#define        unchar        unsigned char
#define        unint        unsigned int
sbit RST=P3^5;
sbit IO=P3^3;
sbit CLK=P3^4;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;

unchar xdata time[]="10:03:30";


void reset() //reset DS1302 system
{
RST=0;
CLK=0;
RST=1;
}

void sendbyte(unchar cDATA) //send one byte to DS1302
{
unchar i;
ACC=cDATA;
for(i=0;i<8;i++)
{
IO=ACC0;
CLK=0;
_nop_();
CLK=1;
_nop_();
CLK=0;
ACC=ACC>>1;
}
}

unchar read_c_byte() //read one byte from DS1302
{
unchar i;
for(i=8;i>0;i--)
{
ACC=ACC>>1;
ACC7=IO;
CLK=1;
_nop_();
CLK=0;
}
return (ACC);
}

void write_byte(unchar caddr,unchar cDATA)
{
reset();
sendbyte(0x8E);
sendbyte(0);
reset();
sendbyte(caddr);
sendbyte(cDATA);
sendbyte(0x80);
CLK=1;
RST=0;
CLK=0;
}

void set_time()
{
reset();
write_byte(0xBE,0x00);
write_byte(0x90,0xA5);
write_byte(0x80,0x10);         //second
write_byte(0x82,0x58);         //minute
write_byte(0x84,0x10);         //hour
write_byte(0x86,0x14);         //day
write_byte(0x88,0x03);         //month
write_byte(0x8A,0x05);         //week
write_byte(0x8C,0x10);          //year
write_byte(0x8E,0x80);
}

unchar read_byte(unchar caddr)
{
unchar cDATA;
reset();
sendbyte(caddr|0x01);
cDATA=read_c_byte();
CLK=1;
RST=0;
return (cDATA);
}

void Rtime()
{
unchar d,b;
d=read_byte(0x80);
b=d/16+48;
writechar(13, 0, b,0);
b=d%16+'0';
writechar(12, 0, b,0);
b=':';
writechar(11, 0, b,0);
d=read_byte(0x82);
b=d/16+'0';
writechar(10, 0, b,0);
b=d%16+'0';
writechar(9, 0, b,0);
b=':';
writechar(8, 0, b,0);
d=read_byte(0x84);
b=d/16+'0';
writechar(7, 0, b,0);
b=d%16+'0';
writechar(6, 0, b,0);
}

使用特权

评论回复
5
gxst123|  楼主 | 2010-3-15 22:39 | 只看该作者
我也是想先仿真一下~~可惜没能成功啊~~
最后 你 有什么办法解决了吗?你是怎样显示的~~
请多多指教啊~~谢谢

使用特权

评论回复
6
j-ason| | 2010-3-15 23:01 | 只看该作者
我的用1602 lcd显示的,也没有成功。看看这里有没有高手路过指教。

使用特权

评论回复
7
mengyujun888| | 2010-3-15 23:50 | 只看该作者
:$

使用特权

评论回复
8
new1988| | 2010-3-16 18:54 | 只看该作者
DS1302里面存储的数据是以BCD码形式存储的,要做进制的转换,关于DS1302的 问题,这个论坛上有很多帖子,去找找就知道原因了

使用特权

评论回复
9
j-ason| | 2010-3-16 20:11 | 只看该作者
8楼说的,我都知道,以下的就是数制转换部分了。但下载到IC中还是不行呀。
d=read_byte(0x80);
b=d/16+48;
writechar(13, 0, b,0);
b=d%16+'0';
writechar(12, 0, b,0);
b=':';
writechar(11, 0, b,0);
d=read_byte(0x82);
b=d/16+'0';
writechar(10, 0, b,0);
b=d%16+'0';
writechar(9, 0, b,0);
b=':';
writechar(8, 0, b,0);
d=read_byte(0x84);
b=d/16+'0';
writechar(7, 0, b,0);
b=d%16+'0';
writechar(6, 0, b,0);

使用特权

评论回复
10
维修超人| | 2010-5-23 10:49 | 只看该作者
在Proteus上仿真ds1302是可以,包括数字温度传感IC DS18B20都可以。效果不错。
截了几幅图,以供参考:
                 
只要Ds1302驱动程序正确,Proteus仿真是可以的。实物笔者也做过,效果与仿真相差不大。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

21

帖子

1

粉丝