请问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条回答
60user82
1楼-- · 2019-07-15 03:11
nyvvhxcs 发表于 2018-9-25 11:30
没有指明发送数据的数量

能不能更加详细一点,谢谢了大哥
nyvvhxcs
2楼-- · 2019-07-15 04:49
我看了库函数就头晕,给你发个我的代码
nyvvhxcs
3楼-- · 2019-07-15 05:30
 精彩回答 2  元偷偷看……
nyvvhxcs
4楼-- · 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
}
nyvvhxcs
5楼-- · 2019-07-15 07:40
你参考看看
60user82
6楼-- · 2019-07-15 10:06
nyvvhxcs 发表于 2018-9-25 12:02
void
I2C_Master_Transmit(uint8_t         DevAddress,
                    uint8_t         eerom_start_address,

谢谢了,我研究研究

一周热门 更多>