可不可以直接用i2c向外面按照协议格式一直发数据,需要有什么方面的配置。为什么我的配置之后只能发送地址信息,然后就结束了。。。。。小白求帮助。 /* Includes ------------------------------------------------------------------*/ #include "main.h" /** @addtogroup Template_Project * @{ */ /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ static __IO uint32_t TimingDelay; RCC_ClocksTypeDef RCC_Clocks; __IO uint16_t sEEAddress = 0x44; //__IO uint32_t sEETimeout = sEE_LONG_TIMEOUT; __IO uint16_t sEEDataNum; __IO uint32_t sEETimeout; __IO uint32_t sEETrials = 0; #define sEE_WRITE_ADDRESS1 0x00 #define sEE_READ_ADDRESS1 0x00 #define BUFFER_SIZE1 256 #define BUFFER_SIZE2 256 #define sEE_WRITE_ADDRESS2 (sEE_WRITE_ADDRESS1 + BUFFER_SIZE1) #define sEE_READ_ADDRESS2 (sEE_READ_ADDRESS1 + BUFFER_SIZE1) //状态变量 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus; /* Private functions ---------------------------------------------------------*/ /** * @brief Main program. * @param None * @retval None */ void main(void) { uint8_t senddata=0xa0; GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStruct; // 配置I2C始终资源 RCC_I2CCLKConfig(RCC_I2C1CLK_HSI); //GPIO使能 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); //IIC使能 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); //GPIO管脚配置 GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_1; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin =GPIO_Pin_7; GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin =GPIO_Pin_5; GPIO_Init(GPIOB,&GPIO_InitStructure); /* I2C configuration */ I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_AnalogFilter = I2C_AnalogFilter_Enable; I2C_InitStruct.I2C_DigitalFilter = 0x00; I2C_InitStruct.I2C_OwnAddress1 =0x00; // I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStruct.I2C_Timing =0x00210507;//100Kbits I2C_Init(I2C1, &I2C_InitStruct); //I2C_SMBusAlertCmd(I2C1,ENABLE); I2C_Cmd(I2C1, ENABLE); while(1) { I2C_TransferHandling(I2C1, sEEAddress, 2, I2C_Reload_Mode, I2C_Generate_Start_Write); I2C_SendData(I2C1,senddata); } } /** * @brief Inserts a delay time. * @param nTime: specifies the delay time length, in 10 ms. * @retval None */ void Delay(__IO uint32_t nTime) { TimingDelay = nTime; while(TimingDelay != 0); } /** * @brief Decrements the TimingDelay variable. * @param None * @retval None */ void TimingDelay_Decrement(void) { if (TimingDelay != 0x00) { TimingDelay--; } } /** * @} */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
能不能更加详细一点,谢谢了大哥
I2C_Master_Transmit(uint8_t DevAddress,
uint8_t eerom_start_address,
const uint8_t * pData,
uint16_t Size
)
{
uint32_t sizetmp;
if ((pData == NULL) || (Size == 0))
{
return;
}
while (__HAL_I2C_GET_FLAG(I2C_ISR_BUSY))
{
}
/* Send Slave Address */
/* Set NBYTES to write and reload if size > I2C_MAX_BYTES and generate RESTART */
/* Size > I2C_MAX_BYTES, need to set RELOAD bit */
if (Size > I2C_MAX_BYTES)
{
I2C_TransferConfig(DevAddress, I2C_MAX_BYTES, I2C_CR2_RELOAD, I2C_GENERATE_START_WRITE);
sizetmp = I2C_MAX_BYTES;
}
else
{
I2C_TransferConfig(DevAddress, Size, I2C_CR2_AUTOEND, I2C_GENERATE_START_WRITE);
sizetmp = Size;
}
while (!__HAL_I2C_GET_FLAG(I2C_ISR_TXIS))
{
}
I2C_CHANNEL->TXDR = eerom_start_address;
while ((Size - 1) > 0)
{
/* Wait until TXIS flag is set */
while (!__HAL_I2C_GET_FLAG(I2C_ISR_TXIS))
{
}
/* Write data to TXDR */
I2C_CHANNEL->TXDR = (uint32_t)*pData;
pData++;
sizetmp--;
Size--;
if ((sizetmp == 0) && (Size != 0))
{
/* Wait until TXE flag is set */
while (!__HAL_I2C_GET_FLAG(I2C_ISR_TCR))
{
}
if (Size > I2C_MAX_BYTES)
{
I2C_TransferConfig(DevAddress, I2C_MAX_BYTES, I2C_CR2_RELOAD, I2C_NO_STARTSTOP);
sizetmp = I2C_MAX_BYTES;
}
else
{
I2C_TransferConfig(DevAddress, Size, I2C_CR2_AUTOEND, I2C_NO_STARTSTOP);
sizetmp = Size;
}
}
}
/* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
/* Wait until STOPF flag is set */
while (!__HAL_I2C_GET_FLAG(I2C_ISR_STOPF))
{
}
/* Clear STOP Flag */
__HAL_I2C_CLEAR_FLAG(I2C_ISR_STOPF);
/* Clear Configuration Register 2 */
__HAL_I2C_RESET_CR2();
}
I2C_TransferConfig(uint8_t DevAddress,
uint16_t Size,
uint32_t Mode,
uint32_t Request
)
{
/* clear tmpreg specific bits */
I2C_CHANNEL->CR2 &= (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 */
#if I2C_ADDRESSINGMODE_7BIT
I2C_CHANNEL->CR2 |= (uint32_t)(((uint32_t)(DevAddress << 1) & I2C_CR2_SADD) \
| (((uint32_t)Size << 16) & I2C_CR2_NBYTES) \
| (uint32_t)Mode | (uint32_t)Request);
#else
I2C_CHANNEL->CR2 |= (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) \
| (((uint32_t)Size << 16) & I2C_CR2_NBYTES) \
| (uint32_t)Mode | (uint32_t)Request);
#endif
}
谢谢了,我研究研究
大神,我还想请教你个问题,就是如果我只将stm板子,作为主机,不给其链接从设备,那样就没有反馈的ack信号,那么在地址信息发出之后主机没有得到ack那么他就不会再向从设备发送数据信息,如何通过软件的形式给主机提供反馈的ack信号?大神在帮帮忙