본문 바로가기

공부/STM32F4

19. [STM32F429] ADC Polling

반응형

 PA0에 ADC를 연결 했습니다.

#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc1;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_ADC1_Init(void);
/* USER CODE BEGIN PFP */
int fputc(int ch, FILE *f)
{
    uint8_t temp[1]={ch};
    HAL_UART_Transmit(&huart1, temp, 1, 2);
  return(ch);
}
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  uint32_t ADC_Val=0;
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_ADC1_Init();
  /* USER CODE BEGIN 2 */
  printf("ADC_Test\r\n");
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    HAL_ADC_Start(&hadc1);
    ADC_Val=HAL_ADC_GetValue(&hadc1);
    printf("%d\r\n",ADC_Val);
    HAL_Delay(500);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

10bit로 설정했습니다.

3V 일때 1024가 최대입니다.

그리고 소수 2번째 자리까지는 볼수 있는데 3째 자리 넘어가면 값이 흔들립니다.

계산으로는 1당 0,0029V 입니다.

 

ADC는 12bit까지 지원합니다만,,,12bit까지 올리면 값이 많이 흔들립니다.

 

반응형

'공부 > STM32F4' 카테고리의 다른 글

[STM32F401CCU6] USB CDC  (0) 2022.03.30
18. [STM32F429] SPI Master 와 Slave  (0) 2021.09.08
stm32cubeIDE  (0) 2020.10.05
17. [STM32F429] A4988 스텝모터 구동  (1) 2020.06.01
mpu_arm7.h 에서 에러  (0) 2020.05.22