高级会员
- 积分
- 673
- 金钱
- 673
- 注册时间
- 2020-11-21
- 在线时间
- 67 小时
|
发表于 2020-12-29 15:37:14
|
显示全部楼层
- #include "timer1.h"
- void TIM1_PWM_Init(u16 arr, u16 psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //复用时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- TIM_TimeBaseInitStructure.TIM_Period = arr;
- TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
- TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //只有高级定时器需要设置,其他定时器可以不设置。
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC1Init(TIM1, &TIM_OCInitStructure); //TIM1_OC1 PB6
- TIM_OC2Init(TIM1, &TIM_OCInitStructure); //TIM1_OC2 PB7
- TIM_OC3Init(TIM1, &TIM_OCInitStructure); //TIM1_OC1 PB8
- TIM_OC4Init(TIM1, &TIM_OCInitStructure); //TIM1_OC1 PB9
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_Cmd(TIM1, ENABLE);
- TIM_CtrlPWMOutputs(TIM1, ENABLE); //高级定时器才有 必须打开
- }
- //PWM互补输出
- void TIM1_PWM_CHN_Init(u16 arr, u16 psc)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); //复用时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //基本设置
- TIM_TimeBaseInitStructure.TIM_Period = arr;
- TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
- TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //只有高级定时器需要设置,其他定时器可以不设置。
- TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure);
- //死区设置 不设置的话 互补波形完全对称 死区时间为0
- TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
- TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
- TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
- TIM_BDTRInitStructure.TIM_DeadTime = 0xff;
- TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
- TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
- TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
- TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
- //输出模式设置
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
- //TIM_OCInitStructure.TIM_Pulse = i; //设置比较寄存器值 调节占空比 此程序在主函数中直接用 TIM_SetCompare()方法设置
-
- TIM_OC1Init(TIM1, &TIM_OCInitStructure); //TIM1_OC1 PB6
- TIM_OC2Init(TIM1, &TIM_OCInitStructure); //TIM1_OC2 PB7
- TIM_OC3Init(TIM1, &TIM_OCInitStructure); //TIM1_OC1 PB8
- TIM_OC4Init(TIM1, &TIM_OCInitStructure); //TIM1_OC1 PB9
- //使能自动重装载 直到产生更新事件才能更改设置
- TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
- TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
-
- TIM_ARRPreloadConfig(TIM1, ENABLE);
- TIM_Cmd(TIM1, ENABLE);
- TIM_CtrlPWMOutputs(TIM1, ENABLE); //高级定时器才有 必须打开
- }
- int main(void)
- {
- u16 i =450;
- delay_init();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- TIM1_PWM_Init(1000, 72);
- //TIM1_PWM_CHN_Init(1000,72);
- while(1)
- {
- i++;
- if(i > 999)
- i = 0;
- TIM_SetCompare1(TIM1, i);
- TIM_SetCompare2(TIM1, i);
- TIM_SetCompare3(TIM1, i);
- TIM_SetCompare4(TIM1, i);
- delay_ms(10);
- }
- }
复制代码 |
|