【STM32CubeMX】5,STM32之ADC
1,初步配置。对于滑动变阻器的电压输出
还有就是printf、scanf的重定向,在uart.c中
int fputc (int ch ,FILE *f)
{HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xfff);
return ch;
}
int fgetc(FILE *f)
{
uint8_t ch;
HAL_UART_Receive(&huart1,(uint8_t *)&ch,1,0xfff);
return ch;
}
2,以轮寻模式来运行
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_ADC_Start(&hadc2);
HAL_ADC_PollForConversion(&hadc2, 50);
if(HAL_ADC_STATE_REG_EOC)//²»»á×Ô¶¯¸´Î»
{
ADC_ConvertedValue=HAL_ADC_GetValue(&hadc2);
ADC_ConvertedValueLocal =(float) ADC_ConvertedValue/4096*3.3;
printf("\r\n The current AD value = 0x%04X \r\n", ADC_ConvertedValue);
printf("\r\n The current AD value = %f V \r\n",ADC_ConvertedValueLocal);
}
HAL_Delay(1000);
}
/* USER CODE END 3 */
ADC_ConvertedValueLocal =(float) ADC_ConvertedValue/4096*3.3; //不同的类型有不同的转换公式,但一般都要除以4096
3,不同的模式进入赋值的判断语句不同
1
2
3
4
5
6
7
8
9
10
|
/*##-3- Start the conversion process ######/ if(HAL_ADC_Start(&AdcHandle) != HAL_OK) { Error_Handler(); } /*##-4- Wait for the end of conversion ######/ HAL_ADC_PollForConversion(&AdcHandle, 10); if(HAL_ADC_GetState(&AdcHandle) == HAL_ADC_STATE_EOC_REG) { /*##-5- Get the converted value of channel ##*/ uhADCxConvertedValue = HAL_ADC_GetValue(&AdcHandle); } |
2 中断模式
1
2
3
4
5
6
7
|
/*##-3- Start the conversion process ######/ if(HAL_ADC_StartIT(&AdcHandle) != HAL_OK) { Error_Handler(); } void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle) { /* Get the converted value of regular channel */ uhADCxConvertedValue = HAL_ADC_GetValue(AdcHandle); } |
3 DMA模式
1
2
3
4
5
6
7
|
/*##-3- Start the conversion process ######/ if(HAL_ADC_StartDMA(&AdcHandle,&uhADCxConvertedValue,1) != HAL_OK) { Error_Handler(); } void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle) { /* Turn LED1 on: Transfer process is correct */ BSP_LED_On(LED1); } |
3、几个函数
callback函数
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc);
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc);
void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc);
void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc);
- 1
- 2
- 3
- 4
Adc中断、DMA传输、看门狗超过阀值、发生Adc错误,这些函数返回前都调用了Callback函数,用来在非中断模式下处理Adc数据,如果想进行一些操作可以直接修改Callback函数。
/**HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
官方给的解释说明很好用
* @brief Get ADC regular group conversion result.* @note Reading register DR automatically clears ADC flag EOC
* (ADC group regular end of unitary conversion).
* @note This function does not clear ADC flag EOS
* (ADC group regular end of sequence conversion).
* Occurrence of flag EOS rising:
* - If sequencer is composed of 1 rank, flag EOS is equivalent
* to flag EOC.
* - If sequencer is composed of several ranks, during the scan
* sequence flag EOC only is raised, at the end of the scan sequence
* both flags EOC and EOS are raised.
* To clear this flag, either use function:
* in programming model IT: @ref HAL_ADC_IRQHandler(), 在中断模式下,参考HAL_ADC_IRQHandler()
in programming model polling: @ref HAL_ADC_PollForConversion() or @ref __HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS).
在轮询模式下,参考HAL_ADC_PollForConversion()或者__HAL_ADC_CLEAR_FLAG(&hadc, ADC_FLAG_EOS)
* @param hadc: ADC handle* @retval ADC group regular conversion data
*/
uint32_t HAL_ADC_GetValue(ADC_HandleTypeDef* hadc)
{
/* Check the parameters */
assert_param(IS_ADC_ALL_INSTANCE(hadc->Instance));
/* Note: EOC flag is not cleared here by software because automatically */
/* cleared by hardware when reading register DR. */
/* Return ADC converted value */
return hadc->Instance->DR;
}
uint32_tEOCSelection -- EOC(EndOfConversion),用来做轮询或中断标记;每个rank转换结束,或序列转换结束;
MSP:MCU Specific Package
4,DMA多通道的ADC
PC0对应ad1,PC1对应ad2,
看串口的数据,ad2的数据是对的。ad1是不是接了高电平???
5.2 增加ADC1的温度通道
可以看到ad1,ad2与上面只有两个时的数字是一样的,ad3温度。加入温度等式,如下图
上一篇: ES6构建**项目
下一篇: 基于FPGA的SDRAM控制器设计(2)