久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2471|回復: 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 共享資料的黑幣獎勵!

查看全部評分

回復

使用道具 舉報

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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 一区二区三区在线观看视频 | 日韩成人性视频 | 久久久久91 | 免费播放一级片 | 日韩免费看片 | 日韩精品一区二区三区中文字幕 | 欧美区日韩区 | 久久久九九 | 综合五月婷 | 久久精品一区二区三区四区 | 久久精品国产久精国产 | 国产成人免费视频网站高清观看视频 | 久精品久久| 最新日韩在线视频 | 国产一区在线免费 | 九九99精品 | 成人精品一区二区 | 九九九久久国产免费 | 亚洲精品成人免费 | 91九色在线观看 | www.狠狠干| 国产精品1区2区 | 丁香六月伊人 | 国产福利资源 | 久久国产日韩 | 日本网站免费在线观看 | 三级av在线| 欧美a级网站 | 国产免费拔擦拔擦8x高清 | 亚洲一区二区三区在线播放 | 少妇淫片aaaaa毛片叫床爽 | 久久久久久久一区二区三区 | 一区二区三区免费 | 大香网伊人 | 黄色国产 | 狠狠婷婷综合久久久久久妖精 | 人人玩人人干 | 欧美精品二区 | 欧美日韩一区二区三区视频 | 亚洲视频在线一区 | 99成人精品 |