stm32cube中文教程:AD用TIM8定时采集,uart3输出
arm片子stm32f407ZGT,用ADC1,TIM8,UART3,。
ad脚PB0,uart脚PB10和11,PF6输出led灯。
晶振25M,时钟168M。
stm32cubemx软件设置:
uart9600,8,0,1,不使能uart中断,TX:PULL_up,fast。RX:NO PULL_UP,fast
DMA不使能。
ADC设置:修改了continuous conversion mode:enable;end of conversion selection :Eoc flag signle channel
External Trigger conversion Edge : Trigger detection on the rising edge
External Trigger Conversion Source : Time 8 Trigger Out event
其余的默认即可。
ADC中断不使能。
TIme8定时设置:
Prescaler(psc - 16 bits value) : 0
counter mode : UP
counter Period(autoreload register - 16 bits value) : 60
internal clock division (CDK) : NO Division
Repetition counter(RCR - 8bits value) : 0
slave mode controller : trigger mode
master/slave mode : Disable
trigger event selection : Update enent
不使能任何TIM中断,DMA不使能。
----------------------------------------------------------
以上软件配置结束,自动生成代码即可。
main文件中添加:
#include "stdio.h"#ifdef __GNUC__ /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)#else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)#endif / __GNUC__ //** * @brief Retargets the C library printf function to the USART. * @param None * @retval None */PUTCHAR_PROTOTYPE{ / Place your implementation of fputc here / / e.g. write a character to the EVAL_COM1 and Loop until the end of transmission / HAL_UART_Transmit(&huart3 , (uint8_t *)&ch, 1, 0xFFFF); return ch;}
---------------------------------------为了能使用printf。
main()函数中添加:
/ USER CODE BEGIN 2 / /-3- Start the conversion process and enable interrupt ######/ if(HAL_ADC_Start_IT(&hadc1) != HAL_OK) { / Start Conversation Error / // Error_Handler(); HAL_GPIO_WritePin (GPIOF,GPIO_PIN_6,GPIO_PIN_SET ); HAL_Delay (5000); } if(HAL_TIM_Base_Start(&htim8) != HAL_OK) { / Counter Enable Error / //Error_Handler(); HAL_GPIO_WritePin (GPIOF,GPIO_PIN_6,GPIO_PIN_SET ); HAL_Delay (5000); } / USER CODE END 2 / / USER CODE BEGIN 3 / / Infinite loop / while (1) { HAL_Delay (1000); HAL_GPIO_TogglePin (GPIOF,GPIO_PIN_6); printf ("%d",s_value); // HAL_ADC_Start_IT(&hadc1); } / USER CODE END 3 /
-------------------------------------------------开启AD和TIM
底部添加:
/ USER CODE BEGIN 4 /void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle){ / Get the converted value of regular channel / uhADCxConvertedValue = HAL_ADC_GetValue(AdcHandle); v_value += uhADCxConvertedValue; count++; if(count>=500) { s_value = v_value/500; v_value = 0; count=0; }}/ USER CODE END 4 /
---------------------------------------AD采集完成数据处理调用
其他文件不要变动。
下面是完整的main文件代码:
/ Includes ------------------------------------------------------------------/#include "stm32f4xx_hal.h"/ Private variables ---------------------------------------------------------/ADC_HandleTypeDef hadc1;TIM_HandleTypeDef htim8;UART_HandleTypeDef huart3;/ USER CODE BEGIN 0 /#include "stdio.h"#ifdef __GNUC__ /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf set to 'Yes') calls __io_putchar() */ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)#else #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)#endif / __GNUC__ //** * @brief Retargets the C library printf function to the USART. * @param None * @retval None */PUTCHAR_PROTOTYPE{ / Place your implementation of fputc here / / e.g. write a character to the EVAL_COM1 and Loop until the end of transmission / HAL_UART_Transmit(&huart3 , (uint8_t *)&ch, 1, 0xFFFF); return ch;}/ Variable used to get converted value /__IO uint16_t uhADCxConvertedValue = 0; int32_t v_value; int32_t s_value; uint16_t count=0;/ USER CODE END 0 // Private function prototypes -----------------------------------------------/static void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_ADC1_Init(void);static void MX_TIM8_Init(void);static void MX_USART3_UART_Init(void);int main(void){ / USER CODE BEGIN 1 / / 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_TIM8_Init(); MX_USART3_UART_Init(); / USER CODE BEGIN 2 / /-3- Start the conversion process and enable interrupt ######/ if(HAL_ADC_Start_IT(&hadc1) != HAL_OK) { / Start Conversation Error / // Error_Handler(); HAL_GPIO_WritePin (GPIOF,GPIO_PIN_6,GPIO_PIN_SET ); HAL_Delay (5000); } if(HAL_TIM_Base_Start(&htim8) != HAL_OK) { / Counter Enable Error / //Error_Handler(); HAL_GPIO_WritePin (GPIOF,GPIO_PIN_6,GPIO_PIN_SET ); HAL_Delay (5000); } / USER CODE END 2 / / USER CODE BEGIN 3 / / Infinite loop / while (1) { HAL_Delay (1000); HAL_GPIO_TogglePin (GPIOF,GPIO_PIN_6); printf ("%d",s_value); // HAL_ADC_Start_IT(&hadc1); } / USER CODE END 3 /}/** System Clock Configuration*/static void SystemClock_Config(void){ RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; __PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 25; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; HAL_RCC_OscConfig(&RCC_OscInitStruct); RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1 |RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);}/ ADC1 init function /void MX_ADC1_Init(void){ ADC_ChannelConfTypeDef sConfig; ADC_MultiModeTypeDef multimode; /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */ hadc1.Instance = ADC1; hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2; hadc1.Init.Resolution = ADC_RESOLUTION12b; hadc1.Init.ScanConvMode = DISABLE; hadc1.Init.ContinuousConvMode = ENABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.NbrOfDiscConversion = 1; hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T8_TRGO; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 1; hadc1.Init.DMAContinuousRequests = DISABLE; hadc1.Init.EOCSelection = EOC_SINGLE_CONV; HAL_ADC_Init(&hadc1); /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time. */ sConfig.Channel = ADC_CHANNEL_8; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; HAL_ADC_ConfigChannel(&hadc1, &sConfig); /**Configure the ADC multi-mode */ multimode.Mode = ADC_MODE_INDEPENDENT; multimode.TwoSamplingDelay = ADC_TWOSAMPLINGDELAY_5CYCLES; HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode);}/ TIM8 init function /void MX_TIM8_Init(void){ TIM_SlaveConfigTypeDef sSlaveConfig; TIM_MasterConfigTypeDef sMasterConfig; htim8.Instance = TIM8; htim8.Init.Prescaler = 0; htim8.Init.CounterMode = TIM_COUNTERMODE_UP; htim8.Init.Period = 60; htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim8.Init.RepetitionCounter = 0; HAL_TIM_Base_Init(&htim8); sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER; sSlaveConfig.InputTrigger = TIM_TS_ITR0; HAL_TIM_SlaveConfigSynchronization(&htim8, &sSlaveConfig); sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig);}/ USART3 init function /void MX_USART3_UART_Init(void){ huart3.Instance = USART3; huart3.Init.BaudRate = 9600; huart3.Init.WordLength = UART_WORDLENGTH_8B; huart3.Init.StopBits = UART_STOPBITS_1; huart3.Init.Parity = UART_PARITY_NONE; huart3.Init.Mode = UART_MODE_TX_RX; huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart3.Init.OverSampling = UART_OVERSAMPLING_16; HAL_UART_Init(&huart3);}/** Configure pins as * Analog * Input * Output * EVENT_OUT * EXTI*/void MX_GPIO_Init(void){ GPIO_InitTypeDef GPIO_InitStruct; / GPIO Ports Clock Enable / __GPIOF_CLK_ENABLE(); __GPIOH_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); /Configure GPIO pin : PF6 / GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);}/ USER CODE BEGIN 4 /void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle){ / Get the converted value of regular channel / uhADCxConvertedValue = HAL_ADC_GetValue(AdcHandle); v_value += uhADCxConvertedValue; count++; if(count>=500) { s_value = v_value/500; v_value = 0; count=0; }}/ USER CODE END 4 /
|