- I2C口没有用对,本来应该是I2C1,我配置成了I2C0、
- /* Tmp1075 i2c 7-bit address */
#define TMP1075_I2C_ADDRESS (0x48<<1) //这个地址要是slave address 左移一位
/* send slave address to I2C bus */
i2c_master_addressing(I2C1, TMP1075_I2C_ADDRESS, I2C_TRANSMITTER);
void i2c_master_addressing(uint32_t i2c_periph, uint32_t addr, uint32_t trandirection)
{
/* master is a transmitter or a receiver */
if(I2C_TRANSMITTER == trandirection){
addr = addr & I2C_TRANSMITTER;
}else{
addr = addr | I2C_RECEIVER;
}
/* send slave address */
I2C_DATA(i2c_periph) = addr;
}
/* I2C transfer direction */
#define I2C_RECEIVER ((uint32_t)0x00000001U) /*!< receiver */
#define I2C_TRANSMITTER ((uint32_t)0xFFFFFFFEU) /*!< transmitter */
从这些过程就可以看出,需要的addr 参数是一个左移一位后的地址,因为最后一位读写位会在i2c_master_addressing中去决定
附上代码:
先初始化:
void tmp1075_power_on(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
gpio_init(TMP1075_POWER_EN_GPIO, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ,TMP1075_POWER_EN_PIN);
delay_1ms(1000);
//gpio_bit_reset(TMP1075_POWER_EN_GPIO,TMP1075_POWER_EN_PIN);
gpio_bit_set(TMP1075_POWER_EN_GPIO,TMP1075_POWER_EN_PIN);
delay_1ms(1000);
}
void tmp1075_gpio_config(void)
{
rcu_periph_clock_enable(RCU_AF);
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* enable I2C1 clock */
rcu_periph_clock_enable(RCU_I2C1);
/* connect PB10 to I2C1_SCL */
/* connect PB11 to I2C1_SDA */
gpio_init(I2C1_GPIO_NUM, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, I2C1_GPIO_SCL_PIN | I2C1_GPIO_SDA_PIN );
}
void tmp1075_i2c_config(void)
{
/* configure I2C1 clock */
i2c_clock_config(I2C1, 100000, I2C_DTCY_2);
/* configure I2C1 address */
i2c_mode_addr_config(I2C1, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, TMP1075_I2C_ADDRESS);
/* enable I2C1 */
i2c_enable(I2C1);
/* enable acknowledge */
i2c_ack_config(I2C1, I2C_ACK_ENABLE);
}
读取数据:
uint8_t databuf[2] = {0x00,0x00};
//memset(databuf,0x00,sizeof(databuf)/sizeof(databuf[0]));
tmp1075_buffer_read(databuf,DATA_REG,2);
printf("databuf[0] = 0x%x,databuf[1] = 0x%x\n",databuf[0],databuf[1]);
void tmp1075_buffer_read(uint8_t* p_buffer, uint8_t read_address, uint16_t number_of_byte)
{
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
if(2 == number_of_byte){
i2c_ackpos_config(I2C1,I2C_ACKPOS_NEXT);
}
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C1);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C1, TMP1075_I2C_ADDRESS, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
while(SET != i2c_flag_get( I2C1 , I2C_FLAG_TBE));
/* enable I2C1*/
i2c_enable(I2C1);
/* send the EEPROM's internal address to write to */
i2c_data_transmit(I2C1, read_address);
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C1);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C1, TMP1075_I2C_ADDRESS, I2C_RECEIVER);
if(number_of_byte < 3){
/* disable acknowledge */
i2c_ack_config(I2C1,I2C_ACK_DISABLE);
}
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C1,I2C_FLAG_ADDSEND);
if(1 == number_of_byte){
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
}
/* while there is data to be read */
while(number_of_byte){
if(3 == number_of_byte){
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
/* disable acknowledge */
i2c_ack_config(I2C1,I2C_ACK_DISABLE);
}
if(2 == number_of_byte){
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C1, I2C_FLAG_BTC));
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C1);
}
/* wait until the RBNE bit is set and clear it */
if(i2c_flag_get(I2C1, I2C_FLAG_RBNE)){
/* read a byte from the EEPROM */
*p_buffer = i2c_data_receive(I2C1);
/* point to the next location where the byte read will be saved */
p_buffer++;
/* decrement the read bytes counter */
number_of_byte--;
}
}
/* wait until the stop condition is finished */
while(I2C_CTL0(I2C1)&0x0200);
/* enable acknowledge */
i2c_ack_config(I2C1,I2C_ACK_ENABLE);
i2c_ackpos_config(I2C1,I2C_ACKPOS_CURRENT);
}
博客指出I2C口配置错误,本应配置为I2C1却配置成了I2C0。还提到I2C地址参数,需将slave address左移一位,最后一位读写位会在i2c_master_addressing中决定,同时附上了初始化和读取数据的代码。

4481

被折叠的 条评论
为什么被折叠?



