STM32F030的I2C发送数据的问题

2019-07-14 16:32发布

最近在调试STM32F030单片机的I2C总线,现在虽然参考官方实例调试成功了 但是有一些地方还不是很明白,暂时也未看懂,希望知道的大神可以赐教 小弟先谢过了!

void I2C_uWriteData(INT8U SlaveAddr,INT16U WriteAddr,INT8U WriteLen,INT8U *WriteBuf)
{
INT8U i;

while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_TransferHandling(I2C1,SlaveAddr,2,I2C_Reload_Mode,I2C_Generate_Start_Write);   // I2C_SoftEnd_Mode
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_TXIS) == RESET);
I2C_SendData(I2C1,WriteAddr >> 8);
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_TXIS) == RESET);
I2C_SendData(I2C1,WriteAddr % 256);
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_TCR) == RESET);
I2C_TransferHandling(I2C1,SlaveAddr,WriteLen,I2C_AutoEnd_Mode,I2C_No_StartStop);   // I2C_Generate_Start_Write
for(i=0;i
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
18条回答
TOPCB
1楼-- · 2019-07-15 10:14
I2C_SoftEnd_Mode,这个就是进入结束模式。如果需要从设备继续工作,就要重新连接,你I2C基础不好。建议还是看看协议吧。
60user135
2楼-- · 2019-07-15 14:16
回复第 8 楼 于2014-02-14 14:50:48发表:
I2C_SoftEnd_Mode,这个就是进入结束模式。如果需要从设备继续工作,就要重新连接,你I2C基础不好。建议还是看看协议吧。 

那读数据的配置也是I2C_SoftEnd_Mode呀,而且个人理解I2C_SoftEnd_Mode配置的意思是 有软件具体操作来发送停止位吧(又自动发送停止位)
TOPCB
3楼-- · 2019-07-15 17:41
 精彩回答 2  元偷偷看……
60user135
4楼-- · 2019-07-15 23:32
回复第 10 楼 于2014-02-15 08:54:04发表:
I2C_SoftEnd_Mode,说明要发送停止位了。这个只是一个定义,为了方便大家理解这个标志位用来做什么。 

这是固件库对此函数的注解和代码
/**
  * @brief  Handles I2Cx communication when starting transfer or during transfer (TC or TCR flag are set).
  * @param  I2Cx: where x can be 1 or 2 to select the I2C peripheral.
  * @param  Address: specifies the slave address to be programmed.
  * @param  Number_Bytes: specifies the number of bytes to be programmed.
  *          This parameter must be a value between 0 and 255.
  * @param  ReloadEndMode: new state of the I2C START condition generation.
  *          This parameter can be one of the following values:
  *            @arg I2C_Reload_Mode: Enable Reload mode .
  *            @arg I2C_AutoEnd_Mode: Enable Automatic end mode.
  *            @arg I2C_SoftEnd_Mode: Enable Software end mode.
  * @param  StartStopMode: new state of the I2C START condition generation.
  *          This parameter can be one of the following values:
  *            @arg I2C_No_StartStop: Don't Generate stop and start condition.
  *            @arg I2C_Generate_Stop: Generate stop condition (Number_Bytes should be set to 0).
  *            @arg I2C_Generate_Start_Read: Generate Restart for read request.
  *            @arg I2C_Generate_Start_Write: Generate Restart for write request.
  * @retval None
  */
void I2C_TransferHandling(I2C_TypeDef* I2Cx, uint16_t Address, uint8_t Number_Bytes, uint32_t ReloadEndMode, uint32_t StartStopMode)
{
  uint32_t tmpreg = 0;
 
  /* Check the parameters */
  assert_param(IS_I2C_ALL_PERIPH(I2Cx));
  assert_param(IS_I2C_SLAVE_ADDRESS(Address)); 
  assert_param(IS_RELOAD_END_MODE(ReloadEndMode));
  assert_param(IS_START_STOP_MODE(StartStopMode));
   
  /* Get the CR2 register value */
  tmpreg = I2Cx->CR2;
 
  /* clear tmpreg specific bits */
  tmpreg &= (uint32_t)~((uint32_t)(I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN | I2C_CR2_START | I2C_CR2_STOP));
 
  /* update tmpreg */
  tmpreg |= (uint32_t)(((uint32_t)Address & I2C_CR2_SADD) | (((uint32_t)Number_Bytes CR2 = tmpreg; 
}
参数的宏定义
#define  I2C_CR2_RELOAD                    ((uint32_t)0x01000000)        /*!< NBYTES reload mode */
#define  I2C_CR2_AUTOEND                 ((uint32_t)0x02000000)        /*!< Automatic end mode (master mode) *
#define  I2C_Reload_Mode                I2C_CR2_RELOAD
#define  I2C_AutoEnd_Mode               I2C_CR2_AUTOEND
#define  I2C_SoftEnd_Mode               ((uint32_t)0x00000000)
我参考的是F05x的编程手册(不知道我参考的编程手册是否适合F03x)
Bit 25 AUTOEND :  Automatic end mode (master mode)
This bit is set and cleared by software.
0: software end mode: TC flag is set when NBYTES data are transferred, stretching SCL low.
1: Automatic end mode: a STOP condition is automatically sent when NBYTES data are
transferred.
Note: This bit has no effect in slave mode or when the RELOAD bit is set.
Bit 24 RELOAD:   NBYTES  reload mode
This bit is set and cleared by software.
0: The transfer is completed after the NBYTES data transfer (STOP or RESTART will follow).
1: The transfer is not completed after the NBYTES data transfer (NBYTES will be reloaded).
TCR flag is set when NBYTES data are transferred, stretching SCL low.
从上述的资料显示应该不是说要发送停止位
 
 
TOPCB
5楼-- · 2019-07-15 23:42
这里的I2C_SoftEnd_Mode 是在地址数据发送以后产生一个restart信号,以表示用来读取E2的数据。而在写模式下,不用产生这个restart信号。
开始我理解的是发送一个NOACK,用来控制I2C总线停止工作做。通过手册,这里控制的是restart信号,可以参考下图中restart产生的信号位置。
chexiaohuo
6楼-- · 2019-07-16 05:26
能告诉我st的参考例程从那边下吗

一周热门 更多>