?? 8563.c.txt
字號:
/**————————————————————
〖說明〗I2C總線驅(qū)動程序(用兩個普通IO模擬I2C總線)
包括100Khz(T=10us)的標(biāo)準(zhǔn)模式(慢速模式)選擇,
和400Khz(T=2.5us)的快速模式選擇,
默認(rèn)11.0592Mhz的晶振。
—————————————————————*/
#ifndef SDA
#define SDA P0_0
#define SCL P0_1
#endif
extern uchar SystemError;
#define uchar unsigned char
#define uint unsigned int
#define Byte unsigned char
#define Word unsigned int
#define bool bit
#define true 1
#define false 0
#define SomeNOP(); _nop_();_nop_();_nop_();_nop_();
/**--------------------------------------------------------------------------------
調(diào)用方式:void I2CStart(void) 函數(shù)說明:私有函數(shù),I2C專用
---------------------------------------------------------------------*/
void I2CStart(void)
{
EA=0;
SDA=1; SCL=1; SomeNOP();//INI
SDA=0; SomeNOP(); //START
SCL=0;
}
/**--------------------------------------------------------------------------------
調(diào)用方式:void I2CStop(void) ﹫2001/07/0 4
函數(shù)說明:私有函數(shù),I2C專用
---------------------------------------------------------------------------------*/
void I2CStop(void)
{
SCL=0; SDA=0; SomeNOP(); //INI
SCL=1; SomeNOP(); SDA=1; //STOP
EA=1;
}
/**--------------------------------------------------------------------------------
調(diào)用方式:bit I2CAck(void) ﹫2001/07/0 4
函數(shù)說明:私有函數(shù),I2C專用,等待從器件接收方的應(yīng)答
---------------------------------------------------------------------------------*/
bool WaitAck(void)
{
uchar errtime=255;//因故障接收方無ACK,超時值為255。
SDA=1;SomeNOP();
SCL=1;SomeNOP();
while(SDA) {errtime--;
if (!errtime) {I2CStop();SystemError=0x11;
return false;}}
SCL=0;
return true;
}
/**--------------------------------------------------------------------------------
調(diào)用方式:void SendAck(void) ﹫2001/07/0 4
函數(shù)說明:私有函數(shù),I2C專用,主器件為接收方,從器件為發(fā)送方時,應(yīng)答信號。
---------------------------------------------------------------------------------*/
void SendAck(void)
{
SDA=0; SomeNOP();
SCL=1; SomeNOP();
SCL=0;
}
/**--------------------------------------------------------------------------------
調(diào)用方式:void SendAck(void) ﹫2001/07/0 4
函數(shù)說明:私有函數(shù),I2C專用,主器件為接收方,從器件為發(fā)送方時,非應(yīng)答信號。
}**--------------------------------------------------------------------------------
void SendNotAck(void)
{
SDA=1; SomeNOP();
SCL=1; SomeNOP();
SCL=0;
}
/**--------------------------------------------------------------------------------
調(diào)用方式:void I2CSend(uchar ch) ﹫2001/07/0 5
函數(shù)說明:私有函數(shù),I2C專用
---------------------------------------------------------------------------------*/
void I2CSendByte(Byte ch)
{
uchar i=8;
while (i--)
{
SCL=0;_nop_();
SDA=(bit)(ch&0x80); ch<<=1; SomeNOP();
SCL=1; SomeNOP();
}
SCL=0;
}
/**----------------------------------------------------------------
調(diào)用方式:uchar I2CReceive(void) ﹫2001/07/0 5
函數(shù)說明:私有函數(shù),I2C專用
---------------------------------------------------------------------------------*/
Byte I2CReceiveByte(void)
{
uchar i=8;
Byte ddata=0;
SDA=1;
while (i--)
{
ddata<<=1;
SCL=0;SomeNOP();
SCL=1;SomeNOP();
ddata|=SDA;
}
SCL=0;
return ddata;
}
//--------------------------------------------------------------------- //開始PCF8563T驅(qū)動程序
/**--------------------------------------------------------------------------------
調(diào)用方式:void GetPCF8563(uchar firsttype,uchar count,uchar *buff) ﹫2001/08/0 7
函數(shù)說明:讀取時鐘芯片PCF8563的時間,設(shè)置要讀的第一個時間類型firsttype,并設(shè)置讀取的字節(jié)數(shù),則會一次把時間讀取到buff中。順序是:
0x02:秒/0x03:分/0x04:小時/0x05:日/0x06:星期/0x07:月(世紀(jì))/0x08:年
---------------------------------------------------------------------------------*/
void GetPCF8563(uchar firsttype,uchar count,uchar *buff)
{
uchar i;
I2CStart();
I2CSendByte(0xA2);
WaitAck();
I2CSendByte(firsttype);
WaitAck();
I2CStart();
I2CSendByte(0xA3);
WaitAck();
for (i=0;i<count;i++)
{
buff[i]=I2CReceiveByte();
if (i!=count-1) SendAck();//除最后一個字節(jié)外,其他都要從MASTER發(fā)應(yīng)答。
}
SendNotAck();
I2CStop();
}
/**--------------------------------------------------------------------------------
調(diào)用方式:void SetPCF8563(uchar timetype,uchar value) ﹫2001/08/0 7
函數(shù)說明:調(diào)整時鐘。timetype是要改的時間類型,value是新設(shè)置的時間值(BCD格式)。
0x02:秒/0x03:分/0x04:小時/0x05:日/0x06:星期/0x07:月(世紀(jì))/0x08:年
---------------------------------------------------------------------------------*/
void SetPCF8563(uchar timetype,uchar value)
{
I2CStart();
I2CSendByte(0xA2);
WaitAck();
I2CSendByte(timetype);
WaitAck();
I2CSendByte(value);
WaitAck();
I2CStop();
}
/**--------------------------------------------------------------------------------
調(diào)用方式:void SetAlarmHour(uchar count) ﹫2001/08/0 7
函數(shù)說明:設(shè)置報警鬧鐘在一天的第count點報警。例如:count=23,則在晚上11點報警。
---------------------------------------------------------------------------------*/
void SetAlarm(uchar alarmtype,uchar count)
{
SetPCF8563(0x01,0x02);
SetPCF8563(alarmtype,count);
}
/**--------------------------------------------------------------------------------
調(diào)用方式:void CleanAlarm(void) ﹫2001/08/0 7
函數(shù)說明:清除所有報警設(shè)置。
---------------------------------------------------------------------------------*/
void CleanAlarm(void)
{
SetPCF8563(0x01,0x00);
SetPCF8563(0x09,0x80);
SetPCF8563(0x0A,0x80);
SetPCF8563(0x0B,0x80);
SetPCF8563(0x0C,0x80);
// SetPCF8563(0x0D,0x00);
// SetPCF8563(0x0E,0x03);
}
/*--------------------------------------------------------------------------------
調(diào)用方式:uchar read1380(uchar command )
函數(shù)說明:read1380()返回當(dāng)前時間, command指要返回的時間類型。
秒:81H 分鐘:83H 小時:85H 日期:87H 星期:89H 星期幾:8BH 年:8D H
---------------------------------------------------------------------------------*/
uchar read1380 (uchar command)
{
uchar time;
GetPCF8563(command,1,&time);
return time;
}
/*--------------------------------------------------------------------------------
調(diào)用方式:void write1380(uchar command ,uchar time )
函數(shù)說明:write1380()往HT1380寫命令和數(shù)據(jù),command是命令字, time是后寫入的數(shù)據(jù)
---------------------------------------------------------------------------------*/
void write1380(uchar command ,uchar time)
{
SetPCF8563(command,time);
}
/*--------------------------------------------------------------------------------
調(diào)用方式:void time_display(uchar x0,uchar y0 )
函數(shù)說明:time_display()在指定的x0,y0坐標(biāo),以00:00:00格式顯示當(dāng)前時間。
---------------------------------------------------------------------------------*/
//uchar time[]="00:11:11";
void time_display(uchar x0,uchar y0,bit type) //液晶時間顯示
{
uchar time[]="00:00:00";
uchar con[3];
uchar time_type;
GetPCF8563(0x02,3,con);
time[0]=(con[2]>>4)+'0';
time[1]=(con[2]&0x0f)+'0';
time[3]=(con[1]>>4)+'0';
time[4]=(con[1]&0x0f)+'0';
time[6]=(con[0]>>4)+'0';
time[7]=(con[0]&0x0f)+'0';
time[8]=0;
if(type==1)
{
time_type=0xff;
}
else
{
time_type=0;
}
dipchar0(x0,y0,F57,1,time_type,time);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -