输入捕获测方波信号的上升沿,输入捕获脚测到第一个上升沿的时候AD开始采集,测到第二个上升沿的时候AD停止采集,AD的采样频率为200KHz,AD的输入的正弦信号的频率为610Hz,理论上一个周期可以采集327个点,但是现在每一周期(输入捕获的两个上升沿之间)采集的点数都不够定,例如90多,100多,200多。程序如下!不知道是什么原因用的是Mini开发板,之后自己搭的电路
不知道是自己的逻辑有问题,还是输入捕获的问题,请大家帮忙
这是主函数
int main(void)
{
u16 AData1,AD_temp,AData,AD_temp1,AD_temp2;
u16 AData2;
u16 led0pwmval=0,i=0;
uint16_t AD_Buf[ADSampNumMax]={0};
u16 Demo[ADSampNumMax]={0};
u16 a,m=0,k=0,ii=0;
u16 AD_SUM,AD_SUM1=0;
u16 ADSampNowNum=0;
float v;
//unsigned int ADbuffer[8];
// EXTIX_Init();
delay_init(); //
TIM2_Cap_Init(0xffff,2-1);
TIM1_PWM_Init(359,0); //
uart_init(9600); //
AD_Init();
delay_ms(10);
led0pwmval=288; //DUTY 20%
if(TIM1CH1_CAPTURE_FLAG1) //这里是判断是检测到一个上升沿开始AD读数,又检测到一个上升沿停止读数的一个逻辑
TIM1CH1_CAPTURE_FLAG1=0;
while(!TIM1CH1_CAPTURE_FLAG1)
{
while(!BUSY);
AData1=GPIO_ReadInputData(GPIOB)&0xFFFF;
AD_Buf[ADSampNowNum]=AData1;
ADSampNowNum++;
while(BUSY);
}
a++;
for(ii=0;ii<327;ii++)
{
AData2=AD_Buf[ii];
printf("%d,
",AData2 );
}
printf("%d,
",ADSampNowNum );
}
输入捕获中断服务函数
void TIM2_IRQHandler(void)
{
TIM1CH1_CAPTURE_FLAG1=0;
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) //
{
ICP4_T1 = TIM_GetCapture1(TIM2); //
TIM1CH1_CAPTURE_STA |= 0X40; //
TIM_OC1PolarityConfig(TIM2, TIM_ICPolarity_Rising);//
TIM1CH1_CAPTURE_FLAG1=1; //检测到上升沿将这个变量赋值
}
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1|TIM_IT_Update); //
}
TIM_ICInitTypeDef TIM2_ICInitStructure;
void TIM2_Cap_Init(u16 arr, u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //ê1ÄüTIM4ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //ê1ÄüGPIOBê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PB8 Çå3y֮ǰéèÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PB8
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA, GPIO_Pin_0 );//PB7 ÏÂà-
//3õê¼»ˉ¶¨ê±Æ÷4 TIM4
TIM_TimeBaseStructure.TIM_Period = arr; //é趨¼ÆêyÆ÷×Ô¶ˉÖØ×°Öμ
TIM_TimeBaseStructure.TIM_Prescaler = psc; //Ô¤·ÖÆμÆ÷
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
//3õê¼»ˉTIM4êäèë2¶»ñ2Îêy í¨μà1
TIM2_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 Ñ¡Ôñêäèë¶Ë IC1ó3éäμ½TI1éÏ
TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //éÏéyÑØ2¶»ñ
TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //ó3éäμ½TI1éÏ
TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //ÅäÖÃêäèë·ÖÆμ,2»·ÖÆμ
TIM2_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000 ÅäÖÃêäèëÂË2¨Æ÷ 2»ÂË2¨
TIM_ICInit(TIM2, &TIM2_ICInitStructure);
//ÖD¶Ï·Ö×é3õê¼»ˉ
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM4ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //ÏèÕ¼óÅÏ輶2¼¶
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //′óóÅÏ輶0¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
TIM_ITConfig(TIM2, TIM_IT_CC1,ENABLE); //2»ÔêDí¸üDÂÖD¶Ï£¬ÔêDíCC1IE2¶»ñÖD¶Ï
TIM_Cmd(TIM2, ENABLE); //ê1Äü¶¨ê±Æ÷4
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>