新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > PIC單片機(jī)CCS之C語(yǔ)言(#USE I2C)

PIC單片機(jī)CCS之C語(yǔ)言(#USE I2C)

作者: 時(shí)間:2016-11-17 來(lái)源:網(wǎng)絡(luò) 收藏
#USE I2C

語(yǔ)法:#use i2c(options)

本文引用地址:http://m.butianyuan.cn/article/201611/315404.htm

options被逗號(hào)隔開(kāi),可能是:

MASTER //設(shè)置成主機(jī)方式

SLAVE //設(shè)置成從機(jī)方式

SCL=pin //指定SCL引腳(pin是一個(gè)位地址)

SDA=pin //指定SDA引腳

ADDRESS=nn //指定從機(jī)方式地址

FAST //使用fast I2C規(guī)范

SLOW //使用slow I2C規(guī)范

RESTART_WDT //在I2C_READ等待的時(shí)候,重新啟動(dòng)WDT

FORCE_HW //使用硬件I2C函數(shù)

NOFLOAT_HIGH //不允許信號(hào)漂浮至高,從低到高驅(qū)動(dòng)信號(hào)

SMBUS //總線不使用I2C,但很相似,即模擬I2C

目的:I2C的零件庫(kù)包含了一個(gè)實(shí)現(xiàn)I2C總線的函數(shù), #USE I2C使得I2C_START, I2C_STOP, I2C_READ, I2C_WRITE和I2C_POLL函數(shù)保持有效,直到下一個(gè)#USE I2C的出現(xiàn)為止.除非指定了FORCE_HW,否則會(huì)產(chǎn)生模擬I2C的軟件函數(shù).SLAVE方式只能同內(nèi)置的SSP一起被使用.

例子:#use I2C(master, sda=PIN_B0, scl=PIN_B1)

例子:#use i2c(master,sda=EEPROM_SDA, scl=EEPROM_SCL)

// init_ext_eeprom(); Call before the other functions are used //
// write_ext_eeprom(a, d); Write the byte d to the address a //
// d = read_ext_eeprom(a); Read the byte d from the address a //

#define EEPROM_ADDRESS long int
#define EEPROM_SIZE 32768

void init_ext_eeprom()
{
output_float(EEPROM_SCL);
output_float(EEPROM_SDA);

}
void write_ext_eeprom(long int address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xa0);
i2c_write(address>>8);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xa0);
while(status==1)
{
i2c_start();
status=i2c_write(0xa0);
}
}
BYTE read_ext_eeprom(long int address) {
BYTE data;
i2c_start();
i2c_write(0xa0);
i2c_write(address>>8);
i2c_write(address);
i2c_start();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
}
void Long_write_ext_eeprom(long int address, Long data) {
int A;
for (A=0;A<4;A++)
write_ext_eeprom(A + address, *(&data + A));
}

Long Long_read_ext_eeprom(long int address) {
int A;
Long Data;
for (A=0;A<4;A++)
*(&Data + A) = read_ext_eeprom( A + address);
return(Data);
}

#use I2C(slave, sda=PIN_C4, scl=PIN_C3, address=0xa0, FORCE_HW)例子文件:ex_extee.c同2464.c一起使用. 在前面已介紹過(guò)了.



關(guān)鍵詞: PIC單片機(jī)CCSUSEI2

評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉