#ifndef _TIMER_H_
#define _TIMER_H_
#include "stm32f10x.h"
#include "type.h"
// LED定時器,按鍵定時器,重發定時器
#define LED_TIMER TIM1
#define LED_TIMER_DIV 7200
#define LED_TIMER_PERIOD 2500
#define PWM_TIMER TIM2
#define RESEND_TIMER TIM3
#define RESEND_TIMER_DIV 7200
#define RESEND_TIMER_PERIOD 2500
#define KEY_TIMER TIM4
#define KEY_TIMER_DIV 7200
#define KEY_TIMER_PERIOD 200
void TimerNvicConfig(void);
void TimerConfig(TIM_TypeDef *timer, uint16_t div, uint16_t period);
void TimerEnable(TIM_TypeDef *timer);
void TimerDisable(TIM_TypeDef *timer);
#endif /* _TIMER_H_ */
#include "stm32f10x_tim.h"
#include "stm32f10x_rcc.h"
#include "timer.h"
/*
* 三個定時器: 1 燈色 2 按鍵 3 重發
*/
void TimerNvicConfig(void)
{
NVIC_InitTypeDef NVIC_StructInit;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_StructInit.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_StructInit.NVIC_IRQChannelCmd = ENABLE;
NVIC_StructInit.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_StructInit.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_StructInit);
NVIC_StructInit.NVIC_IRQChannel = TIM2_IRQn;
NVIC_StructInit.NVIC_IRQChannelCmd = ENABLE;
NVIC_StructInit.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_StructInit.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_StructInit);
NVIC_StructInit.NVIC_IRQChannel = TIM3_IRQn;
NVIC_StructInit.NVIC_IRQChannelCmd = ENABLE;
NVIC_StructInit.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_StructInit.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_StructInit);
NVIC_StructInit.NVIC_IRQChannel = TIM4_IRQn;
NVIC_StructInit.NVIC_IRQChannelCmd = ENABLE;
NVIC_StructInit.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_StructInit.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_StructInit);
}
void TimerConfig(TIM_TypeDef *timer, uint16_t div, uint16_t period)
{
TIM_TimeBaseInitTypeDef TIM_StructInit;
if(LED_TIMER == timer)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
}
else if(RESEND_TIMER == timer)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
}
else if(KEY_TIMER == timer)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
}
TIM_StructInit.TIM_Period = period;
TIM_StructInit.TIM_Prescaler = div;
TIM_StructInit.TIM_ClockDivision = TIM_CKD_DIV1; /* 數字濾波器采樣頻率 */
TIM_StructInit.TIM_CounterMode = TIM_CounterMode_Up; /* 向上計數 */
TIM_StructInit.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(timer, &TIM_StructInit);
TIM_ITConfig(timer, TIM_IT_Update, ENABLE); /* 允許中斷 */
TIM_ClearFlag(timer, TIM_FLAG_Update); /* 更新定時器會産生更新時間,清除标志位 */
}
void TimerEnable(TIM_TypeDef *timer)
{
TIM_Cmd(timer, ENABLE);
}
void TimerDisable(TIM_TypeDef *timer)
{
TIM_Cmd(timer, DISABLE);
}