修改了pwm.c的bug

main
cgy 2026-02-11 15:27:42 +08:00
parent 3b3614bd31
commit 488b80690c
1 changed files with 84 additions and 83 deletions

View File

@ -32,7 +32,7 @@ void TIM1_PWM_Init(u16 arr,u16 psc)
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE,&GPIO_InitStructure); GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_ResetBits(GPIOE, GPIO_Pin_9); // GPIO_ResetBits(GPIOE, GPIO_Pin_9);
TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值 TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频 TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频
@ -43,8 +43,8 @@ void TIM1_PWM_Init(u16 arr,u16 psc)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 100; TIM_OCInitStructure.TIM_Pulse = arr/2;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_OC1Init(TIM1, &TIM_OCInitStructure);
@ -52,8 +52,9 @@ void TIM1_PWM_Init(u16 arr,u16 psc)
TIM_ARRPreloadConfig(TIM1,ENABLE); TIM_ARRPreloadConfig(TIM1,ENABLE);
//TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
} }
//定时器3中断服务函数 //定时器3中断服务函数
@ -95,73 +96,73 @@ extern u16 real_dis_data[6];
extern u16 real_dis_data_bak[6]; extern u16 real_dis_data_bak[6];
extern u8 double_buffer_flag; extern u8 double_buffer_flag;
//定时器3中断服务函数 //定时器2中断服务函数
//void TIM2_IRQHandler(void) void TIM2_IRQHandler_bak(void)
//{ {
// char vcm_cmd[16]; char vcm_cmd[16];
// if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET) //溢出中断 if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET) //溢出中断
// { {
//#if 1 #if 1
// if(led_flag == 0) if(led_flag == 0)
// { {
// GPIO_SetBits(GPIOE, GPIO_Pin_0); GPIO_SetBits(GPIOE, GPIO_Pin_0);
// led_flag = 1; led_flag = 1;
// TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位 TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位
// return; return;
// } }
// if(led_flag == 1) if(led_flag == 1)
// { {
// GPIO_ResetBits(GPIOE, GPIO_Pin_0); GPIO_ResetBits(GPIOE, GPIO_Pin_0);
// led_flag = 0; led_flag = 0;
// TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位 TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位
// return; return;
// } }
//#endif #endif
//#if 0 #if 0
// sprintf(vcm_cmd,VCM_DISPLACE_CMD,real_dis_data[index]); sprintf(vcm_cmd,VCM_DISPLACE_CMD,real_dis_data[index]);
// usart_send(USART3, vcm_cmd); usart_send(USART3, vcm_cmd);
// usart_send(USART3, START_MACRO_NUM); usart_send(USART3, START_MACRO_NUM);
// TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位 TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位
// return; return;
//
//#endif #endif
//#if 0 #if 0
// if(led_flag == 0) if(led_flag == 0)
// { {
// sprintf(vcm_cmd,VCM_DISPLACE_CMD,real_dis_data[index]); sprintf(vcm_cmd,VCM_DISPLACE_CMD,real_dis_data[index]);
// usart_send(USART3, vcm_cmd); usart_send(USART3, vcm_cmd);
// usart_send(USART3, START_MACRO_NUM); usart_send(USART3, START_MACRO_NUM);
// index++; index++;
// TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位 TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位
// return; return;
// } }
// if(led_flag == 1) if(led_flag == 1)
// { {
// sprintf(vcm_cmd,VCM_DISPLACE_CMD,real_dis_data_bak[index]); sprintf(vcm_cmd,VCM_DISPLACE_CMD,real_dis_data_bak[index]);
// usart_send(USART3, vcm_cmd); usart_send(USART3, vcm_cmd);
// usart_send(USART3, START_MACRO_NUM); usart_send(USART3, START_MACRO_NUM);
// TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位 TIM_ClearITPendingBit(TIM2,TIM_IT_Update); //清除中断标志位
// return; return;
// } }
// if(index > 199 && led_flag == 0) if(index > 199 && led_flag == 0)
// { {
// index = 0; index = 0;
// led_flag = 1; led_flag = 1;
// double_buffer_flag = 0; double_buffer_flag = 0;
// usart_send(USART1, "laishuju"); usart_send(USART1, "laishuju");
// } }
// if(index > 199 && led_flag == 1) if(index > 199 && led_flag == 1)
// { {
// index = 0; index = 0;
// led_flag = 0; led_flag = 0;
// double_buffer_flag = 1; double_buffer_flag = 1;
// usart_send(USART1, "laishuju"); usart_send(USART1, "laishuju");
// } }
//#endif #endif
// } }
//
//} }
void TIM2_PWM_Init(u16 arr,u16 psc) void TIM2_PWM_Init(u16 arr,u16 psc)
@ -174,7 +175,7 @@ void TIM2_PWM_Init(u16 arr,u16 psc)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); ///使能TIM1时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); ///使能TIM1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_TIM1); GPIO_PinAFConfig(GPIOB,GPIO_PinSource3,GPIO_AF_TIM2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
@ -183,7 +184,7 @@ void TIM2_PWM_Init(u16 arr,u16 psc)
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_ResetBits(GPIOB, GPIO_Pin_9); // GPIO_ResetBits(GPIOB, GPIO_Pin_3);
TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值 TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频 TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频
@ -195,15 +196,15 @@ void TIM2_PWM_Init(u16 arr,u16 psc)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_Pulse = 100; TIM_OCInitStructure.TIM_Pulse = arr/2;
TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2,ENABLE); TIM_ARRPreloadConfig(TIM2,ENABLE);
//TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM2, ENABLE);
} }
@ -273,7 +274,7 @@ void TIM4_PWM_Init(u16 arr,u16 psc)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); ///使能TIM3时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); ///使能TIM3时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource15,GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOD,GPIO_PinSource15,GPIO_AF_TIM4);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
@ -282,7 +283,7 @@ void TIM4_PWM_Init(u16 arr,u16 psc)
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD,&GPIO_InitStructure); GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_ResetBits(GPIOD, GPIO_Pin_15); // GPIO_ResetBits(GPIOD, GPIO_Pin_15);
TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值 TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频 TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频
@ -294,15 +295,15 @@ void TIM4_PWM_Init(u16 arr,u16 psc)
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_Pulse = 100; TIM_OCInitStructure.TIM_Pulse = arr/2;
TIM_OC1Init(TIM4, &TIM_OCInitStructure); TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4,ENABLE); TIM_ARRPreloadConfig(TIM4,ENABLE);
//TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM4, ENABLE);
} }