找回密码
 立即注册

QQ登录

只需一步,快速开始

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

ds1307驱动文件

[复制链接]
跳转到指定楼层
楼主
ID:155811 发表于 2021-11-3 12:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
ds1307 驱动头文件,包含 iic 文件和 使用方便的51文件,需要的拿去

defType.h

#include "intrins.h"

//#define MAIN_Fosc        22118400L        //定义主时钟
#define MAIN_Fosc   6000000L        //定义主时钟
//#define MAIN_Fosc   11059200L        //定义主时钟
#define MAIN_Fosc_KHz    6000  //晶振频率,khz
#define MAIN_Fosc_MHz    6  //晶振频率,

/**********************************/
typedef unsigned char        u8;
typedef unsigned int        u16;
typedef unsigned long        u32;

typedef unsigned char        uchar;
typedef unsigned int        uint;
typedef unsigned long        ulong;

typedef  char bool ;
#define        TRUE        1
#define        FALSE        0

#define        ENABLE                1
#define        DISABLE                0
//==================
#define NO_MATTER   0
#define        LOW                0       
#define        HIGH        1       


void delay(uchar n)
{
   uchar i=n;
   while(i--);
}


// 防抖按键函数
//p        是一个按键位,如p1_1
//exec 是执行语句
#define KeyPress(p,exec) if(p== 0)   \
   { \
     delay(200);  \
     if(p== 0) \
      exec;\
          while(p== 0); \
   }
//=====================


/***********************************/
#define NOP  _nop_()
#define NOP2 _nop_();_nop_()
#define NOP3 _nop_();_nop_();_nop_()
#define NOP4 _nop_();_nop_();_nop_();_nop_()
#define NOP6 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
#define NOP8 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
#define NOP10 _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
#define NOP12 NOP6;NOP6
#define NOP14 NOP8;NOP6
#define NOP16 NOP8;NOP8
#define NOP18 NOP8;NOP10
#define NOP20 NOP10;NOP10
#define NOP22 NOP10;NOP10;NOP2
#define NOP24 NOP10;NOP10;NOP4
#define NOP26 NOP10;NOP10;NOP6
#define NOP28 NOP10;NOP10;NOP8
#define NOP30 NOP10;NOP10;NOP10
#define NOP32 NOP10;NOP10;NOP10;NOP2
#define NOP34 NOP10;NOP10;NOP10;NOP4
#define NOP36 NOP10;NOP10;NOP10;NOP6
#define NOP38 NOP10;NOP10;NOP10;NOP8
#define NOP40 NOP10;NOP10;NOP10;NOP10
#define NOP42 NOP10;NOP10;NOP10;NOP10;NOP2
#define NOP44 NOP10;NOP10;NOP10;NOP10;NOP4
#define NOP46 NOP10;NOP10;NOP10;NOP10;NOP6
#define NOP48 NOP10;NOP10;NOP10;NOP10;NOP8
#define NOP50 NOP10;NOP10;NOP10;NOP10;NOP10
#define NOP52 NOP10;NOP10;NOP10;NOP10;NOP10;NOP2
#define NOP54 NOP10;NOP10;NOP10;NOP10;NOP10;NOP4
#define NOP56 NOP10;NOP10;NOP10;NOP10;NOP10;NOP6
#define NOP58 NOP10;NOP10;NOP10;NOP10;NOP10;NOP8
#define NOP60 NOP10;NOP10;NOP10;NOP10;NOP10;NOP10

iic.h

//define mcu connect for iic bus
sbit SCL=P0^0;         //clock line
sbit SDA=P0^1;         //data line

#define DELAY_T 2

//the data HIGH to LOW, while the clock is HIGH,
//defines a START condition.
void iic_Start()
{
  SDA=1;
  SCL=1;
  NOP2;

  SDA=0;
  NOP2;

  SCL=0;
  NOP2;
}


//the data line LOW to HIGH, while the clock line is HIGH,
//defines the STOP condition
void iic_Stop()
{
  SDA=0;
  SCL=1;
  NOP2;

  SDA=1;
  NOP2;
  
  SCL=0;
  NOP2;
}


//发送0,在SCL为高电平时使SDA信号为低
void iic_Send0()
{
  SDA=0;
  SCL=1;
  NOP2;

  SCL=0;
  NOP2;
}


//发送1,在SCL为高电平时使SDA信号为高
void iic_Send1()
{
  SDA=1;
  SCL=1;
  NOP2;
  
  SCL=0;
  NOP2;
}


//发送完一个字节后检验设备的应答信号
char iic_CheckAck(void)
{
  SDA=1;
  SCL=1;
  NOP;

  F0=SDA;
  NOP;

  SCL=0;
  NOP2;

  return F0;
}


//向IIC总线写一个字节,返回应答
char iic_WriteByte(char n)
{
   char i;
   for(i=0;i<8;i++)
   {
      if((n<<i)&0x80)
            iic_Send1();
          else
            iic_Send0();
        }
        return iic_CheckAck();
}


//向I2C总线写n个字节,返回写了多少字节
char iic_WriteBytes(char*buffer,char n)
{
  char i;
  for(i=0;i<n;i++)
  {
        iic_WriteByte(buffer[i]);
        if(!iic_CheckAck())
        {
          iic_Stop();
          return(i);
        }
   }
   return n;
}

//从I2C总线读一个字节
char iic_ReadByte(void)
{
  char b=0,i;
  for(i=0;i<8;i++)
  {
    SDA=1;//释放总线
        SCL=1;//接受数据
        NOP6;
       
        F0=SDA;
        NOP8;
        SCL=0;
        if(F0==1)
        {
          b=b<<1;
          b=b|0x01;
        }
        else
          b=b<<1;
       

   return b;
  }
}

//从I2C总线读n个字节
char iic_ReadBytes(char SlaveAdr,char n,char*buffer)
{
  char i;
  iic_WriteByte(SlaveAdr);//向总线发送接收器地址
  if(!iic_CheckAck())//等待接收器应答信号
    return 0;
  for(i=0;i<n;i++)
  {
        buffer[i]=iic_ReadByte();
        if(i!=n)
          iic_Send0();//发送应答
        else
          iic_Send1();//发送非应答
  }
  return 1;
}

ds1307.h

#include "defType.h"
#include "iic.h"


#define HZ_1     0
#define HZ_4096  1
#define HZ_8192  2
#define HZ_32768 3

#define HOUR24 0
#define HOUR12 1

#define AM     0
#define PM     1

typedef struct
{ char sec;
  char minute;
  char hour;
} TIME;

typedef struct
{ char week;
  char day;
  char month;
  char year;
} DATE;

//ASCII 码 (十 进 制 )转BCD 码
uchar Dec2BCD (uchar dec)
{
   uchar x, y;
   x=dec/10;
   y=dec%10;
   y=(x<<4)|y;
   return y;
}

//BCD 码 转 ASCII 码(十进制)
uchar BCD2Dec(uchar bcd)
{
  uchar x, y;
  y=bcd/16;
  x=bcd % 16;
  y=y*10+x;
  return y;
}

void Set1307Out(bool e, char hz)
{
   iic_Start();
   if(iic_WriteByte(0xd0) ) //        指明要写数据到1307
   {
      if(iic_WriteByte(0x07) )        //指明要写人的地址
          {
             uchar b= hz;
                 if(e) b |=0x10;
             iic_WriteByte(b);
          }
   }
   iic_Stop();
}

//if set to 12 hours must set AM or PM,els no matter
void Set1307Time(TIME t,bool bHour12, bool bPM)
{
   iic_Start();
   if(iic_WriteByte(0xd0) ) //        指明要写数据到1307
   {
      if(iic_WriteByte(0) )        //指明要写人的地址
          {
            t.sec=Dec2BCD(t.sec)& 0x7f;        //确保1307 run
                t.minute= Dec2BCD(t.minute);
                t.hour= Dec2BCD(t.hour)& 0x1f;
                if(bHour12)
                  t.hour|= 0x40;
                if(bPM)
                  t.hour|= 0x20;
            iic_WriteBytes( (char*)&t, 3); //写入time
          }
   }
   iic_Stop();
}
   
void Set1307Date(DATE d)
{
   iic_Start();
   if(iic_WriteByte(0xd0) ) //        指明要写数据到1307
   {
      if(iic_WriteByte(0x03) )        //指明要写人的地址
          {
            d.week=Dec2BCD(d.week)& 0x07;
                d.day= Dec2BCD(d.day) & 0x3f;
                d.month= Dec2BCD(d.month)& 0x1f;
                d.year=  Dec2BCD(d.year);
            iic_WriteBytes( (char*)&d, 4); //写入time
          }
   }
   iic_Stop();
}

void Read1307Time(TIME* t)
{
   iic_Start();
   iic_ReadBytes(0, 3,(char*)t);
   t->sec&= 0x7f;
   t->sec= BCD2Dec(t->sec);

   t->minute&= 0x7f;
   t->minute= BCD2Dec(t->minute);

   t->hour&= 0x1f;
   t->hour=  BCD2Dec(t->hour);
   iic_Stop();
}

void Read1307Date(DATE* t)
{
   iic_Start();
   iic_ReadBytes(0x03, 4,(char*)t);
   t->week= BCD2Dec(t->week);
   t->day= BCD2Dec(t->day);
   t->month=   BCD2Dec(t->month);
   t->year = BCD2Dec(t->year);
   iic_Stop();
}

评分

参与人数 1黑币 +10 收起 理由
admin + 10 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:155811 发表于 2021-12-14 17:30 | 只看该作者
定义一个TIME 结构变量,赋值,然后直接 set1307Time(), 设置1307的时间
定义一个DATE 结构变量,赋值,然后直接 set1307Date().设置日期

定义一个TIME 结构变量,然后 read1307Time(),读取时间
定义一个DATE 结构变量,然后 read1307Date().读取日期

简单直接
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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