找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1564|回复: 0
打印 上一主题 下一主题
收起左侧

蓝桥杯 DS1302驱动程序

[复制链接]
跳转到指定楼层
楼主
ID:230970 发表于 2018-4-13 14:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/*
  程序说明: DS1302驱动程序
  软件环境: Keil uVision 4.10
  硬件环境: CT107单片机综合实训平台 8051,12MHz
  日    期: 2011-8-9
*/

#include <reg52.h>
#include <intrins.h>

sbit SCK=P1^7;               
sbit SDA=P2^3;               
sbit RST = P1^3;   // DS1302复位                                                                                                

void Write_Ds1302_Byte(unsigned  char temp)
{
        unsigned char i;
        for (i=0;i<8;i++)            
        {
                SCK=0;
                SDA=temp&0x01;
                temp>>=1;
                SCK=1;
        }
}   

void Write_Ds1302( unsigned char address,unsigned char dat )     
{
         RST=0;
        _nop_();
         SCK=0;
        _nop_();
         RST=1;        
           _nop_();  
         Write_Ds1302_Byte(address);        
         Write_Ds1302_Byte(dat);               
         RST=0;
}

unsigned char Read_Ds1302 ( unsigned char address )
{
         unsigned char i,temp=0x00;
         RST=0;
        _nop_();
         SCK=0;
        _nop_();
         RST=1;
        _nop_();
         Write_Ds1302_Byte(address);
         for (i=0;i<8;i++)         
         {               
                SCK=0;
                temp>>=1;        
                 if(SDA)
                 temp|=0x80;        
                 SCK=1;
        }
         RST=0;
        _nop_();
         RST=0;
        SCK=0;
        _nop_();
        SCK=1;
        _nop_();
        SDA=0;
        _nop_();
        SDA=1;
        _nop_();
        return (temp);                        
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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