目前在网上找了一些关于高级定时器输出PWM的例子,不过都牵扯到死区什么的,不是特别理解,调了半天没有输出波形。。。。
我只是想单纯的输出PWM,求各位大神提供源码,让我参考参考。。。。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
以下是TIM1 输出PWM(已测试通过的)
// TIM1_PWM_Init(40-1,84-1); //168M/84=2Mhz计数频率,重装值40,PWM频率 2M/40=50Mhz. PA8
[mw_shl_code=c,true]void TIM1_PWM_Init(u32 arr,u32 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1);
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //??????
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //?????
TIM_TimeBaseStructure.TIM_Prescaler = psc; //Timer clock = sysclock /(TIM_Prescaler+1) = 168M
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseStructure.TIM_Period = arr; //Period = (TIM counter clock / TIM output clock) - 1 = 20K
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = (arr+1)/2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
} [/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
---------------------------------
已上传。。。
大神,您好,您上传的TIM1输出PWM从哪里可以看到?
一周热门 更多>