您的位置 首页 主动

PIC单片机CCS之C言语(#USE I2C)

#USEI2C语法:#usei2c(options)options被逗号隔开,可能是:MASTER//设置成主机方式SLAVE//设置成从机方式SCL=pin//指定SCL引…

#USE I2C

语法:#use i2c(options)

options被逗号离隔,可能是:

MASTER //设置成主机方法

SLAVE //设置成从机方法

SCL=pin //指定SCL引脚(pin是一个位地址)

SDA=pin //指定SDA引脚

ADDRESS=nn //指定从机方法地址

FAST //运用fast I2C标准

SLOW //运用slow I2C标准

RESTART_WDT //在I2C_READ等候的时分,重新启动WDT

FORCE_HW //运用硬件I2C函数

NOFLOAT_HIGH //不允许信号漂浮至高,从低到高驱动信号

SMBUS //总线不运用I2C,但很类似,即模仿I2C

意图:I2C的零件库包含了一个完成I2C总线的函数, #USE I2C使得I2C_START, I2C_STOP, I2C_READ, I2C_WRITE和I2C_POLL函数坚持有用,直到下一个#USE I2C的呈现停止.除非指定了FORCE_HW,否则会发生模仿I2C的软件函数.SLAVE方法只能同内置的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一同运用. 在前面已介绍过了.

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ziliao/zhudong/268184.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部