总线时钟是64M,目前要实现ADC的采样频率为2.5kHz,代码中需要如何配置ADC和DMA的相关时钟?
目前配置代码如下:
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_PeriphCLKInitTypeDef PeriphClkInit;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
- RCC_OscInitStruct.MSIState = RCC_MSI_ON;
- RCC_OscInitStruct.MSICalibrationValue = 0;
- RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
- RCC_OscInitStruct.PLL.PLLM = 1;
- RCC_OscInitStruct.PLL.PLLN = 40;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
- RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
- RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|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_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_ADC;
- PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
- PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_PLLSAI1;
- PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_MSI;
- PeriphClkInit.PLLSAI1.PLLSAI1M = 1;
- PeriphClkInit.PLLSAI1.PLLSAI1N = 16;
- PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV7;
- PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2;
- PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2;
- PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_ADC1CLK;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
-
- /**Configure the main internal regulator output voltage
- */
- if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
复制代码- /* ADC1 init function */
- void MX_ADC1_Init(void)
- {
- ADC_MultiModeTypeDef multimode;
- ADC_ChannelConfTypeDef sConfig;
- /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
- */
- hadc1.Instance = ADC1;
- hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV8;
- hadc1.Init.Resolution = ADC_RESOLUTION_12B;
- hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
- hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
- hadc1.Init.LowPowerAutoWait = DISABLE;
- hadc1.Init.ContinuousConvMode = ENABLE;
- hadc1.Init.NbrOfConversion = 4;
- hadc1.Init.DiscontinuousConvMode = DISABLE;
- hadc1.Init.NbrOfDiscConversion = 1;
- hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
- hadc1.Init.DMAContinuousRequests = DISABLE;
- hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
- hadc1.Init.OversamplingMode = DISABLE;
- if (HAL_ADC_Init(&hadc1) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure the ADC multi-mode
- */
- multimode.Mode = ADC_MODE_INDEPENDENT;
- if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
- {
- Error_Handler();
- }
- sConfig.Channel = ADC_CHANNEL_5;
- sConfig.Rank = ADC_REGULAR_RANK_1;
- sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5; //47.5 个ADC 时钟
- sConfig.SingleDiff = ADC_SINGLE_ENDED; /* Single-ended input channel */
- sConfig.OffsetNumber = ADC_OFFSET_NONE; /* No offset subtraction */
- sConfig.Offset = 0; /* Parameter discarded because offset correction is disabled */
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_6;
- sConfig.Rank = ADC_REGULAR_RANK_2;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_3;
- sConfig.Rank = ADC_REGULAR_RANK_3;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- /**Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_4;
- sConfig.Rank = ADC_REGULAR_RANK_4;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
- {
- Error_Handler();
- }
- }
- void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- // static DMA_HandleTypeDef hdma_adc1;
-
- if(hadc->Instance==ADC1)
- {
- /* USER CODE BEGIN ADC1_MspInit 0 */
- /* USER CODE END ADC1_MspInit 0 */
- /* ADC1 clock enable */
- __HAL_RCC_ADC_CLK_ENABLE();
-
- /**ADC1 GPIO Configuration
- PC2 ------> ADC1_IN3
- PC3 ------> ADC1_IN4
- PA0 ------> ADC1_IN5
- PA1 ------> ADC1_IN6
- */
- GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
- GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
- GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- // GPIO_InitStruct.Pin = GPIO_PIN_1;
- // GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
- // GPIO_InitStruct.Pull = GPIO_NOPULL;
- // HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* Peripheral DMA init*/
-
- hdma_adc1.Instance = DMA1_Channel1;
- hdma_adc1.Init.Request = DMA_REQUEST_0;
- hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
- hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
- hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
- hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
- hdma_adc1.Init.Mode = DMA_CIRCULAR;
- hdma_adc1.Init.Priority = DMA_PRIORITY_MEDIUM;
- if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(hadc,DMA_Handle,hdma_adc1);
- /* USER CODE BEGIN ADC1_MspInit 1 */
- /* USER CODE END ADC1_MspInit 1 */
- }
- }
- void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
- {
- if(hadc->Instance==ADC1)
- {
- /* USER CODE BEGIN ADC1_MspDeInit 0 */
- /* USER CODE END ADC1_MspDeInit 0 */
- /* Peripheral clock disable */
- __HAL_RCC_ADC_CLK_DISABLE();
-
- /**ADC1 GPIO Configuration
- PC2 ------> ADC1_IN3
- PC3 ------> ADC1_IN4
- PA0 ------> ADC1_IN5
- PA1 ------> ADC1_IN6
- */
- HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1);
- HAL_GPIO_DeInit(GPIOC, GPIO_PIN_2|GPIO_PIN_3);
- // HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1);
- /* Peripheral DMA DeInit*/
- HAL_DMA_DeInit(hadc->DMA_Handle);
- }
- /* USER CODE BEGIN ADC1_MspDeInit 1 */
- /* USER CODE END ADC1_MspDeInit 1 */
- }
复制代码- void MX_DMA_Init(void)
- {
- /* DMA controller clock enable */
- __HAL_RCC_DMA1_CLK_ENABLE();
- /* DMA interrupt init */
- /* DMA1_Channel1_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
- }
复制代码 目前都好模糊,也找不到对应的数据手册,求大神帮忙解决~
|
评分
查看全部评分
评分
查看全部评分
按照你程序配出来的ADC时钟是32M,不是64M。系统时钟是80M。
我改了下PLLSAI1的N值,ADC时钟才成64M的。
你要的ADC采样为2.5KHz,是4个通道循环为一周期算的频率呢,还是就指ADC本身的采样频率。
如果是本身ADC采样频率的话,取决于ADC主时钟和采样时间设置。
比如,你要设置2.5KHz采样的话,采样时间假如设置为最大的640.5 cycles,那么主时钟频率就是2.5K*640.5=1601250=1.60125MHz。ADC主时钟能不能设置这么小还两说。
评分
查看全部评分
时钟问题已解决,谢谢~
另外,问一下:
在做ADC-DMA转电压值时,输出值存储在uhADCxConvertedValue中,长度为256,但是打印出来的值每个通道的前128个有数值,但是后128个数据都为0,很奇怪,不知道为什么?
#define NB 256 //每个通道长度为256
#define CHN 2 //两个通道
#define ADCNB NB*CHN
u16 uhADCxConvertedValue[NB][CHN];
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&uhADCxConvertedValue, ADCNB);
for(int a=0;a<NB;a++){
printf("uhADCxConvertedValue[%d][0]= %d\r\n",a,uhADCxConvertedValue[a][0]); //通道1的数据
printf("uhADCxConvertedValue[%d][1]= %d\r\n",a,uhADCxConvertedValue[a][1]); //通道2的数据
}
评分
查看全部评分
没看出来,你什么时候读数据的?
从程序看,DMA会进两次中断,第一次是一半传输完,第二次是全部传输完
dmaflage在DMA中断的时候进行赋值1的。
我这还有个疑问,你说的这句话“从程序看,DMA会进两次中断,第一次是一半传输完,第二次是全部传输完”怎么理解,我现在遇到一个问题就是中断给dmaflage赋值1后,我就在while进行读值,每次只读到uhADCxConvertedValue的前128个数据有值,后128个数据都是0;是不是和你说的这句话有关,麻烦帮忙解释下“DMA会进两次中断,第一次是一半传输完,第二次是全部传输完”?我需要怎么操作才能读完整这256个数据呢
1,如楼上朋友的方法,通过中断标志来读取
2,通过外部中断TIM3触发采样,TIM3设置为300HZ定时器
但是遇到的问题是,方式一,CLOCK如何配置?
问题二,调试时
__IO uint16_t uhADCxConvertedValue[1000];
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)uhADCxConvertedValue,10);
启动DMA后,进不了中断。
但是,修改为HAL_ADC_Start_DMA(&hadc1, (uint32_t*)uhADCxConvertedValue,4); // 测试1,2,3,4都能进中断。请各位老师帮忙指点一下是什么原因大于4就不能进中断呢?
我的配置和源代码如下:
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();
/* 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_DMA_Init();
MX_ADC1_Init();
MX_USART1_UART_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)uhADCxConvertedValue,4);
printf("start\r");
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
HAL_Delay(1000);
printf("1scc\r");
/* USER CODE BEGIN 3 */
//check_ecg_dma_data();
}
/* USER CODE END 3 */
}
打的DEBUG LOG位置:
void DMA1_Channel1_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel1_IRQn 0 */
printf("dma ir\r");
/* USER CODE END DMA1_Channel1_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_adc1);
/* USER CODE BEGIN DMA1_Channel1_IRQn 1 */
/* USER CODE END DMA1_Channel1_IRQn 1 */
}
__weak void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hadc);
printf("cplt \r");
/* NOTE : This function should not be modified. When the callback is needed,
function HAL_ADC_ConvCpltCallback must be implemented in the user file.
*/
}
启动后的打印log:
dma ir
把参数修改为10后: