高级会员

- 积分
- 971
- 金钱
- 971
- 注册时间
- 2020-1-6
- 在线时间
- 124 小时
|
发表于 2019-8-6 12:05:52
|
显示全部楼层
void TIM4_Config(int arr,int psc)
{
GPIO_InitTypeDef GPIO_InitStructure; //结构体初始化
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //结构体初始化
TIM_OCInitTypeDef TIM_OCInitStructure; //结构体初始化
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化GPIO
GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_2);
/* TIM1 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 , ENABLE);
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;// 设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //初始化TIM4
/* Channel 1Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//比较输出使能
TIM_OCInitStructure.TIM_Pulse =0;// Channel1Pulse;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCNPolarity_High;//TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity =TIM_OCPolarity_Low;// TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_Cmd(TIM4, ENABLE);//使能TIM4
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM4, ENABLE);
}
|
|