DS1302驱动

// DS1302.c

#include <reg52.h>

#include <intrins.h>
#include "DS1302.h"
#define NOP(); _nop_();_nop_();
sbit DS1302_Sclk=P2^0;
sbit DS1302_IO=P2^1;
sbit DS1302_CE=P2^2;
/**********************************************************
 写一个字节
***********************************************************/
static void Ds1302_WriteByte(uchar temp)
{
uchar i;
for(i=0;i<8;i++)
{
_nop_();
DS1302_Sclk=0;
DS1302_IO=temp&0x01;
temp>>=1;
DS1302_Sclk=1;
_nop_();
}
}
/****************************************************************
指定地址写数据
*****************************************************************/
void Write_Ds1302(uchar address,uchar dat)
{
DS1302_CE=0;
NOP();
DS1302_Sclk=0;
NOP();
DS1302_CE=1;
NOP();
Ds1302_WriteByte(address);
Ds1302_WriteByte(dat);
NOP();
DS1302_CE=0;
}
/************************************************************
指定地址读数据
*************************************************************/
uchar Read_Ds1302(uchar address)
{
uchar i,temp=0x00;
DS1302_CE=0;
NOP();
DS1302_Sclk=0;
NOP();
DS1302_CE=1;
NOP();
Ds1302_WriteByte(address);
for(i=0;i<8;i++)
{
if(DS1302_IO)
temp|=0x80;
DS1302_Sclk=0;
temp>>=1;
NOP();
DS1302_Sclk=1;
NOP();
}
  NOP();
    DS1302_Sclk=0;
  NOP();
  DS1302_CE=0;
return temp/16*10+temp%16;
}
/************************************************************
设置DS1302
*************************************************************/
void Set_Ds1302(uchar address ,uchar dat)
{
uchar temp;

        temp=dat/10;
dat=dat%10;
temp=dat+temp*16;
Write_Ds1302(0x8E,0X00);
Write_Ds1302(address,temp);
Write_Ds1302(0x8E,0X080);
}
/****************************************************************/
void Set_Ds1302_ram(uchar address ,uchar dat)
{
uchar temp;
    temp=dat/10;
dat=dat%10;
temp=dat+temp*16;
Write_Ds1302(0x8E,0X00);
Write_Ds1302(address,temp);
Write_Ds1302(0x8E,0X080);

}  


//DS1302.h

#ifndef _DS1302_H

#define _DS1302_H

#define uchar unsigned char
#define uint unsigned int

void Write_Ds1302(unsigned char address,unsigned char dat);  
unsigned char Read_Ds1302(unsigned char address);  
void Set_Ds1302(unsigned char address ,unsigned char dat); 
void Set_Ds1302_ram(unsigned char address ,unsigned char dat);
#endif


猜你喜欢

转载自blog.csdn.net/mygod2008ok/article/details/81039858