STM32 F401RE ADC その2
開発環境
・評価ボード NUCLEO-F401RE
・IDE MDK-ARM V5 and STM32CubeMX
・OS windows 10 pro
・本
定番!ARMキット&PIC用Cプログラムでいきなりマイコン制御[DVD-ROM付き] (マイコン活用シリーズ)
- 作者:芹井 滋喜
- 発売日: 2016/01/29
- メディア: 単行本
ただし、うまくいくとは限らない模様(´・_・`)w
とりあえずSTM32CubeMXの設定を順に
・Pinout
・Pinout USART2
・Configurationタブ USART
・Configurationタブ ADC1
・Configurationタブ NVIC
としました。
メインのコードはこのようになりました。
・main.c
/* Includes ------------------------------------------------------------------*/ #include "stm32f4xx_hal.h" /* USER CODE BEGIN Includes */ #include <stdio.h> #include <stdlib.h> #include <string.h> /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ ADC_HandleTypeDef hadc1; UART_HandleTypeDef huart2; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ int AdcValue = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); void Error_Handler(void); static void MX_GPIO_Init(void); static void MX_ADC1_Init(void); static void MX_USART2_UART_Init(void); /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ char sio_getc(void); // UARTから1文字取得 void sio_putc(char); // UARTに1文字を出力 void sio_puts(char *); // UARTに文字列を出力 /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ char sio_getc() { char ch; while(HAL_UART_Receive(&huart2, (uint8_t *)&ch, 1, 1000)==HAL_TIMEOUT) ; return ch; } void sio_putc(char ch) { HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 1000); } void sio_puts(char *str) { HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen(str), 1000); } void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { AdcValue = HAL_ADC_GetValue(hadc); sio_puts("ADC IT\n"); } /* USER CODE END 0 */ int main(void) { /* USER CODE BEGIN 1 */ char str[16]; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC1_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ HAL_ADCEx_InjectedStart(&hadc1); HAL_ADC_Start_IT(&hadc1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { sprintf(str, "AD0: %04X\n", AdcValue); sio_puts(str); HAL_Delay(100); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }
結果はこのようになりました。
デバッガでも確認したのですが最初の一回しか呼ばれません・・・そしてその値をとりあえず出力し続けています。
HAL Libraryの手順どおりのはずなのですが・・・困りました。
AD変換部分だけを抜き出すと
ADC_HandleTypeDef hadc1; int AdcValue = 0; static void MX_ADC1_Init(void); void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { AdcValue = HAL_ADC_GetValue(hadc); sio_puts("ADC IT\n"); } int main(void) { HAL_Init(); SystemClock_Config(); MX_ADC1_Init(); HAL_ADCEx_InjectedStart(&hadc1); HAL_ADC_Start_IT(&hadc1); while (1) { sprintf(str, "AD0: %04X\n", AdcValue); sio_puts(str); HAL_Delay(100); } }
という感じです。もうすこしいろいろと探ってみます。
20160916の記事