打印
[其他ST产品]

stm32cube中文教程:AD用TIM8定时采集,uart3输出

[复制链接]
569|13
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
stormwind123|  楼主 | 2022-12-7 09:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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 /

使用特权

评论回复
沙发
stormwind123|  楼主 | 2022-12-7 09:11 | 只看该作者
那个printf用的代码是:
#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(&huart2 , (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}

使用特权

评论回复
板凳
laocuo1142| | 2022-12-8 09:00 | 只看该作者
请问大神 这个实验最后得出的现象是什么啊 用示波器么?




使用特权

评论回复
地板
麻花油条| | 2022-12-9 09:00 | 只看该作者
主按照你的设置,我的事Nucleo 401re的板子,结果永远出的数据是0 .。。。




使用特权

评论回复
5
Bblythe| | 2024-10-16 07:23 | 只看该作者

因此以更高的速度使用重型铣削钻头。

使用特权

评论回复
6
周半梅| | 2024-10-16 09:19 | 只看该作者

共模电感有时候又叫共模扼流圈

使用特权

评论回复
7
Pulitzer| | 2024-10-16 10:22 | 只看该作者

电镀半孔可用于标准和高级PCB设计。

使用特权

评论回复
8
Wordsworth| | 2024-10-16 13:21 | 只看该作者

因此,可以精确铣削电镀半孔并提高工艺可靠性。

使用特权

评论回复
9
Clyde011| | 2024-10-16 14:24 | 只看该作者

确保当开关管导通,

使用特权

评论回复
10
公羊子丹| | 2024-10-16 15:17 | 只看该作者

高频变压器磁芯利用率高(与单端电路相比)、电源电压利用率高(与后面要叙述的半桥电路相比)、输出功率大、两管基极均为低电平,驱动电路简单。

使用特权

评论回复
11
万图| | 2024-10-16 16:20 | 只看该作者

静态测试是通过使用直流电压表和负载电流计来分别测试电源的输出电压和输出电流

使用特权

评论回复
12
Uriah| | 2024-10-16 17:23 | 只看该作者

主要起到支承作用,板不会散掉。

使用特权

评论回复
13
帛灿灿| | 2024-10-16 19:19 | 只看该作者

为了抑制共模干扰

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

410

主题

2097

帖子

2

粉丝