ADC引脚和PWM引脚引线短接,PWM频率1K,定时器触发12K
ADC通过定时器触发,接收完毕后通过DMA中断打印数据
仿真调试中发现数据不正常(每1024个数据都有), (数据如下)
939:0,
940:1,
941:1,
942:0,
943:0,
944:0,
945:4095,
946:4095,
947:4095,
948:4095,
949:4095,
950:4095,
951:0,
952:4095,
953:4095,
954:4095,
955:0,
956:0,
957:0,
958:0,
959:0,
960:1,
961:4095,
962:4095,
963:4095,
964:4095,
965:4095,
966:4095,
现在不知是哪了的问题,请大家帮忙看下 谢谢
外设配置代码如下
void DMA_ADC_1_Init(void) { DMA_InitTypeDef DMA_InitStructure = {0}; NVIC_InitTypeDef NVIC_InitStructure = {0}; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(DMA_ADC1_CHANNEL); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_1_ARR; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = DMA_ADC1_BufferSize ; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA_ADC1_CHANNEL, &DMA_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); DMA_ITConfig(DMA1_Channel1,DMA_IT_TC, ENABLE); //使能传输完成中断 DMA_Cmd(DMA_ADC1_CHANNEL, ENABLE); } void TIM4_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); TIM_TimeBaseStructure.TIM_Period = USER_TIM_Period; TIM_TimeBaseStructure.TIM_Prescaler = USER_TIM_Prescaler; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; TIM_OCInitStructure.TIM_Pulse = USER_TIM_Period/2000; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC4Init(TIM4,&TIM_OCInitStructure); TIM_Cmd(TIM4, ENABLE); } void ADC_Function_Init(void) { ADC_InitTypeDef ADC_InitStructure = {0}; GPIO_InitTypeDef GPIO_InitStructure = {0}; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_ADCCLKConfig(RCC_PCLK2_Div8);/*12M*/ GPIO_InitStructure.GPIO_Pin = ADC1_LOOP_1 | ADC1_LOOP | ADC1_Light | ADC1_AC_Light; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); ADC_DeInit(ADC1); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T4_CC4; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = ADC_1_NbrOfChannel; ADC_InitStructure.ADC_OutputBuffer = ADC_OutputBuffer_Disable;/*不用输出BUFF*/ ADC_InitStructure.ADC_Pga = ADC_Pga_1;/*不用放大增益*/ ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC1_IN1_LOOP,1, ADC_SampleTime_71Cycles5);//PWM ADC_Cmd(ADC1, ENABLE); ADC_BufferCmd(ADC1, DISABLE); //disable buffer ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1)); Calibrattion_Val = Get_CalibrationValue(ADC1); ADC_DMACmd(ADC1, ENABLE); ADC_ExternalTrigConvCmd(ADC1, ENABLE); } /*PWM*/ void TIM1_PWMOut_Init(void) { GPIO_InitTypeDef GPIO_InitStructure={0}; TIM_OCInitTypeDef TIM_OCInitStructure={0}; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0}; RCC_APB2PeriphClockCmd( ADVANCE_TIM_CH1N_GPIO_CLK | RCC_APB2Periph_TIM1, ENABLE ); GPIO_InitStructure.GPIO_Pin = ADVANCE_TIM_CH1N_PIN ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( ADVANCE_TIM_CH3N_PORT, &GPIO_InitStructure ); TIM_TimeBaseInitStructure.TIM_Period = TIM1_PWMOut_Period; TIM_TimeBaseInitStructure.TIM_Prescaler = TIM1_PWMOut_Prescaler; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = ADVANCE_TIM_CounterMode; TIM_TimeBaseInit( ADVANCE_TIM, &TIM_TimeBaseInitStructure); #if (PWM_MODE == PWM_MODE1) TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; #elif (PWM_MODE == PWM_MODE2) TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; #endif /*CH1N*/ TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = TIM1_CCR1_Value; TIM_OCInitStructure.TIM_OCNPolarity = TIM_CH1N_OCPolarity; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; TIM_OC1Init( ADVANCE_TIM, &TIM_OCInitStructure ); // 主输出使能,当使用的是通用定时器时,这句不需要 TIM_CtrlPWMOutputs(ADVANCE_TIM, ENABLE ); /*TIM_OCPreload_Disable 后 更改CCP值后 马上更新数据*/ TIM_OC1PreloadConfig( ADVANCE_TIM, TIM_OCPreload_Disable ); TIM_ARRPreloadConfig( ADVANCE_TIM, ENABLE ); } void DMA1_Channel1_IRQHandler(void) { uint32_t i; if(DMA_GetITStatus(DMA1_IT_TC1)!=RESET) { TIM_Cmd( TIM4, DISABLE ); for(i = 0;i
热门产品 :
CH32V317: 互联型青稞RISC-V MCU