STM32通过I2C发送12位ADC

时间:2018-01-11 06:06:50

标签: casting stm32 i2c dma adc

如何理解通过I2C发送的STM32F4数据?我正在将这个I2C流从一个STM32F4发送到另一个STM32F4。

我得到了我期望的ACK,但数据本身看起来有点不寻常。

我有一个12位ADC,我正在通过DMA管理。这是设置代码:

  volatile uint32_t ADCValue[2];
 if(HAL_ADC_Start(&hadc1) != HAL_OK)
        HAL_GPIO_WritePin(GPIOC, LED3_Pin, SET);
 if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)ADCValue,4) != HAL_OK)
        HAL_GPIO_WritePin(GPIOC, LED4_Pin, SET);
 while(1){
 while(HAL_I2C_Master_Transmit(&hi2c3, 252, (uint8_t*) ADCValue[1], 4, 10000) != HAL_OK)
}

而且,这是ADC设置

ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;

/* ADC1 init function */
void MX_ADC1_Init(void)
{
  ADC_ChannelConfTypeDef sConfig;

    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 
    */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  hadc1.Init.Resolution = ADC_RESOLUTION_8B;
  hadc1.Init.ScanConvMode = ENABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 2;
  hadc1.Init.DMAContinuousRequests = ENABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
    */
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_144CYCLES;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. 
    */
  sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = 2;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

所以,很自然地,我认为我在这里做的最麻烦的事情是尝试将单个ADCValue元素分解为4个8位值,因为它被定义为32位int ...

我在地址字节后得到4个字节,但我不确定我在DMA和转换类型中做的这件事是否存在根本缺陷。

任何C专家都对如何使用HAL库通过I2C发送32位值有所了解?

1 个答案:

答案 0 :(得分:0)

我认为你发送了一个指向I2C的无效指针:

 while(HAL_I2C_Master_Transmit(&hi2c3, 252, (uint8_t*) ADCValue, 4, 10000) != HAL_OK)

我不明白你想通过I2C发送什么,这意味着ADCValue [1]的值是一个指向要传输的数据的指针,也许你应该改变:

 while(HAL_I2C_Master_Transmit(&hi2c3, 252, &((uint8_t*) ADCValue)[1], 4, 10000) != HAL_OK)

如果要发送ADCValue的值,或者想要移位1个字节:

  <a onclick="window.open(this.href);return false;" href="javascript: document.body.innerHTML='<div>&lt;Img width=&quot;250&quot; height=&quot;250&quot; src=&quot;file:///home/lt-176/tudip-image.jpg&quot; alt=&quot;text&quot;&gt;</div>';">VIEW2</a>

在这种情况下,如果你在uin32数组和uint8数组之间建立联合会更容易。