?? x1226讀寫程序.txt
字號:
#include <W77C32.H>
#include <INTRINS.H>
#include "global.h"
#define SDA P27 file://數(shù)據(jù)
#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://送一字節(jié)數(shù)據(jù)到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字節(jié)數(shù)據(jù)
{
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字節(jié)數(shù)據(jù)
unsigned char bytedata;
I2cStart();
I2cSendByte(device); //送一字節(jié)數(shù)據(jù)到slave
I2cSendByte(0);
I2cSendByte(address);
I2cStart();
I2cSendByte(device|0x01);
bytedata = I2cReceiveByte(); //接收1字節(jié)數(shù)據(jù)
SendAck(1);
I2cStop();
return bytedata;
}
void I2cByteWrite(unsigned char device,unsigned int address,unsigned char bytedata)
{ file://將數(shù)據(jù)寫入指定slave的地址內(nèi)
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://正常,跳出循環(huán)
}
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);//將數(shù)據(jù)寫入指定slave的地址內(nèi)
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://調(diào)節(jié)負載電容值
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://時鐘調(diào)試程序
{
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 + -