TIM1的pwm输出没有波形?

2019-07-21 02:26发布

我感觉是库函数的问题,因为库函数没有要求给他TIM3,参数只有数据 [mw_shl_code=c,true]void TIM_BDTRStructInit(TIM_BDTRInitTypeDef* TIM_BDTRInitStruct) { /* Set the default configuration */ TIM_BDTRInitStruct->TIM_OSSRState = TIM_OSSRState_Disable; TIM_BDTRInitStruct->TIM_OSSIState = TIM_OSSIState_Disable; TIM_BDTRInitStruct->TIM_LOCKLevel = TIM_LOCKLevel_OFF; TIM_BDTRInitStruct->TIM_DeadTime = 0x00; TIM_BDTRInitStruct->TIM_Break = TIM_Break_Disable; TIM_BDTRInitStruct->TIM_BreakPolarity = TIM_BreakPolarity_Low; TIM_BDTRInitStruct->TIM_AutomaticOutput = TIM_AutomaticOutput_Disable; }[/mw_shl_code] 没有要求让我把TIM1给他,那么他如何知道我是要初始化TIM1的呢?其他部分代码应该没有问题,因为使用tim3时可以输出
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
7条回答
灰太狼小灰兔
1楼-- · 2019-07-21 16:27
麻烦楼主能贴下 代码么?鄙人的照着原子哥的修改,出不来波形?

void TIM1_PWM_Init(u32 arr, u32 psc)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);    //TIM1时钟使能
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);   //使能PORTB时钟

    GPIO_PinAFConfig(GPIOB,GPIO_PinSource13, GPIO_AF_TIM1);//PB13 复用为TIM1

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;              //GPIOB13
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;           //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;     //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;         //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;           //上拉
    GPIO_Init(GPIOB, &GPIO_InitStructure);                 //初始化 B13  

TIM_TimeBaseStructure.TIM_Prescaler = psc;             //定时器分频
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
    TIM_TimeBaseStructure.TIM_Period = arr;                //自动重装值
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
 
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);              //初始化定时器1

//初始化 TIM1 Channel WM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;            //PWM调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;     //输出极性低
TIM_OC1Init(TIM1, &TIM_OCInitStructure);                     //初始化外设 TIM1 OC1 
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);             //使能预装载寄存器
        TIM_ARRPreloadConfig(TIM1,ENABLE);                           //ARPE使能

        TIM_CtrlPWMOutputs(TIM1,ENABLE);                             //高级定时器 还需要设置一个MOE位以使能主输出 否则不会输出                       WM
   TIM_Cmd(TIM1,ENABLE);                                        //使能TIM1
}
胡桃
2楼-- · 2019-07-21 04:41
明白了,我贴的那个函数,用用来初始化TIM_BDTRInitStruct的,初始化TIM1的另一个函数。。。TIM_BDTRConfig(TIM1,&TIM_BDTRInitStruct);是这个。。。。
正点原子
3楼-- · 2019-07-21 06:04
 精彩回答 2  元偷偷看……
胡桃
4楼-- · 2019-07-21 09:25
改配置的我都配置了,但是输出还是为0,还望赐教。。。。我对于那个不用传TIM的函数还是耿耿于怀,应为看了网上其他的函数,是要传的 [mw_shl_code=c,true]GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_BDTRInitTypeDef TIM_BDTRInitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //?????¨?±?÷1?±?? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //????GPIO???è??AFIO???????????é?±?? TIM_DeInit(TIM1); //?????÷???è???±???? GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE); //Timer1??·??????? //?è?????????????????????? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8; //PA7??PA8 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //???????ì???? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);//??????GPIO //??????TIM1 TIM_TimeBaseStructure.TIM_Period = arr; //?è???????????ü??????×°????????×?????×°???????÷???????? TIM_TimeBaseStructure.TIM_Prescaler =psc; //?è??????×÷??TIMx?±?????????????¤·????? TIM_TimeBaseStructure.TIM_ClockDivision = 0; //?è???±??·???:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM?ò?????????? TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //?ù??TIM_TimeBaseInitStruct?????¨????????????TIMx???±???ù?????? //??????TIM1 Channel2 PWM???? TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //?????¨?±?÷????:TIM?????í???÷??????2 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±??????????? TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = 200; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //????????:TIM????±????????? TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC1Init(TIM1, &TIM_OCInitStructure); //?ù??T???¨???????????????èTIM1 OC2 TIM_OC2Init(TIM1, &TIM_OCInitStructure); TIM_OC3Init(TIM1, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //????TIM1??CCR2?????¤×°???????÷ TIM_BDTRInitStruct.TIM_OSSRState = TIM_OSSRState_Enable; TIM_BDTRInitStruct.TIM_OSSIState = TIM_OSSIState_Enable; TIM_BDTRInitStruct.TIM_LOCKLevel = TIM_LOCKLevel_OFF; TIM_BDTRInitStruct.TIM_DeadTime = 1; TIM_BDTRInitStruct.TIM_Break = TIM_Break_Disable; TIM_BDTRInitStruct.TIM_BreakPolarity = TIM_BreakPolarity_High; TIM_BDTRInitStruct.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; TIM_BDTRStructInit(&TIM_BDTRInitStruct); TIM1->BDTR |= 1<<15; TIM_Cmd(TIM1, ENABLE); //????TIM1 TIM_ARRPreloadConfig(TIM1, ENABLE); TIM_CtrlPWMOutputs(TIM1,ENABLE);[/mw_shl_code]
胡桃
5楼-- · 2019-07-21 14:21
再贴一段别人需要传参的代码 [mw_shl_code=c,true]TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);[/mw_shl_code] [mw_shl_code=c,true]这里是传了TIM1的[/mw_shl_code] [mw_shl_code=c,true]TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //运行模式下“关闭状态”使能 TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //关闭模式下“关闭状态”使能 TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //锁定关闭 TIM_BDTRInitStructure.TIM_DeadTime = 80; //死区时间为13.8ns TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //刹车功能使能 TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //刹车输入高电平有效 TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //自动输出使能 TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);[/mw_shl_code]
胡桃
6楼-- · 2019-07-21 15:38
 精彩回答 2  元偷偷看……

一周热门 更多>