前言
为了偏于后期移植,会对相应的功能进行API封装,留出对外接口函数,方便调用。
1. 定时器
仅展示和介绍作者在项目中测试和使用过的代码:
- 普通定时 ,定时0.1s:
/*!
\brief configure the TIMER1 peripheral
\param[in] none
\param[out] none
\retval none
*/
void ConfigTimer1(void)
{
/* ----------------------------------------------------------------------------
TIMER1 Configuration:
TIMER1CLK = SystemCoreClock/7200 = 10KHz, the period is 1s(1000/10000 = 0.1s).
---------------------------------------------------------------------------- */
timer_parameter_struct timer_initpara;
rcu_periph_clock_enable(RCU_TIMER1);
timer_deinit(TIMER1);
/* initialize TIMER init parameter struct */
timer_struct_para_init(&timer_initpara);
/* TIMER1 configuration */
timer_initpara.prescaler = 7199;
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
timer_initpara.counterdirection = TIMER_COUNTER_UP;
timer_initpara.period = 999;
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
timer_init(TIMER1, &timer_initpara);
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
timer_interrupt_enable(TIMER1, TIMER_INT_UP);
timer_enable(TIMER1);
/* configure the TIMER1 interrupt */
nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
nvic_irq_enable(TIMER1_IRQn, 1, 1);
}
中断函数为:
/*!
\brief this function handles TIMER1 interrupt request.
\param[in] none
\param[out] none
\retval none
*/
void TIMER1_IRQHandler(void)
{
if(SET == timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_UP)){
/* clear channel 0 interrupt bit */
timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
// 功能实现
}
}
- 读取PWM输入占空比和频率 :
/*!
\brief configure the TIMER3 peripheral
\param[in] none
\param[out] none
\retval none
*/
void ConfigTimer3(void)
{
/* TIMER3 configuration: PWM input mode ------------------------
the external signal is connected to TIMER3 CH0 pin(PB6)
the rising edge is used as active edge
the TIMER3 CH0CV is used to compute the frequency value
the TIMER3 CH1CV is used