实验2:CubeMx+protues+AD单通道采样

目标:实现采样读取AD口的电压。

 

实验2:CubeMx+protues+AD单通道采样_第1张图片

 

 

实验2:CubeMx+protues+AD单通道采样_第2张图片

 

添加全局变量

  /* USER CODE BEGIN 1 */
	uint16_t ADC1_Value;
  /* USER CODE END 1 */

在收到串口指令的时候,进行采样,并在串口打印:

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    unsigned char *pCmd = NULL;
	  unsigned char buf[128];
    int len;
    /* Prevent unused argument(s) compilation warning */
    UNUSED(huart);
    /* NOTE: This function Should not be modified, when the callback is needed,
             the HAL_UART_TxCpltCallback could be implemented in the user file
    */
 
	switch(aRxBuffer){
        case 'a':
            pCmd = NULL;
            len  = 0;
            HAL_ADC_Start(&hadc1);
            HAL_ADC_PollForConversion(&hadc1, 50);
 
            if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
            {
               ADC1_Value = HAL_ADC_GetValue(&hadc1);
               snprintf(buf,sizeof(buf),"ADC1 Reading : %d \r\n",ADC1_Value);
							 HAL_UART_Transmit(&huart1, (uint8_t *)buf, strlen(buf),0xFFFF);
               snprintf(buf,sizeof(buf),"PA0 Voltage : %dmV \r\n",ADC1_Value*5*1000/4096);
							 HAL_UART_Transmit(&huart1, (uint8_t *)buf, strlen(buf),0xFFFF);
            }
            break;
        case 'b':
            pCmd = "command b\r\n";
            len  = strlen(pCmd);
            break;
        case 'c':
            pCmd = "command c\r\n";
            len  = strlen(pCmd);
            break;
        default:
            pCmd = "error cmd\r\n";
            len  = strlen(pCmd);
            break;
	}
	
    if(pCmd != NULL)HAL_UART_Transmit(&huart1, (uint8_t *)pCmd, len,0xFFFF);
	  HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);   //ÔÙ¿ªÆô½ÓÊÕÖжÏ
}
/* USER CODE END 4 */

在调节可调电阻,并通过串口发送a进行实时采样,可以看到串口打印出来的实时电压值:

 

实验2:CubeMx+protues+AD单通道采样_第3张图片

 

本文验证了AD采样的基本原理,实际使用时,需要加上定时器和DMA方式的采样,后续验证。

本文所涉及到的代码在如下地址下载:

https://download.csdn.net/download/qq_39657229/10907416

 

你可能感兴趣的:(实验2:CubeMx+protues+AD单通道采样)