stm32 高級(jí)定時(shí)器產(chǎn)生PWM
掃描二維碼
隨時(shí)隨地手機(jī)看文章
用stm32的高級(jí)定時(shí)器TIM1和TIM8產(chǎn)生PWM,需要注意:
1.都有TIM1,但只有flash容量大于256K的大容量單片機(jī)才有TIM8
2.高級(jí)定時(shí)器相對(duì)于通用定時(shí)器,多了TIM_CtrlPWMOutputs(TIM8, ENABLE);
TIM1產(chǎn)生四路PWM程序:
staticvoidTIM1_GPIO_Config(void)
{
GPIO_InitTypeDefGPIO_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;//復(fù)用推挽輸出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
staticvoidTIM1_Mode_Config(void)
{
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
TIM_OCInitTypeDefTIM_OCInitStructure;
u16CCR1_Val=500;
u16CCR2_Val=500;
u16CCR3_Val=500;
u16CCR4_Val=500;
TIM_TimeBaseStructure.TIM_Period=999;//當(dāng)定時(shí)器從0計(jì)數(shù)到999,即為1000次,為一個(gè)定時(shí)周期
TIM_TimeBaseStructure.TIM_Prescaler=71;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//設(shè)置時(shí)鐘分頻系數(shù):不分頻
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上計(jì)數(shù)模式
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//配置為PWM模式1
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
//TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;//設(shè)置跳變值,當(dāng)計(jì)數(shù)器計(jì)數(shù)到這個(gè)值時(shí),電平發(fā)生跳變
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//當(dāng)定時(shí)器計(jì)數(shù)值小于CCR1_Val時(shí)為高電平
TIM_OC1Init(TIM1,&TIM_OCInitStructure);//使能通道1
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR2_Val;//設(shè)置通道2的電平跳變值,輸出另外一個(gè)占空比的PWM
TIM_OC2Init(TIM1,&TIM_OCInitStructure);//使能通道2
TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR3_Val;//設(shè)置通道3的電平跳變值,輸出另外一個(gè)占空比的PWM
TIM_OC3Init(TIM1,&TIM_OCInitStructure);//使能通道3
TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=CCR4_Val;//設(shè)置通道4的電平跳變值,輸出另外一個(gè)占空比的PWM
TIM_OC4Init(TIM1,&TIM_OCInitStructure);//使能通道4
TIM_OC4PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1,ENABLE);//使能TIM1重載寄存器ARR
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE);
}
voidTIM1_PWM_Init(void)
{
TIM1_GPIO_Config();
TIM1_Mode_Config();
}
TIM8產(chǎn)生2路PWM程序:
staticvoidTIM8_GPIO_Config(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//復(fù)用推挽輸出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
staticvoidTIM8_Mode_Config(void)
{
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
TIM_OCInitTypeDefTIM_OCInitStructure;
u16CCR1_Val=500;
u16CCR2_Val=500;
u16CCR3_Val=500;
u16CCR4_Val=500;
TIM_TimeBaseStructure.TIM_Period=999;//當(dāng)定時(shí)器從0計(jì)數(shù)到999,即為1000次,為一個(gè)定時(shí)周期
TIM_TimeBaseStructure.TIM_Prescaler=71;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;//設(shè)置時(shí)鐘分頻系數(shù):不分頻
TIM_TimeBaseStructure.T