我在项目中用到了BQ24195,通过STM32单片机的I2C读写寄存器,现在发现我可以正常的读寄存器的值,但我如果设置寄存器时,设置相关寄存器后读取的话显示是0,过一段时间后显示为默认值,无论CE脚设置是高电平或低电平。
KW X:
亲;你是如何设置寄存器的?
Xiao Ma3:
回复 KW X:
你好!
我的部分代码如下:
CE_POWER = 0; delay_ms(2000);
slave_addr = 0x6B;reg_addr = 0x1;data_addr = 0x2b;
i2c_write_sreg(slave_addr,reg_addr,data_addr); delay_ms(500); reg_data = i2c_read_sreg(slave_addr, reg_addr) ;printf("0x%02x ",reg_data); delay_ms(500);
unsigned char i2c_read_sreg(unsigned char slave_addr, unsigned char reg_addr) { int i; unsigned char t; unsigned char reg_data; i2c_start(); t = (slave_addr << 1) | 0; i2c_write_byte(t); i2c_write_byte(reg_addr);
i2c_start(); t = (slave_addr << 1) | 1; i2c_write_byte(t); //i2c_write_byte(reg_addr); reg_data = i2c_nread_byte(); i2c_stop(); return reg_data;} void i2c_write_sreg (unsigned char slave_addr, unsigned char reg_addr, unsigned char reg_data)
{ int i; unsigned char t; i2c_start(); t = (slave_addr << 1) | 0; i2c_write_byte(t); i2c_write_byte(reg_addr); //i2c_write_byte(slave_addr); i2c_write_byte(reg_data); i2c_stop(); return;}
Xiao Ma3:
回复 KW X:
你好!
我的部分代码如下:
CE_POWER = 0;delay_ms(2000);
slave_addr = 0x6B;reg_addr = 0x1;data_addr = 0x2b;
i2c_write_sreg(slave_addr,reg_addr,data_addr);delay_ms(500);reg_data = i2c_read_sreg(slave_addr, reg_addr) ;printf("0x%02x ",reg_data);delay_ms(500);
unsigned char i2c_read_sreg(unsigned char slave_addr, unsigned char reg_addr) { int i; unsigned char t; unsigned char reg_data; i2c_start(); t = (slave_addr << 1) | 0; i2c_write_byte(t); i2c_write_byte(reg_addr);
i2c_start();t = (slave_addr << 1) | 1; i2c_write_byte(t); //i2c_write_byte(reg_addr);reg_data = i2c_nread_byte();i2c_stop();return reg_data;} void i2c_write_sreg (unsigned char slave_addr, unsigned char reg_addr, unsigned char reg_data)
{ int i; unsigned char t; i2c_start(); t = (slave_addr << 1) | 0;i2c_write_byte(t); i2c_write_byte(reg_addr); //i2c_write_byte(slave_addr);i2c_write_byte(reg_data); i2c_stop();return;}
DZW:
回复 Xiao Ma3:
看你的程序步骤没错啊,现在调通了吗?我也遇到这个问题,读写不了寄存器,能发你的例程我看看吗?274344073@qq.com