V1.0
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
/*!
|
||||
* \file timer.c
|
||||
*
|
||||
* \brief Target timer implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "timer.h"
|
||||
#include "xc_drv_timer.h"
|
||||
#include "timeslice.h"
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
TimerStatus_t timer_status =
|
||||
{
|
||||
.t0_flag = false,
|
||||
.t1_flag = false,
|
||||
.t2_flag = false,
|
||||
.t3_flag = false,
|
||||
};
|
||||
// 定义多个软定时器
|
||||
Timer timer1, timer2, timer4, timer5;
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
// 初始化定时器
|
||||
void initTimer(Timer *timer)
|
||||
{
|
||||
timer->count = 0;
|
||||
timer->active = 1;
|
||||
}
|
||||
|
||||
// 更新定时器
|
||||
void updateTimer(Timer *timer)
|
||||
{
|
||||
if (timer->active && timer->count > 0)
|
||||
{
|
||||
timer->count--;
|
||||
if (timer->count == 0)
|
||||
{
|
||||
timer->active = 0; // 定时器到达设定时间,将活跃状态置为0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 启动定时器
|
||||
void startTimer(Timer *timer, unsigned int duration)
|
||||
{
|
||||
timer->count = duration;
|
||||
timer->active = 1;
|
||||
}
|
||||
// 中止定时器
|
||||
void stopTimer(Timer *timer)
|
||||
{
|
||||
timer->active = 1;
|
||||
timer->count = 0;
|
||||
}
|
||||
|
||||
// 软定时器1到达设定时间时执行的函数
|
||||
void timer1Callback(uint8_t mode)//模式切换回调
|
||||
{
|
||||
//modefunctin[mode]();
|
||||
}
|
||||
|
||||
// 软定时器2到达设定时间时执行的函数
|
||||
void timer2Callback(void)//占空比刷新回调
|
||||
{
|
||||
|
||||
}
|
||||
// 软定时器4到达设定时间时执行的函数
|
||||
void timer4Callback(void)//对码计时
|
||||
{
|
||||
// MatchOverFlag = 1;
|
||||
}
|
||||
//// 软定时器5到达设定时间时执行的函数
|
||||
//void timer5Callback(uint8_t mode)//模式循环,每个模式执行的时间
|
||||
//{
|
||||
|
||||
//// if (++MODE19_i > 18) //先自增,如果大于20,就赋值0;
|
||||
//// MODE19_i = 1;
|
||||
// mode++;
|
||||
// DEBUG("mode:%d\n",mode);
|
||||
// if (mode > 18) //先自增,如果大于20,就赋值0;
|
||||
// mode = 1;
|
||||
// // if (mode == MODE19)
|
||||
// {
|
||||
// updateParameterMode19(mode);
|
||||
// startTimer(&timer1, 1); // 立刻启动定时器1,设定间隔为1ms,执行1次,每次进来就是切换到下一个程序,需要立刻切换,
|
||||
// startTimer(&timer5, 5000);//程序切换周期20s
|
||||
//
|
||||
// DEBUG("r:%d\n",mode);
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
/**
|
||||
* @brief timer0_init
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
|
||||
uint8_t dd=0;
|
||||
void timer0_2_init(void)
|
||||
{
|
||||
uint32_t us = 1000;
|
||||
Timer_InitCfg_t timer_cfg;
|
||||
|
||||
timer_cfg.timer_src_clk = TIMER_CLK_SRC_32K;
|
||||
timer_cfg.timer_div_clk = TIMER_DIV_CLK_16MHzOr16K;
|
||||
timer_cfg.timer_mode = TIMER_MODE_CYCLE;
|
||||
|
||||
// // Timer0 Config & Start
|
||||
// xc_timer_init(TIMER0_IDX, &timer_cfg);
|
||||
// xc_timer_set_value(TIMER0_IDX, us);//for(int i=0;i<0x455000;i++);
|
||||
// xc_timer_start(TIMER0_IDX);
|
||||
// Timer1 Config & Start
|
||||
xc_timer_init(TIMER1_IDX, &timer_cfg);
|
||||
xc_timer_set_value(TIMER1_IDX, us);
|
||||
xc_timer_start(TIMER1_IDX);
|
||||
// Timer2 Config & Start
|
||||
xc_timer_init(TIMER2_IDX, &timer_cfg);
|
||||
xc_timer_set_value(TIMER2_IDX, us);
|
||||
xc_timer_start(TIMER2_IDX);
|
||||
// Timer3 Config & Start
|
||||
xc_timer_init(TIMER3_IDX, &timer_cfg);
|
||||
xc_timer_set_value(TIMER3_IDX, us);
|
||||
xc_timer_start(TIMER3_IDX);
|
||||
}
|
||||
|
||||
|
||||
///**
|
||||
// * @brief Timer0_Callback
|
||||
// * @details Timer0 handler callback function
|
||||
// * @param void *context
|
||||
// * @retval void
|
||||
// */
|
||||
//__RAM_CODE void timer0_callback(void *context) //
|
||||
//{
|
||||
// //TaskRemarks();
|
||||
//// if(dd==1){
|
||||
//// dd=0;
|
||||
//// }else if(dd==0){
|
||||
////
|
||||
//// }
|
||||
// dd++;
|
||||
//}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Timer0_Callback
|
||||
* @details Timer0 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void timer0_callback(void *context)
|
||||
{
|
||||
// timer_status.t0_flag = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Timer1_Callback
|
||||
* @details Timer1 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void timer1_callback(void *context)
|
||||
{
|
||||
timer_status.t1_flag = true;
|
||||
if(timer_status.t1_flag == true)
|
||||
{
|
||||
TaskRemarks();
|
||||
timer_status.t1_flag = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Timer2_Callback
|
||||
* @details Timer2 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void timer2_callback(void *context)
|
||||
{
|
||||
timer_status.t2_flag = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Timer3_Callback
|
||||
* @details Timer3 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
*/
|
||||
__RAM_CODE void timer3_callback(void *context)
|
||||
{
|
||||
timer_status.t3_flag = true;
|
||||
}
|
||||
Reference in New Issue
Block a user