先配置好IO
注:TIM1的定时器通道时间1到4分别为PA8 PB9 PB10 PB11而互补输出分别为PB13 PB14 PB15 中止PB12
void GPIO_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//设置推挽输出
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//PWM波设为复用推挽输出
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//设置推挽输出
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//PWM波设为复用推挽输出
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//PWM波设为复用推挽输出
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//PWM波设为复用推挽输出
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
再对TIM1进行配置:
void TIM1_PWM_1_init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Prescaler=4000;//预分频(时钟分频)72M/4000=18K
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period=144;//装载值 18K/144=125Hz 向上加144就满了
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//设置了时钟分割
TIM_TimeBaseStructure.TIM_RepetitionCounter=0x00;//周期数值
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);//初始化TIMX的时间计基数单位
/*channel 1 configuration in pwm mode 通道一的PWM*/
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;//PWM模式2
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//正向通道有效
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;//方向通道有效
TIM_OCInitStructure.TIM_Pulse=40;//占空时间144中有40的时间为高,互补的输出正好相反
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;//输出极性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;//互补端的输出极性
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;//空闲状态下的非工作状态下
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);//初始化外设TIMX通道1
//*************************************通道2******************************************//
TIM_OCInitStructure.TIM_Pulse=60;//占空比的初值
TIM_OC2Init(TIM1,&TIM_OCInitStructure);//初始化外设TIMX通道2
//**************************************通道3********************************************//
TIM_OCInitStructure.TIM_Pulse=70;
TIM_OC3Init(TIM1,&TIM_OCInitStructure);//初始化外设TIMX通道3
//************************************通道4***********************************************//
TIM_OCInitStructure.TIM_Pulse=80;
TIM_OC2Init(TIM1,&TIM_OCInitStructure);//初始化外设TIMX通道4
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
//设置TIM1捕获寄存器 设置占空比
void SetT1Pwm1(u16 pulse1,u16 pulse2,u16 pulse3,u16 pulse4 )
{
TIM1->CCR1=pulse1;
TIM1->CCR2=pulse2;
TIM1->CCR3=pulse3;
TIM1->CCR4=pulse4;
}
时钟配置
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD, ENABLE);
}
接下来就是主函数了
void main()
{
RCC_Configuration();
GPIO_init();
TIM1_PWM_1_init();
SetT1Pwm1(10,20,30,40);//控制4路PWM的占空比
while(1);
}
|