• PUDN用户
    了解作者
  • Windows_Unix
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 99
    下载次数
  • 2005-09-25 04:17
    上传日期
时钟芯片x1226的读写程序.c编程,广泛用于与单片机组成的时钟控制
x1226读写程序.rar
  • x1226读写程序.txt
    5KB
  • www.pudn.com.txt
    218B
内容介绍
#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(); } }
评论
    相关推荐
    • X1226.rar
      2407对X1226的控制,已经调通,可直接用,带注释
    • x1226.zip
      x1226时钟的读写程序,用c51编写。
    • X1226V8I.rar
      X1226的说明手册,好像别的地方 也有, 大家方便
    • x1226.rar
      用msp430f149对x1226进行读写
    • X1226.rar
      x1226的技术资料
    • X1226.rar
      X1226模拟I2C程序,此程序为51环境下,用在其它芯片上需要修正时序
    • X1226_Interface.rar
      实时时钟X1226及其与单片机89C51的接口设计
    • Test_SM32x_H1226
      支持KingMax 联想 超棒 4G 8G Install SMI Factory Driver Guide 1 Install SMI Factory Driver: 1.1 Running the「SMI Factory Driver Update」Program as bottom figure. You can choose the SMI Factory Driver ...
    • 单片机控制实时时钟x1226的设计
      在这里,整理发布了单片机控制实时时钟x1226的设计,只为方便大家用于学习、参考,喜欢单片机...该文档为单片机控制实时时钟x1226的设计,是一份很不错的参考资料,具有较高参考价值,感兴趣的可以下载看看
    • C8051F04x.zip
      C8051f04x开发必备,很有帮助的!!!!!!