找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1754|回复: 0
收起左侧

ds1302 51单片机实验设计+Proteus仿真

[复制链接]
ID:805551 发表于 2020-7-30 15:06 | 显示全部楼层 |阅读模式
#ifndef _1302_H
#define _1302_H

#include"reg52.h"
#include<intrins.h>
//---重定义关键词---//
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
//---定义 ds1302 使用的 IO 口---//
sbit DSIO=P3^4;
sbit RST=P3^5;
sbit SCLK=P3^6;
//---定义全局函数---//
void Ds1302Write(uchar addr, uchar dat);
uchar Ds1302Read(uchar addr);
void Ds1302Init();
void Ds1302ReadTime();
//---加入全局变量--//
extern uchar TIME[7]; //加入全局变量
#endif



#include<reg52.h>
#include<1302.h>
//---DS1302 写入和读取时分秒的地址命令---//
//---秒分时日月周年 最低位读写位;-------//
uchar code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b,
0x8d};
uchar code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a,
0x8c};
//---DS1302 时钟初始化 2020 年 7 月 29 日星期三 12 点 00 分 00 秒。---//
//---存储顺序是秒分时日月周年,存储格式是用 BCD 码---//
uchar TIME[7] = {0x00, 0x00, 0x12,0x92 ,0x07,0x03, 0x20};
/****************************************************************
***************
* 函 数 名 : Ds1302Write
* 函数功能 : 向 DS1302 命令(地址+数据)
* 输 入 : addr,dat
* 输 出 : 无
*****************************************************************
**************/
void  Ds1302Write(uchar addr,uchar dat)
{
        uint n;
        RST=0;
        _nop_();
        SCLK=0;       
        RST=1;
        _nop_();
        for(n=0;n<8;n++)
        {
                 DSIO=addr&0x01;
                 addr>>=1;
                 SCLK=1;
                 _nop_();
                 SCLK=0;
                 _nop_();
                       
        }
        for(n=0;n<8;n++)
        {
                 DSIO=dat&0x01;
                 dat>>=1;
                 SCLK=1;
                 _nop_();
                 SCLK=0;
                 _nop_();       
        }
        RST=0;
        _nop_();
}
/****************************************************************
***************
* 函 数 名 : Ds1302Read
* 函数功能 : 读取一个地址的数据
* 输 入 : addr
* 输 出 : dat
*****************************************************************
**************/
uchar  Ds1302Read(uchar addr)
{
        uchar dat,dat1,i;
        RST=0;
        _nop_();
        SCLK=0;
        _nop_();
        RST=1;
        _nop_();
        for(i=0;i<8;i++)
        {
                DSIO=addr&0x01;
                addr>>=1;
                SCLK=1;
            _nop_();
                SCLK=0;
                _nop_();
        }
        for(i=0;i<8;i++)
        {
                dat1=DSIO;
                dat=(dat>>1)|(dat1<<7);
                SCLK=1;
                _nop_();
                SCLK=0;
                _nop_();
        }
         RST=0;                //复位稳定程序
         _nop_();
         SCLK = 1;
        _nop_();
        DSIO = 0;
        _nop_();
        DSIO = 1;
        _nop_();
        return dat;

}
/****************************************************************
***************
* 函 数 名 : Ds1302Init
* 函数功能 : 初始化 DS1302.
* 输 入 : 无
* 输 出 : 无
*****************************************************************
**************/
void Ds1302Init()
{         uint i;
         Ds1302Write(0x80,0x00);//关闭写保护功能
         for(i=0;i<7;i++)
         {
                 Ds1302Write(READ_RTC_ADDR[i],TIME[i]);
                       
         }
          Ds1302Write(0x8e,0x80); //打开写保护位;
}
/****************************************************************
***************
* 函 数 名 : Ds1302ReadTime
* 函数功能 : 读取时钟信息
* 输 入 : 无
* 输 出 : 无
*****************************************************************
**************/
void  Ds1302ReadTime()
{
        uchar n;
        for (n=0; n<7; n++)//读取 7 个字节的时钟信号:分秒时日月周年
        {
                TIME[n] = Ds1302Read(READ_RTC_ADDR[n]);
        }

}






#include<reg52,h>
#include<1302.h>
typedef unsigned int u16; //对数据类型进行声明定义
typedef unsigned char u8;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
char num=0;
u8 DisplayData[8];
u8 code
smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/****************************************************************
***************
* 函 数 名 : delay
* 函数功能 : 延时函数,i=1 时,大约延时 10us
*****************************************************************
**************/
void delay(u16 i)
{
while(i--);
}
/****************************************************************
***************
* 函 数 名 : datapros()
* 函数功能 : 时间读取处理转换函数
* 输 入 : 无
* 输 出 : 无
***/
void datapros()
{
        Ds1302ReadTime();
        DisplayData[0]=smgduan[TIME[2]/16];
        DisplayData[1]=smgduan[TIME[2]&0x0f];
        DisplayData[2] = 0x40;
        DisplayData[3]=smgduan[TIME[1]/16];
        DisplayData[4]=smgduan[TIME[1]&0x0f];
        DisplayData[5]=0x40;
        DisplayData[6]=smgduan[TIME[0]/16];
        DisplayData[7]=smgduan[TIME[0]&0x0f];
}
/****************************************************************
***************
* 函数名 :DigDisplay()
* 函数功能 :数码管显示函数
* 输入 : 无
* 输出 : 无
*****************************************************************
**************/
void DigDisplay()
{
        u8 i;
for(i=0;i<8;i++)
{
switch(i) //位选,选择点亮的数码管,
{
case(0):
LSA=1;LSB=1;LSC=1; break;//显示第 0 位
case(1):
LSA=0;LSB=1;LSC=1; break;//显示第 1 位
case(2):
LSA=1;LSB=0;LSC=1; break;//显示第 2 位
case(3):
LSA=0;LSB=0;LSC=1; break;//显示第 3 位
case(4):
LSA=1;LSB=1;LSC=0; break;//显示第 4 位
case(5):
LSA=0;LSB=1;LSC=0; break;//显示第 5 位
case(6):
LSA=1;LSB=0;LSC=0; break;//显示第 6 位
case(7):
LSA=0;LSB=0;LSC=0; break;//显示第 7 位
}
P0=DisplayData[i];//发送数据
delay(100); //间隔一段时间扫描
P0=0x00;//消隐
}

}
void main()
{
//Ds1302Init();
while(1)
{
datapros(); //数据处理函数
DigDisplay();//数码管显示函数
}
}




proteus仿真

proteus仿真

ds1302时钟通信proteus仿真.zip

45.36 KB, 下载次数: 10, 下载积分: 黑币 -5

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表