新闻中心

EEPW首页>嵌入式系统>设计应用> PIC单片机CCS之C语言(#USE I2C)

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

作者: 时间:2016-11-17 来源:网络 收藏
#USEI2C

语法:#use i2c(options)

本文引用地址://m.amcfsurvey.com/article/201611/315404.htm

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一起使用. 在前面已介绍过了.



关键词:PIC单片机CCSUSEI2

评论


技术专区

关闭