[N32G45x] 适用于嵌入式ADC RC低通滤波

[复制链接]
 楼主| zerorobert 发表于 2025-5-24 20:22 | 显示全部楼层 |阅读模式



  1. #define                ADC_SMPILE_NUM                4
  2. #define                ADC_CHANNEL_NUM                6
  3. uint16_t adc1Value[ADC_SMPILE_NUM][ADC_CHANNEL_NUM];

  4. // Filtered measurement variables
  5. uint32_t  adcFilterSum[ADC_SMPILE_NUM];
  6. uint16_t  adcFilteredAdcVal[ADC_SMPILE_NUM];

  7. uint16_t _ADC_GetSingleReading(uint8_t adcChannel)
  8. {
  9.         uint32_t adcval =adc1Value[0][adcChannel]+
  10.                                         adc1Value[1][adcChannel]+
  11.                                         adc1Value[2][adcChannel]+
  12.                                         adc1Value[3][adcChannel];
  13.         return         adcval>>2;
  14. }

  15. void hdl_adcInit(void){

  16.         GPIO_InitTypeDef GPIO_InitStruct;
  17.         RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);
  18.         RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, ENABLE);

  19.         GPIO_StructInit(&GPIO_InitStruct);
  20.         GPIO_InitStruct.GPIO_Pin  =  GPIO_Pin_1|GPIO_Pin_4/*|GPIO_Pin_5*/|GPIO_Pin_6|GPIO_Pin_7;
  21.         GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  22.         GPIO_InitStruct.GPIO_Mode =GPIO_Mode_AIN;
  23.         GPIO_Init(GPIOA, &GPIO_InitStruct);
  24.         GPIO_InitStruct.GPIO_Pin  =  GPIO_Pin_0|GPIO_Pin_1;
  25.         GPIO_Init(GPIOB, &GPIO_InitStruct);
  26.        
  27.         DMA_InitTypeDef DMA_InitStruct;
  28.         RCC_AHBPeriphClockCmd(RCC_AHBENR_DMA1, ENABLE);
  29.         DMA_DeInit(DMA1_Channel1);
  30.         DMA_StructInit(&DMA_InitStruct);
  31.         //DMA transfer peripheral address
  32.         DMA_InitStruct.DMA_PeripheralBaseAddr = (u32) & (ADC1->DR);
  33.         //DMA transfer memory address
  34.         DMA_InitStruct.DMA_MemoryBaseAddr = (u32)adc1Value;
  35.         //DMA transfer direction from peripheral to memory
  36.         DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
  37.         //DMA cache size
  38.         DMA_InitStruct.DMA_BufferSize = ADC_SMPILE_NUM*ADC_CHANNEL_NUM;
  39.         //After receiving the data, the peripheral address is forbidden to move backward
  40.         DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  41.         //After receiving the data, the memory address is shifted backward
  42.         DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
  43.         //Define the peripheral data width to 16 bits
  44.         DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  45.         //Define the memory data width to 16 bits
  46.         DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  47.         //Cycle conversion mode
  48.         DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
  49.         //DMA priority is high
  50.         DMA_InitStruct.DMA_Priority = DMA_Priority_High;
  51.         //M2M mode is disabled
  52.         DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
  53.         DMA_InitStruct.DMA_Auto_reload = DMA_Auto_Reload_Disable;
  54.         DMA_Init(DMA1_Channel1, &DMA_InitStruct);
  55.         DMA_Cmd(DMA1_Channel1, ENABLE);
  56.                
  57.         ADC_DeInit(ADC1);
  58.         ADC_InitTypeDef  ADC_InitStruct;
  59.         ADC_StructInit(&ADC_InitStruct);
  60.         //Enable ADC clock
  61.         RCC_APB2PeriphClockCmd(RCC_APB2ENR_ADC1, ENABLE);
  62.         ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
  63.         //ADC prescale factor
  64.         ADC_InitStruct.ADC_PRESCARE = ADC_PCLK2_PRESCARE_2;
  65.         //Set ADC mode to continuous conversion mode
  66.         ADC_InitStruct.ADC_Mode = ADC_Mode_Continue;
  67.         //AD data right-justified
  68.         ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
  69.     ADC_InitStruct.ADC_ExternalTrigConv = ADC1_ExternalTrigConv_T2_TRIG;
  70.         ADC_Init(ADC1, &ADC_InitStruct);

  71.         //Enable the channel
  72.         ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 0, ADC_Samctl_8_5);//1
  73.         ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 0, ADC_Samctl_8_5);//2
  74.         ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 0, ADC_Samctl_8_5);//3
  75.         ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 0, ADC_Samctl_8_5);//4
  76.         ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 0, ADC_Samctl_8_5);//5
  77.         ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 0, ADC_Samctl_8_5);//6
  78.   //Enable ADCDMA
  79.         ADC_DMACmd(ADC1, ENABLE);
  80.         //Enable AD conversion
  81.         ADC_Cmd(ADC1, ENABLE);
  82.         ADC_ExternalTrigConvConfig(ADC1,ADC1_ExternalTrigConv_T1_CC1);
  83.         ADC1->CR |=(1<<24)|(3<<19);//下降沿触发 ,512 个延时周期
  84.         ADC_ExternalTrigConvCmd(ADC1, ENABLE);
  85. }

  86. // adc samp
  87. void TIM3_init(void){
  88.    RCC_APB1PeriphClockCmd(RCC_APB1ENR_TIM3, ENABLE);
  89.         uint32_t arr =72000000L /100 -1;//100hz adc采样率
  90.         TIM3->CR1 =(1<<7)|(1<<2);
  91.         TIM3->PSC =0;
  92.         TIM3->ARR =arr;
  93.         TIM3->DIER =(1<<0);
  94.                
  95.         NVIC_InitTypeDef NVIC_InitStruct;
  96.     NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
  97.     NVIC_InitStruct.NVIC_IRQChannelPriority = 2;
  98.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  99.     NVIC_Init(&NVIC_InitStruct);
  100.         TIM3->CR1|=(1<<0);//enable tim1
  101. }

  102. void ADCs_Init(void)
  103. {
  104.         hdl_adcInit();
  105.         delay_ms(5000);
  106.         // Setup the filtered ADC value filters
  107.         adcFilteredAdcVal[ADC_MEAS_VS_INDEX] = _ADC_GetSingleReading(_ADC_VS_CH);
  108.         adcFilterSum[ADC_MEAS_VS_INDEX] = (adcFilteredAdcVal[ADC_MEAS_VS_INDEX] << ADC_V_FILTER_SHIFT);
  109.         adcFilteredAdcVal[ADC_MEAS_IS_INDEX] = _ADC_GetSingleReading(_ADC_IS_CH);
  110.         adcFilterSum[ADC_MEAS_IS_INDEX] = (adcFilteredAdcVal[ADC_MEAS_IS_INDEX] << ADC_I_FILTER_SHIFT);
  111.         adcFilteredAdcVal[ADC_MEAS_VB_INDEX] = _ADC_GetSingleReading(_ADC_VB_CH);
  112.         adcFilterSum[ADC_MEAS_VB_INDEX] = (adcFilteredAdcVal[ADC_MEAS_VB_INDEX] << ADC_V_FILTER_SHIFT);
  113.         adcFilteredAdcVal[ADC_MEAS_IB_INDEX] = _ADC_GetSingleReading(_ADC_IB_CH);
  114.         adcFilterSum[ADC_MEAS_IB_INDEX] = (adcFilteredAdcVal[ADC_MEAS_IB_INDEX] << ADC_I_FILTER_SHIFT);
  115.         TIM3_init();
  116. }

  1. // Measurement indices (first 4 must be measured ADC values)
  2. #define ADC_NUM_MEASUREMENTS 6

  3. #define _ADC_VS_CH 4
  4. #define _ADC_IS_CH 2
  5. #define _ADC_VB_CH 3
  6. #define _ADC_IB_CH 1
  7. #define _ADC_TE_CH 6
  8. #define _ADC_TI_CH 5

  9. #define ADC_MEAS_VS_INDEX    (_ADC_VS_CH-1)//0
  10. #define ADC_MEAS_IS_INDEX    (_ADC_IS_CH-1)//1
  11. #define ADC_MEAS_VB_INDEX    (_ADC_VB_CH-1)//2
  12. #define ADC_MEAS_IB_INDEX    (_ADC_IB_CH-1)//3
  13. #define ADC_MEAS_TI_INDEX    (_ADC_TI_CH-1)//4
  14. #define ADC_MEAS_TE_INDEX    (_ADC_TE_CH-1)//5
  15. //
  16. // Low-pass digital filter parameters
  17. //  From: https://www.edn.com/design/systems-design/4320010/A-simple-software-lowpass-filter-suits-embedded-system-applications
  18. //
  19. //  K        Bandwidth (normalized to 1Hz)     Rise Time (Samples)
  20. //  1        0.1197                            3
  21. //  2        0.0466                            8
  22. //  3        0.0217                            16
  23. //  4        0.0104                            34
  24. //  5        0.0051                            69
  25. //  6        0.0026                            140
  26. //  7        0.0012                            280
  27. //  8        0.0007                            561
  28. //
  29. #define ADC_V_FILTER_SHIFT  3
  30. #define ADC_I_FILTER_SHIFT  6

  31. // ADC Interrupt control macros
  32. #define _ADC_DIS_INT() TIM3->CR1&=~(1<<0)
  33. #define _ADC_EN_INT()  TIM3->CR1|=(1<<0)


szt1993 发表于 2025-7-31 23:02 | 显示全部楼层
适用于嵌入式ADC RC低通滤波
您需要登录后才可以回帖 登录 | 注册

本版积分规则

49

主题

1883

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部