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

📄 x1226读写程序.txt

📁 时钟芯片x1226的读写程序.c编程,广泛用于与单片机组成的时钟控制
💻 TXT
字号:
#include <W77C32.H>
#include <INTRINS.H>
#include "global.h"

 

 

#define SDA P27   file://数据
#define SCL P26      file://时钟


sbit P26 = P2^6;        file://x1226
sbit P27 = P2^7;
sbit P11 = P1^1;

 


void I2cWait(void)    file://延时,I2c大约30k/s
{
  unsigned char i;

  for(i=0;i<10;i++)
  {
    _nop_();
  }
}

void I2cStart(void)    file://起始信号
{
  SDA = 1;
  SCL = 1;
  I2cWait();
  SDA = 0;
  I2cWait();
  SCL = 0;
}

void I2cStop(void)    file://停止信号
{
  SDA = 0;
  I2cWait();
  SCL = 1;
  I2cWait();
  SDA = 1;
  I2cWait();
}

void SendAck(bit ack)    file://送确认信号到slave
{
  SDA = ack;
  SCL = 1;
  I2cWait();
  SCL = 0;
}

bit I2cSendByte(unsigned char bytedata)  file://送一字节数据到slave,并返回确认信号
{
  unsigned char i;
  bit ack;

  for(i=0;i<8;i++)
  {
    if(bytedata & 0x80)
      SDA = 1;
    else
      SDA = 0;

    bytedata <<= 1;
    I2cWait();

    SCL = 1;
    I2cWait();
    SCL = 0;
    I2cWait();
  }

  SDA = 1;
  I2cWait();
  SCL = 1;
  I2cWait();

  ack = SDA;      file://接收确认信号:1,没收到应答;0,收到应答

  SCL = 0;
  I2cWait();

  return ack;
}

unsigned char I2cReceiveByte(void)  file://接收1字节数据
{
  unsigned char i;
  unsigned char bytedata = 0;

  for(i=0;i<8;i++)
  {
    SCL = 1;
    I2cWait();

    bytedata <<= 1;

    if(SDA)
    {
      bytedata |= 0x01;
    }
    SCL = 0;
    I2cWait();
  }

  return bytedata;
}

unsigned char I2cByteRead(unsigned char device,unsigned char address)
{      file://从slave接收1字节数据
  unsigned char bytedata;

  I2cStart();
  I2cSendByte(device); //送一字节数据到slave
  I2cSendByte(0);
  I2cSendByte(address);
  I2cStart();
  I2cSendByte(device|0x01);
  bytedata = I2cReceiveByte(); //接收1字节数据
  SendAck(1);
  I2cStop();

  return bytedata;
}


void I2cByteWrite(unsigned char device,unsigned int address,unsigned char bytedata)
{      file://将数据写入指定slave的地址内
   unsigned char i;
   unsigned char addressh,addressl;
   bit ack;

   addressl = address&0xFF;
   addressh = address>>8;
   for(i=0;i<10;i++)
   {
     I2cStart();
     ack = I2cSendByte(device);
     if(ack==1)
     {
       I2cStop();
       continue;
     }
     ack = I2cSendByte(addressh);
     if(ack==1)
     {
       I2cStop();
       continue;
     }
     ack = I2cSendByte(addressl);
     if(ack==1)
     {
       I2cStop();
       continue;
     }
     ack = I2cSendByte(bytedata);
     if(ack==1)
     {
       I2cStop();
       continue;
     }
     I2cStop();
     if(ack==0)  break;    file://正常,跳出循环
   }
   DelayX5ms(2);
}

 

 

/*****读写X1226*****/

unsigned char year;
unsigned char month;
unsigned char day;
unsigned char hour;
unsigned char minute;
unsigned char second;

void WriteRTC(void)   file://写时钟
{  
  EA = 0;
  I2cByteWrite(0xDE,0x3F,0x02);//将数据写入指定slave的地址内
  I2cByteWrite(0xDE,0x3F,0x06);
  I2cByteWrite(0xDE,0x30,second);
  I2cByteWrite(0xDE,0x31,minute);
  I2cByteWrite(0xDE,0x32,hour + 0x80);
  I2cByteWrite(0xDE,0x33,day);
  I2cByteWrite(0xDE,0x34,month);
  I2cByteWrite(0xDE,0x35,year);
  I2cByteWrite(0xDE,0x37,0x20);
  I2cByteWrite(0xDE,0x3F,0x00);
  EA = 1;
}

/*
void CapAdjust(unsigned char value)
{
  EA = 0;
  I2cByteWrite(0xDE,0x3F,0x02);
  I2cByteWrite(0xDE,0x3F,0x06);
  I2cByteWrite(0xDE,0x12,value);    file://调节负载电容值
  I2cByteWrite(0xDE,0x3F,0x00);
  EA = 1;
}
*/

void ReadRTC(void)   file://读时钟
{
  EA = 0;
  year = I2cByteRead(0xDE,0x35);
  month = I2cByteRead(0xDE,0x34);
  day = I2cByteRead(0xDE,0x33);
  hour = I2cByteRead(0xDE,0x32) - 0x80;
  minute = I2cByteRead(0xDE,0x31);
  second = I2cByteRead(0xDE,0x30);
  EA = 1;
}

 

/*
void ReviTime(void)                         file://效验时间
{
    unsigned char RTC[5],i;
    ByteSend1('T');
    DelayX10ms(10);
    for(i=0;i<6;i++)
    {
        RTC[i]=ReceiveByte1();
    }
    EA = 0;
    I2cByteWrite(0xDE,0x3F,0x06);
    I2cByteWrite(0xDE,0x3F,0x06);
    I2cByteWrite(0xDE,0x30,RTC[0]);
    I2cByteWrite(0xDE,0x31,RTC[1]);
    I2cByteWrite(0xDE,0x32,RTC[2] + 0x80);
    I2cByteWrite(0xDE,0x33,RTC[3]);
    I2cByteWrite(0xDE,0x34,RTC[4]);
    I2cByteWrite(0xDE,0x35,RTC[5]);
    I2cByteWrite(0xDE,0x37,0x20);
    I2cByteWrite(0xDE,0x3F,0x00);
    EA = 1;
}

void judgtime(void)                   file://判断时间
{
    unsigned char hour;
    hour=I2cByteRead(0xDE,0x32) - 0x80;
    if(hour==0)modemdial();             file://拨号
    _nop_();
}
*/
file://04年4月12日16时40分20秒
void RTCtest(void)                    file://时钟调试程序 
{
    year   = 0x04 ;                   file://BCD编码
    month  = 0x04 ;
    day    = 0x12 ;
    hour   = 0x16 ;
    minute = 0x40 ;
    second = 0x20 ;
    WriteRTC();
    DelayX5ms(10);
   
    while(1)
    {        
        ReadRTC();
        ZLG7290_SendCmd(3,second>>4);
        DelayX5ms(50);        
        ZLG7290_SendCmd(2,second&0x0f);
        DelayX5ms(200);
        TestGAL(); 
    } 
}
 

⌨️ 快捷键说明

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