实例:ADC_RegularConversion_DMA 1 首先安装Keil. STM32F4xx_DFP.2.9.0.pack ,该文件支持STM32F412ZG 2 打开:Project( 工程)—op tionsfor target 2.1 Device界面的下拉列表选择STM32F412ZGTx
2.2 target 界面 晶振 8Mhz
2.3 选择调试器:st-link debugger
2.4 点击settings 按钮
2.5 打开utilities界面
2.6 点击settings 按钮,进入debug 界面
2.7 进入flash download界面
3 编译时候 少文件 #include "stm32f4xx_hal.h" 需要官网下载 hal库。
4 程序分析: 4.1主函数 int main(void) { ADC_ChannelConfTypeDef sConfig; HAL_Init();/* 初始化HAL库函数 */ /* 配置 LED1 andLED2输出 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); /* 配置系统时钟 100MHz,STM32F412ZG可不支持超频啊 */ SystemClock_Config(); /*adc硬件配置*/ AdcHandle.Instance =ADCx; AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4; //分频器 AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; //12bit 分辨率 AdcHandle.Init.ScanConvMode = DISABLE; /*扫描模式关闭 */ AdcHandle.Init.ContinuousConvMode = ENABLE; /*连续采集模式关闭,只有在触发沿转化一次 */ AdcHandle.Init.DiscontinuousConvMode = DISABLE; /*参数被丢弃,因为序列发生器是禁用的*/ AdcHandle.Init.NbrOfDiscConversion = 0; AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /*当外部事件触发,adc开始转化 */ AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1; AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; AdcHandle.Init.NbrOfConversion = 1; AdcHandle.Init.DMAContinuousRequests = ENABLE; AdcHandle.Init.EOCSelection = DISABLE; if(HAL_ADC_Init(&AdcHandle) != HAL_OK) { /* ADC初始化错误处理 */ Error_Handler(); } /*配置adc通道*/ sConfig.Channel =ADC_CHANNEL_10; sConfig.Rank = 1; sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; sConfig.Offset = 0; if(HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK) { /*通道配置错误 */ Error_Handler(); } /*开始adc转化*/ if(HAL_ADC_Start_DMA(&AdcHandle,(uint32_t*)&uhADCxConvertedValue, 1) != HAL_OK) { /* 开始转化出错 */ Error_Handler(); } /*主循环*/ while (1) { } }
4.2 配置时钟 /* 配置系统时钟 100 MHz,STM32F412ZG可不支持超频啊 */ //SYSCLK(Hz) = 100000000 //HCLK(Hz) = 100000000 static void SystemClock_Config(void) { RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; HAL_StatusTypeDef ret = HAL_OK;
__HAL_RCC_PWR_CLK_ENABLE();
/*The voltage scaling allows optimizing the power consumption when the device is clocked below the maximum system frequency, to update the voltagescaling value regarding system frequency refer to product datasheet. */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/*Enable HSE Oscillator and activate PLL with HSE as source */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 8; RCC_OscInitStruct.PLL.PLLN = 200; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 7; RCC_OscInitStruct.PLL.PLLR = 2; ret= HAL_RCC_OscConfig(&RCC_OscInitStruct);
if(ret != HAL_OK) { while(1) { ; } }
/*Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; ret= HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3); if(ret != HAL_OK) { while(1) { ; } } }
|