热度 8
2020-12-25 14:56
6172 次阅读|
2 个评论
使用DMA 进行AD采样很方便,但需要事先设置好通道。如果有过程中希望修改通道,可以先做一个DeInit 然后设置不同的Rank // Adc 通道配置 选择 vdc vbat preu pred void ADC1_misc_Init(void) { ADC_ChannelConfTypeDef sConfig; HAL_ADC_DeInit(&hadc1); /**Common config */ hadc1.Instance = ADC1; hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; hadc1.Init.ContinuousConvMode = ENABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 4; if (HAL_ADC_Init(&hadc1) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_12; // bdc sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_13; // vbat sConfig.Rank = ADC_REGULAR_RANK_2; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_1; // preu sConfig.Rank = ADC_REGULAR_RANK_3; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_7; // pred sConfig.Rank = ADC_REGULAR_RANK_4; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } } // adc1 通道配置 bub pos void ADC1_ir_Init(void) { ADC_ChannelConfTypeDef sConfig; HAL_ADC_DeInit(&hadc1); /**Common config */ hadc1.Instance = ADC1; hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE; hadc1.Init.ContinuousConvMode = ENABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 2; if (HAL_ADC_Init(&hadc1) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_5; // position sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } /**Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_6; // bubble sConfig.Rank = ADC_REGULAR_RANK_2; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } } 在启动转换时,依据设置的通道数,选择不同的DMA空间 void StartADC1Misc(void){ HAL_ADC_Start_DMA(&hadc1, ADC1Buf.ValMem,ADCCNT*4); } void StartADC1IR(void){ HAL_ADC_Start_DMA(&hadc1, IRBuf.ValMem,ADCCNT*2); } 获取采样值的时候,找到对应的缓冲区 uint32_t AdcGetValue(uint8_t ch){ uint32_t temp,i; temp = 0; for(i = ADCCNT/2-5;i< ADCCNT/2+5;i++){ temp = temp+ (ADC1Buf.Val &0xFFF); } temp /= 10; return temp; } uint32_t AdcGetIRValue(uint8_t ch){ uint32_t temp,i; temp = 0; for(i = ADCCNT/2-5;i< ADCCNT/2+5;i++){ temp = temp+ (IRBuf.Val &0xFFF); } temp /= 10; return temp; }