?? i2c_m.c
字號:
#include<REG66x.H>
/* 主器件函數(shù)庫 */
/*******************************************
申請總線
功能:進(jìn)行I2C總線的初始化--包括時鐘速率,I2C使能,發(fā)送起始信號
********************************************/
void getbus()
{
S1CON = 0x43; // 設(shè)置時鐘為75k(12M),ENS1置位
STA = 1; // 申請總線主機(jī),啟動總線
while(SI == 0); // 等待起始位的發(fā)送
}
/*******************************************
發(fā)送數(shù)據(jù)函數(shù)
功能:用于向總線發(fā)送數(shù)據(jù)
********************************************/
void sendbyte(unsigned char c)
{
S1DAT = c; // 將要發(fā)送的數(shù)據(jù)裝入S1DAT
S1CON = 0x43; // 清除SI位
while(SI == 0); // 等待數(shù)據(jù)發(fā)送
}
/*******************************************
向無子地址器件發(fā)送字節(jié)數(shù)據(jù)函數(shù)
功能:從啟動總線到發(fā)送地址,數(shù)據(jù),結(jié)束總線的全過程,從器件地址sla,
待發(fā)送的數(shù)據(jù)c,如如果返回1表示操作成功,否則表示操作有誤.
********************************************/
bit isendbyte(unsigned char sla, unsigned char c)
{
getbus(); // 啟動總線
sendbyte(sla); // 發(fā)送從器件地址,若無應(yīng)答則返回
if(S1STA != 0x18)
{
S1CON = 0x53;
return 0;
}
sendbyte(c); // 發(fā)送數(shù)據(jù)
if(S1STA != 0x28)
{
S1CON = 0x53;
return 0;
}
S1CON = 0x43; // 結(jié)束總線
return 1;
}
/*********************************************************
向有子地址器件發(fā)送多字節(jié)數(shù)據(jù)函數(shù)
功能:從啟動總線到發(fā)送地址,子地址,數(shù)據(jù),結(jié)束總線的全過程,從器件地址sla
子地址suba,發(fā)送內(nèi)容事s指向的內(nèi)容,發(fā)送no哥字節(jié).如果返回1表示操作成功,
否則操作有誤.
**********************************************************/
bit isendstr(unsigned char sla, unsigned char suba,
unsigned char *s, unsigned char no)
{
unsigned char i;
getbus(); // 啟動總線
sendbyte(sla); // 發(fā)送從器件地址
if (S1STA != 0x18)
{
S1CON = 0x53;
return 0;
}
sendbyte(suba); // 發(fā)送器件子地址
if(S1STA != 0x28)
{
S1CON = 0x53;
return 0;
}
for(i=0; i<no; i++)
{
sendbyte(*s); // 發(fā)送數(shù)據(jù)
if(S1STA != 0x28)
{
S1CON = 0x53;
return 0;
}
s++;
}
S1CON = 0x53;
return 1;
}
/*********************************************************
向無子地址器件讀字節(jié)數(shù)據(jù)函數(shù)
功能: 從啟動總線到發(fā)送地址,讀數(shù)據(jù),借宿總線的全過程,從器件
地址sla,數(shù)據(jù)值c,如果返回1表示操作成功,否則操作有誤.
***********************************************************/
bit irecvbyte(unsigned char sla, unsigned char *c)
{
gerbus(); // 啟動總線
sendbyte(sla+1); // 發(fā)送器件地址
if(S1STA != 0x40)
{
S1CON = 0x53;
return 0;
}
S1CON = 0x43; // 接收一字節(jié)數(shù)據(jù)即發(fā)送非應(yīng)答位
while(SI == 0); // 等待接收數(shù)據(jù)
if(S1STA != 0x58)
{
S1CON=0x53;
return 0;
}
*c = S1DAT;
S1CON = 0x53;
return 1;
}
/*********************************************************
向有子地址器件讀取多字節(jié)數(shù)據(jù)函數(shù)
功能:從啟動總線到發(fā)送地址,子地址,讀數(shù)據(jù),結(jié)束總線的全過程,從
器件地址sla,子地址suba,讀出內(nèi)容放入s指向的存儲區(qū),讀no個字節(jié).
如果返回1表示操作成功,否則操作有誤.
**********************************************************/
bit irecvstr(unsigned char sla, unsigned char suba,
unsigned char *s, unsigned char no)
{
unsigned char i;
getbus(); // 啟動總線
sendbyte(sla); // 發(fā)送從器件地址
if(S1STA != 0x18)
{
S1CON = 0x53;
return 0;
}
sendbyte(suba); // 發(fā)送器件子地址
if(S1STA != 0x28)
{
S1CON = 0x53;
return 0;
}
S1CON = 0x63; // 重新啟動總線
while(SI == 0);
sendbyte(sla+1);
if(S1STA != 0x40)
{
S1CON = 0x53;
return 0;
}
for(i=0; i<no; i++)
{
S1CON = 0x47; // 接收一個字節(jié)數(shù)據(jù)并發(fā)送應(yīng)答位
while(SI == 0); // 等待接收數(shù)據(jù)
if(S1STA != 0x50)
{
S1CON = 0x53;
return 0;
}
*s=S1DAT; // 讀取數(shù)據(jù)
s++;
}
S1CON = 0x43; // 接收最后一字節(jié)數(shù)據(jù)并發(fā)送非應(yīng)答位
while(SI == 0);
*s = S1DAT;
S1CON = 0x53; // 結(jié)束總線
return 1;
}
/***********************************************************************
對于時鐘芯片pcf8573
* 從地址格式: msb 1 1 0 1 0 A1 A0 R/~W
A1 A0為硬件實(shí)現(xiàn)其從地址
* 主發(fā)送幀格式: SLA+R/~W A MODE POINTER A DATA A P
n位數(shù)據(jù) ||{自動b1,b0增加}
MODE POINTER: 0 c2 c1 c0 0 b2 b1 b0
其含義: 0 c2 c1 c0
0 0 0 0 execute address
0 0 0 1 read control/status flags
0 0 1 0 reset prescaler, including seconds counter; without carry for minute counter
0 0 1 1 time adjust, with carry for minute counter (note 1)
0 1 0 0 reset NODA flag
0 1 0 1 set NODA flag
0 1 1 0 reset COMP flag
0 b2 b1 b0
0 0 0 0 time counter hour
0 0 0 1 time counter minutes
0 0 1 0 time counter days
0 0 1 1 time counter months
0 1 0 0 alarm register hours
0 1 0 1 alarm register minutes
0 1 1 0 alarm register days
0 1 1 1 alarm register months
只有當(dāng)c2,c1,c0為0,0,0時,數(shù)據(jù)才和執(zhí)行地址相匹配.
MSB DATA LSB
ADDRESSED TO: UPPER DIGIT LOWER DIGIT
UD UC UB UA LD LC LB LA
0 0 D D D D D D hours
0 D D D D D D D minutes
0 0 D D D D D D days
0 0 0 D D D D D months
0 0 0 m s NODA COMP POWF control/status flags
*********************************************************************************/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -