请问stm32f0可不可以直接用i2c向外面按照协议格式一直发数据

2019-07-14 17:27发布

可不可以直接用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****/
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
18条回答
nyvvhxcs
2019-07-15 07:38
void
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
}

一周热门 更多>