为什么我用pwm采集dac的电压出来以后,采集出来反而是pwm的占空比呢
代码如下:
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart1_init(115200);
uart3_init(115200);
GPIO_Init_Pins();
PID_left_init();
PID_right_init();
Adc_Init();
Dac1_Init();
TIM5_PWM_Init(10000-1,84-1);
TIM3_CH1_Cap_Init(0XFFFF,84-1);
DAC_SetChannel1Data(DAC_Align_12b_R,left.speed_v);
//DAC_SetChannel1Data(DAC_Align_12b_R,right.speed_v);
while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_8);
GPIO_SetBits(GPIOB,GPIO_Pin_9);
GPIO_SetBits(GPIOB,GPIO_Pin_7);
GPIO_SetBits(GPIOB,GPIO_Pin_6);
left.speed_v = CM_pid_left(100);
//right.speed_v= CM_pid_right(100);
TIM_SetCompare1(TIM5, left.speed_v);
TIM_SetCompare2(TIM5, right.speed_v);
//DAC_SetChannel1Data(DAC_Align_12b_R,left.speed_v);
adcx=TIM_GetCapture1(TIM5);//¼ì²â×óÂֵĻô¶û´«¸ÐÆ÷µçѹֵ
tempADCx=(float)adcx*(3.3/4096);
adcx=tempADCx;
tempADCx-=adcx;
tempADCx*=1000;
DAC_SetChannel1Data(DAC_Align_12b_R,left.speed_v);
//DAC_SetChannel1Data(DAC_Align_12b_R,right.speed_v);
// adcy=DAC_GetDataOutputValue(DAC_Channel_2); //¼ì²âÓÒÂֵĻô¶û´«¸ÐÆ÷µÄµçѹֵ
// tempADCy=(float)adcy*(3.3/4096);
// adcy=tempADCy;
// tempADCy-=adcy;
// tempADCy*=1000;
if(TIM3CH1_CAPTURE_STA&0X80) //³É¹¦²¶»ñµ½ÁËÒ»´Î¸ßµçƽ
{
templeft=TIM3CH1_CAPTURE_STA&0X3F;
templeft*=0XFFFF; //Òç³öʱ¼ä×ܺÍ
templeft+=TIM3CH1_CAPTURE_VAL; //µÃµ½×ܵĸߵçƽʱ¼ä
TIM3CH1_CAPTURE_STA=0; //¿ªÆôÏÂÒ»´Î²¶»ñ
}
if(TIM3CH2_CAPTURE_STA&0X80) //³É¹¦²¶»ñµ½ÁËÒ»´Î¸ßµçƽ
{
tempright=TIM3CH2_CAPTURE_STA&0X3F;
tempright*=0XFFFF; //Òç³öʱ¼ä×ܺÍ
tempright+=TIM3CH2_CAPTURE_VAL; //µÃµ½×ܵĸߵçƽʱ¼ä
TIM3CH2_CAPTURE_STA=0; //¿ªÆôÏÂÒ»´Î²¶»ñ
}
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);
uart1_init(115200);
uart3_init(115200);
GPIO_Init_Pins();
PID_left_init();
PID_right_init();
Adc_Init();
Dac1_Init();
TIM5_PWM_Init(10000-1,84-1);
TIM3_CH1_Cap_Init(0XFFFF,84-1);
DAC_SetChannel1Data(DAC_Align_12b_R,left.speed_v);
//DAC_SetChannel1Data(DAC_Align_12b_R,right.speed_v);
while(1)
{
GPIO_SetBits(GPIOB,GPIO_Pin_8);
GPIO_SetBits(GPIOB,GPIO_Pin_9);
GPIO_SetBits(GPIOB,GPIO_Pin_7);
GPIO_SetBits(GPIOB,GPIO_Pin_6);
left.speed_v = CM_pid_left(100);
//right.speed_v= CM_pid_right(100);
TIM_SetCompare1(TIM5, left.speed_v);
TIM_SetCompare2(TIM5, right.speed_v);
//DAC_SetChannel1Data(DAC_Align_12b_R,left.speed_v);
adcx=TIM_GetCapture1(TIM5);//¼ì²â×óÂֵĻô¶û´«¸ÐÆ÷µçѹֵ
tempADCx=(float)adcx*(3.3/4096);
adcx=tempADCx;
tempADCx-=adcx;
tempADCx*=1000;
DAC_SetChannel1Data(DAC_Align_12b_R,left.speed_v);
//DAC_SetChannel1Data(DAC_Align_12b_R,right.speed_v);
// adcy=DAC_GetDataOutputValue(DAC_Channel_2); //¼ì²âÓÒÂֵĻô¶û´«¸ÐÆ÷µÄµçѹֵ
// tempADCy=(float)adcy*(3.3/4096);
// adcy=tempADCy;
// tempADCy-=adcy;
// tempADCy*=1000;
if(TIM3CH1_CAPTURE_STA&0X80) //³É¹¦²¶»ñµ½ÁËÒ»´Î¸ßµçƽ
{
templeft=TIM3CH1_CAPTURE_STA&0X3F;
templeft*=0XFFFF; //Òç³öʱ¼ä×ܺÍ
templeft+=TIM3CH1_CAPTURE_VAL; //µÃµ½×ܵĸߵçƽʱ¼ä
TIM3CH1_CAPTURE_STA=0; //¿ªÆôÏÂÒ»´Î²¶»ñ
}
if(TIM3CH2_CAPTURE_STA&0X80) //³É¹¦²¶»ñµ½ÁËÒ»´Î¸ßµçƽ
{
tempright=TIM3CH2_CAPTURE_STA&0X3F;
tempright*=0XFFFF; //Òç³öʱ¼ä×ܺÍ
tempright+=TIM3CH2_CAPTURE_VAL; //µÃµ½×ܵĸߵçƽʱ¼ä
TIM3CH2_CAPTURE_STA=0; //¿ªÆôÏÂÒ»´Î²¶»ñ
}
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>