分频写0 如何提高PWM频率 请教

2019-07-21 01:56发布

pwm部分的代码如下,分频为0,即72MHZ,pwm输出频率为20K,求问大家该怎么调整 [mw_shl_code=c,true]void TIM1_PWM_Init(void) { //????·??è????????IO???è?? GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //TIM14?±?????? RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE); //????PORTF?±?? GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM1); //GPIOE9???????¨?±?÷TIM1_CH1N GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1); //TIM1_CH1P GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_TIM1);//TIm1_CH2N GPIO_PinAFConfig(GPIOB,GPIO_PinSource0,GPIO_AF_TIM1);//TIM1_CH2P GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //GPIOA GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //???????? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //????100MHz GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //???ì???????? GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //???? GPIO_Init(GPIOA,&GPIO_InitStructure); //??????PF9 TIM_TimeBaseStructure.TIM_Prescaler=0; //?¨?±?÷·??? TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_CenterAligned1; //?ò?????????? TIM_TimeBaseStructure.TIM_Period=arr; //×?????×°???? TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x00; TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);//???????¨?±?÷14 //??????TIM1 Channel1 PWM???? TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //?????¨?±?÷????:TIM?????í???÷??????2 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±??????????? TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //???????? TIM_OCInitStructure.TIM_Pulse = CCR1_Val; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //????????:TIM????±????????? TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//???????? TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; TIM_OC1Init(TIM1, &TIM_OCInitStructure); //?ù??T???¨???????????????èTIM1 4OC1 TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //????TIM1??CCR1?????¤×°???????÷ TIM_ARRPreloadConfig(TIM1,ENABLE);//ARPE???? TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //?????????????? TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //?????????????????? TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; //???¨?è???????¨??±?1 TIM1_BDTRInitStructure.TIM_DeadTime =50;//?????±??1us TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //???????????? TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low; //??????????????????????????????GND?±??PWM???? TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //×??????????? TIM_BDTRConfig(TIM1, &TIM1_BDTRInitStructure); TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); TIM_ClearFlag(TIM1,TIM_IT_Update); NVIC_InitStructure.NVIC_IRQChannel=TIM1_UP_TIM10_IRQn; //?¨?±?÷3???? NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; //??????????0 NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00; //×???????0 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_Cmd(TIM1,ENABLE); //????TIM1 TIM_CtrlPWMOutputs(TIM1,ENABLE); } void TIM1_UP_TIM10_IRQHandler(void) { if(TIM_GetITStatus(TIM1,TIM_IT_Update)==SET) //???????? { TIM1->CCR1 = CCR1_Val*(TIM1->ARR)/1800; TIM1->CCR2 = CCR2_Val*(TIM1->ARR)/1800; //delay_ms(20); } TIM_ClearITPendingBit(TIM1,TIM_IT_Update); //????????±ê???? }[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
块垒
1楼-- · 2019-07-21 06:41
不要把
TIM_TimeBaseStructure.TIM_Period = arr; 
TIM_TimeBaseStructure.TIM_Prescaler =psc;//这句管分频 
封进去在留2个变量;方便以后改用;
块垒
2楼-- · 2019-07-21 10:25
TIM3_PWM_Init(7199,99);
一句话就行,这个是100hz的频率;有公式你自己仔细看程序

一周热门 更多>