我需要通过 I2C 与传感器通信。传感器数据表https://www.hamamatsu.com/resources/pdf/ssd/s11059-02dt_etc_kpic1082e.pdf显示通信示例:
我的代码:
#include <18f2550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#USE delay(clock=48000000)
#use i2c(Master,Fast,sda=PIN_B0,scl=PIN_B1)
#include <usb_cdc.h>
void main() {
i2c_start();
i2c_write(0x54);
i2c_write(0x00);
i2c_write(0x89);
i2c_write(0x54);
i2c_write(0x00);
i2c_write(0x09);
i2c_stop();
usb_cdc_init();
usb_init();
while(TRUE) {
delay_ms(10);
i2c_write(0x54);
i2c_write(0x03);
i2c_write(0x55);
int8 RM = i2c_read(1);
int8 RL = i2c_read(1);
int8 GM = i2c_read(1);
int8 GL = i2c_read(1);
int8 BM = i2c_read(1);
int8 BL = i2c_read(1);
int8 IM = i2c_read(1);
int8 IL = i2c_read(0);
i2c_stop();
usb_task();
if (usb_enumerated()) {
printf( usb_cdc_putc,"%d", (RM << 8) | RL );
printf(usb_cdc_putc, "\f ");
}
}
}
我想知道我的代码是否正确。
最佳答案
在发布的代码中,微处理器和设备之间的 I/O 顺序不正确
以下链接适用于不同的传感器,但确实显示了正确的 I2C sequence
您需要调整:
- 总线地址,
- 注册地址,
- 命令,
- 和数据字节数
您使用的芯片:S11059-02DT/-03DS
强烈建议使用 enum
语句或 #define
语句为固定值赋予有意义的名称,以便读者阅读您的代码(包括将来某个日期的您自己)轻松理解代码的作用
关于c - 与传感器 CCS 编译器的 I2C 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53822594/