定时器:
频率f=主频/(arr+1)(psc+1)
其中,arr——自动重装值——>Period
psc——预分频值(0-65535)——>Prescaler
例如f=72000000/(99+1)(719+1)=1000Hz,则周期T=1/1000=0.001s,即1ms
初始化:
//定时器14初始化
void TIM14_Counter_Init(u16 arr,u16 psc)
{
/* 定义一个定时器初始化结构体 */
timer_parameter_struct timer_init_struct;
//Enable TIMER14 clock开启 TIMER14的时钟
rcu_periph_clock_enable(RCU_TIMER14);
/* TIMER5 初始化配置 */
timer_deinit(TIMER14);
/* 初始化TIMER相关结构体默认值参数 */
timer_struct_para_init(&timer_init_struct);
timer_init_struct.prescaler = psc;/* 时钟预分频系数 */
timer_init_struct.period = arr;/* 自动重装载值 */
timer_init_struct.counterdirection = TIMER_COUNTER_UP;
timer_init_struct.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER14, &timer_init_struct);
nvic_irq_enable(TIMER14_IRQn, 1); /* TIMER14中断设置,抢占优先级1 */
timer_flag_clear(TIMER14,TIMER_FLAG_UP); //清除TIMx更新中断标志
timer_interrupt_enable(TIMER14,TIMER_INT_UP); /* 使能更新中断 */
timer_enable(TIMER14);
}
中断服务函数
//TIM14定时器中断服务函数
void TIMER14_IRQHandler(void)
{
if(timer_flag_get(TIMER14, TIMER_FLAG_UP)!=RESET)//检查TIM14更新中断发生与否
{
timer_flag_clear(TIMER14,TIMER_FLAG_UP); /* 定时器更新中断的标识位需要手动清除 */
tim4_count++;
if(tim4_count>=100)
{
tim4_count=0;
nowTime++;//100ms更新一次计时器
sleepCount++;
shutCount++;
sleepCheck();//检测振动开关
}
}
}
PWM:
//pwm初始化
void PWM_Init(void)
{
/* enable the GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
//Enable TIMER2 clock开启 TIMER2的时钟
rcu_periph_clock_enable(RCU_TIMER2);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_0);
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_0);
/* 定义一个定时器初始化结构体 */
timer_parameter_struct timer_init_struct;
/* 定义一个定时器输出比较参数结构体*/
timer_oc_parameter_struct timer_oc_init_struct;
//复位外设TIMERx
timer_deinit(TIMER2);
/* TIMER2 configuration */
timer_init_struct.prescaler = 719;
timer_init_struct.alignedmode = TIMER_COUNTER_EDGE;
timer_init_struct.counterdirection = TIMER_COUNTER_UP;
timer_init_struct.period = PWM_T-1;;
timer_init_struct.clockdivision = TIMER_CKDIV_DIV1;
timer_init_struct.repetitioncounter = 0;
timer_init(TIMER2, &timer_init_struct);
/* PWM初始化 */
timer_oc_init_struct.outputstate = TIMER_CCX_ENABLE; /* 通道使能 */
timer_oc_init_struct.outputnstate = TIMER_CCXN_DISABLE; /* 通道互补输出使能(定时器2无效) */
timer_oc_init_struct.ocpolarity = TIMER_OC_POLARITY_HIGH; /* 通道极性 */
timer_oc_init_struct.ocnpolarity = TIMER_OCN_POLARITY_HIGH;/* 互补通道极性(定时器2无效)*/
timer_oc_init_struct.ocidlestate = TIMER_OC_IDLE_STATE_LOW;/* 通道空闲状态输出(定时器2无效)*/
timer_oc_init_struct.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;/*互补通道空闲状态输出(定时器2无效) */
/* PWM Mode configuration: Channel2 */
timer_channel_output_config(TIMER2, TIMER_CH_2, &timer_oc_init_struct);
/* PWM模式0 */
timer_channel_output_mode_config(TIMER2,TIMER_CH_2,TIMER_OC_MODE_PWM0);
/* 不使用输出比较影子寄存器 */
timer_channel_output_shadow_config(TIMER2,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
//所有的通道输出使能
// timer_primary_output_config(TIMER0,ENABLE);
/* 自动重装载影子比较器使能 TIMERx自动重载影子使能 使能定时器自动重装载值*/
timer_auto_reload_shadow_enable(TIMER2);
/* TIMER2 enable使能外设TIMERx */
timer_enable(TIMER2);
}
pwm输出函数,改变uk值改变占空比
//参数1:定时器名称
//参数2:定时器通道,根据输出引脚与定时器通道映射关系,看手册
//参数3:占空比设置 ,改变uk值即改变pwm输出占空比
//占空比d=uk/arr
timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_2, uk); |