Timer1属于高级定时器,在设置PWM模式的时候,必须加这一句话:使能PWM输出
TIM_CtrlPWMOutputs(TIM_1, ENABLE);
-
/*****************************************************************************
-
* Function : timer_GpioInit
-
* Description : gpio initialize for timer module.
-
* A8, A9 - TIM1_CH1, TIM1_CH2
-
A6, A7 - TIM3_CH1, TIM3_CH2
-
B0, B1 - TIM3_CH3, TIM3_CH4
-
-
* Input : void
-
* Output : None
-
* Return :
-
* Others :
-
* Record
-
* 1.Date : 20170307
-
* Author : grant.hu
-
* Modification: Created function
-
-
*****************************************************************************/
-
static void timer_GpioInit(void)
-
{
-
GPIO_InitTypeDef GPIO_InitStructure;
-
-
/* Enable clock for timer1, timer3 module */
-
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
-
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
-
-
/* GPIOB clock enable, Enable AFIO function */
-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
-
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; /* enable alternative function of GPIO */
-
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 |GPIO_Pin_9;
-
GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
-
GPIO_Init(GPIOB, &GPIO_InitStructure);
-
}
-
-
-
/*****************************************************************************
-
* Function : timer_SetFreDty
-
* Description : set input frequency and dutycycle for different channel
-
of different timer module.
-
* Input : TIM_TypeDef *TIM_TYPE
-
uint16_t * pu16HZ
-
uint8_t *u8Dty
-
* Output : None
-
* Return :
-
* Others :
-
* Record
-
* 1.Date : 20170307
-
* Author : grant.hu
-
* Modification: Created function
-
-
*****************************************************************************/
-
static int timer_SetFreDty(TIM_TypeDef *TIM_TYPE, PT_tmrOper ptTmrOper)
-
{
-
uint16_t u16HzTemp;
-
uint16_t u16Period;
-
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
-
TIM_OCInitTypeDef TIM_OCInitStructure;
-
-
if(TIM_TYPE != TIM1 && TIM_TYPE != TIM3)
-
return -1;
-
-
u16HzTemp = (TIM_TYPE == TIM1)? (ptTmrOper->u16Fre[0]) : (ptTmrOper->u16Fre[1]);
-
printf("timer HZ: %d\n", u16HzTemp);
-
-
TIM_InternalClockConfig(TIM_TYPE);
-
/* TIMxCLK = 72MHZ, timer module input clock = 72 MHZ/ (71 + 1) = 1Mhz */
-
TIM_TimeBaseStructure.TIM_Prescaler = 71;
-
-
/* set timer frequency */
-
u16Period = (uint16_t)((long)1000000 / u16HzTemp - 1);
-
-
TIM_TimeBaseStructure.TIM_Period = u16Period;
-
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;
-
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
-
TIM_TimeBaseInit(TIM_TYPE, &TIM_TimeBaseStructure);
-
-
/* timer mode setting. */
-
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
-
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
-
-
if(TIM_TYPE == TIM1)
-
{
-
/* TIM1, channel1 */
-
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
-
TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[0] * u16Period )/100;
-
TIM_OC1Init(TIM_TYPE, &TIM_OCInitStructure);
-
TIM_OC1PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);
-
-
/* TIM1, channel2 */
-
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
-
TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[1] * u16Period )/100;
-
TIM_OC2Init(TIM_TYPE, &TIM_OCInitStructure);
-
TIM_OC2PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);
-
-
// TODO: this code must be add when config TIM1, TIM8, different with configure general timer
-
TIM_CtrlPWMOutputs(TIM_TYPE, ENABLE);
-
}
-
else
-
{
-
/* TIM3, channel1 */
-
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
-
TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[2] * u16Period )/100;
-
TIM_OC1Init(TIM_TYPE, &TIM_OCInitStructure);
-
TIM_OC1PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);
-
-
/* TIM3, channel2 */
-
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
-
TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[3] * u16Period )/100;
-
TIM_OC2Init(TIM_TYPE, &TIM_OCInitStructure);
-
TIM_OC2PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);
-
-
/* TIM3, channel3 */
-
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
-
TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[4] * u16Period )/100;
-
TIM_OC3Init(TIM_TYPE, &TIM_OCInitStructure);
-
TIM_OC3PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);
-
-
/* TIM3, channel4 */
-
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
-
TIM_OCInitStructure.TIM_Pulse = (ptTmrOper->u8Dty[5] * u16Period )/100;
-
TIM_OC4Init(TIM_TYPE, &TIM_OCInitStructure);
-
TIM_OC4PreloadConfig(TIM_TYPE, TIM_OCPreload_Enable);
-
}
-
-
TIM_ARRPreloadConfig(TIM_TYPE, ENABLE);
-
TIM_Cmd(TIM_TYPE, ENABLE); /* TIM4 enable counter */
-
-
return 0;
-
}
阅读(2706) | 评论(0) | 转发(0) |