⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sd2001d.h

📁 SD2001AP时钟驱动头文件
💻 H
字号:
//#include "reg51.h"
//#include "intrins.h"



sbit SD2001_SDA= P0^0;
sbit SD2001_SCL= P0^2;

/*
sbit SCLE = P1^5;
sbit SDAE = P1^4;
*/

unsigned char  SD2001_year  ;
unsigned char  SD2001_month ;
unsigned char  SD2001_day   ;
unsigned char  SD2001_date  ;
unsigned char  SD2001_hour  ;
unsigned char  SD2001_minute;
unsigned char  SD2001_second;

/*--------------------------------------------------------------------------------
  调用方式:void SD2001_I2CStart(void) ﹫2001/07/0 4
  函数说明:私有函数,I2C专用
---------------------------------------------------------------------------------*/

void SomeNOP()
     {
      _nop_(); _nop_(); _nop_();_nop_();
     }

void SD2001_I2CStart(void)
{
  EA=0;
  SD2001_SCL=0;
  SD2001_SDA=1; SomeNOP();
  SD2001_SCL=1; SomeNOP();//INI
  SD2001_SDA=0; SomeNOP(); //START
  SD2001_SCL=0;
}
/*
void I2CStartRAM(void)
{
  EA=0;
  SCLE=0;
  SDAE=1; SomeNOP();
  SCLE=1; SomeNOP();//INI
  SDAE=0; SomeNOP(); //START
  SCLE=0;
} */
/*--------------------------------------------------------------------------------
  调用方式:void SD2001_I2CStop(void) ﹫2001/07/0 4
  函数说明:私有函数,I2C专用
---------------------------------------------------------------------------------*/
void SD2001_I2CStop(void)
{
  SD2001_SDA=0;
  SD2001_SCL=0; SomeNOP(); //INI
  SD2001_SCL=1; SomeNOP();
  SD2001_SDA=1; SomeNOP();//STOP
  SD2001_SCL=0;
  EA=1;
}
/*
void I2CStopRAM(void)
{
  SDAE=0;
  SCLE=0; SomeNOP(); //INI
  SCLE=1; SomeNOP();
  SDAE=1; SomeNOP();//STOP
  SCLE=0;
  EA=1;
}
*/
/*--------------------------------------------------------------------------------
 调用方式:void I2CSend(unsigned char ch) ﹫2001/07/0 5
 函数说明:私有函数,I2C专用
 MSB
---------------------------------------------------------------------------------*/
void SD2001_I2CSendByte(unsigned char ch)
{
  unsigned char i=8;
  while(i--)
       {
         SD2001_SCL=0;_nop_();
         SD2001_SDA=(bit)(ch&0x80); ch<<=1; SomeNOP();
         SD2001_SCL=1; SomeNOP();
       }
  SD2001_SCL=0;
   //  WaitAck();
}
/*--------------------------------------------------------------------------------
 调用方式:void I2CSend(unsigned char ch) ﹫2001/07/0 5
 函数说明:私有函数,I2C专用
 LSB
---------------------------------------------------------------------------------*/
void SD2001_I2CSendByte1(unsigned char ch)
{
  unsigned char i=8;
  while(i--)
       {
         SD2001_SCL=0;_nop_();
         SD2001_SDA=(bit)(ch & 0x01); ch>>=1; SomeNOP();
         SD2001_SCL=1; SomeNOP();
       }
  SD2001_SCL=0;
 //WaitAck();
}


//ACK
bitout()
{
    SD2001_SDA=0;
    SD2001_SCL=0;
    SD2001_SDA=0;
    SomeNOP();
    SD2001_SCL=1;  SomeNOP();
    SD2001_SCL=0;
}

//接受时钟的1位
bit bitin()
{
 bit temp;
 SD2001_SDA=1;
 SD2001_SCL=0;
 SomeNOP();
 temp=SD2001_SDA;
 SomeNOP();
 SD2001_SCL=1;
 SomeNOP();
 SD2001_SCL=0;
 return(temp);
 }

//接受时钟的1个字节
unsigned char rx()
{
  unsigned char i=8;
  unsigned char dat=0;
  while(i--)
  {
   dat>>=1;
   if(bitin())
    dat=dat|0x80;
   }
  return(dat);
 }


 /*-------------SRAM-------------*/
/*
SRAMACK()
{
 SCLE=0; SomeNOP();  //ACK
 SDAE=0; SomeNOP();
 SCLE=1; SomeNOP();
 SCLE=0; SomeNOP();
 }

void SRAMSent(unsigned char ch)     //发送一个字节给RAM
{
  unsigned char i=8;
  while(i--)
       {
         SCLE=0;_nop_();
         SDAE=(bit)(ch&0x80); ch<<=1; SomeNOP();
         SCLE=1; SomeNOP();
         SCLE=0;
       }
 SDAE=1;

 SCLE=0; SomeNOP();  //ACK
 SCLE=1; SomeNOP();  //ACK
 while(SDAE==1){}    //ACK
 SCLE=0;
}


SRAMRX()                    //接收一个字节从RAM
{
 unsigned char i=8,dat=0;
 SCLE=0;SomeNOP();
 SDAE=1;SomeNOP();
 while(i--)
 {
  SCLE=1;SomeNOP();
  dat<<=1;
  dat|=SDAE;
  SCLE=0;
  }
  return(dat);
}*/
 /*==============================*/



//改时钟
void InitSD2001Time(unsigned char year,unsigned char month,unsigned char day,unsigned char hour,unsigned char minute)
{
    SD2001_I2CStart();
    SD2001_I2CSendByte(0x64);                    //从年开始写
    bitout();

    SD2001_I2CSendByte1(year);
    bitout();
    SD2001_I2CSendByte1(month);
    bitout();
    SD2001_I2CSendByte1(day);
    bitout();
    SD2001_I2CSendByte1(SD2001_date);
    bitout();
    SD2001_I2CSendByte1(hour);
    bitout();
    SD2001_I2CSendByte1(minute);
    bitout();
    SD2001_I2CSendByte1(0x00);
    bitout();
    SD2001_I2CStop();
}
//读时间
 readSD2001_Time()
{
 unsigned char time[8]=0,i;
 SD2001_I2CStart();
 SD2001_I2CSendByte(0x65);           //从年开始读
 bitout();
 i=8;
 while(i--)
 {
   time[i]=rx();
   bitout();
 }
 SD2001_I2CStop();
 SD2001_year  =time[7];
 SD2001_month =time[6];
 SD2001_day   =time[5];
 SD2001_date  =time[4];
 SD2001_hour  =time[3]&0x7F;
 SD2001_minute=time[2];
 SD2001_second=time[1];
 }
/*
//时钟初始化
 InitSD2001()
 {
  SD2001_I2CStart();
  SD2001_I2CSendByte(0x62);//写状态寄存器
  bitout();
  SD2001_I2CSendByte1(0xC2);//Power    12/24    INT1AE    INT2AE    INT1ME    INT2ME    INT1FE    INT2FE
                     //  1        1        0         0         0         0          1        0
  SD2001_I2CStop();
  }
*/
/*
 SaveToSRAM(unsigned int Address,unsigned char SRAM_Dat)
{
  unsigned char AddrH,AddrL;
  AddrL=Address;
  AddrH=Address>>8;

  I2CStartRAM();

  SRAMSent(0xA0);      //送关键字

  SRAMSent(AddrH);     //送高地址   已经含ACK
  SRAMSent(AddrL);     //送低地址

  SRAMSent(SRAM_Dat);

  I2CStopRAM();
}


 LoadFromSRAM(unsigned int Address)
{
 unsigned char AddrH,AddrL,Dat;


 AddrL=Address;
 AddrH=Address>>8;

  I2CStartRAM();
  SRAMSent(0xA0);
  SRAMSent(AddrH);
  SRAMSent(AddrL);

  I2CStartRAM();
  SRAMSent(0xA1);

  Dat=SRAMRX();
  I2CStopRAM();
  if(Dat==255)
     Dat=0;
  return(Dat);
 }
*/
////////////////////////////////////////
/*
main()
{
     InitSD2001();
  SaveToSRAM(5,20,1,3,5,7,9);
  LoadFromSRAM(20);


    while(1)
     {

       ReadSD2001_Time();

       _nop_();
     }

}

*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -