V1.0
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
#include "timer.h"
|
||||
#include "xc_drv_timer.h"
|
||||
#include "timeslice.h"
|
||||
#include "PWM.h"
|
||||
#include "mode.h"
|
||||
#include "uart.h"
|
||||
#include "RF433.h"
|
||||
#include "pwm.h"
|
||||
TimerStatus_t timer_status =
|
||||
{
|
||||
.t0_flag = false,
|
||||
.t1_flag = false,
|
||||
.t2_flag = false,
|
||||
.t3_flag = false,
|
||||
};
|
||||
// 定义多个软定时器
|
||||
Timer timer1, timer2,timer3, 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, uint32_t 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()
|
||||
{
|
||||
// 在这里编写定时器2到达设定时间时需要执行的代码
|
||||
|
||||
}
|
||||
// 软定时器4到达设定时间时执行的函数
|
||||
void timer4Callback(void)//对码计时
|
||||
{
|
||||
// MatchOverFlag = 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @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_32M_DIV;
|
||||
timer_cfg.timer_div_clk = TIMER_DIV_CLK_16MHzOr16K;
|
||||
timer_cfg.timer_mode = TIMER_MODE_CYCLE;
|
||||
|
||||
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, 100); //10us
|
||||
xc_timer_start(TIMER2_IDX);
|
||||
|
||||
NVIC_SetPriority((IRQn_Type)TIMER2_IRQn,0);
|
||||
NVIC_EnableIRQ((IRQn_Type)TIMER2_IRQn);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Timer0_Callback
|
||||
* @details Timer0 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
* 100
|
||||
*/
|
||||
__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
|
||||
*/
|
||||
|
||||
uint8_t isssss=0;
|
||||
__RAM_CODE void timer1_callback(void *context)
|
||||
{
|
||||
TaskRemarks();
|
||||
// printf("dd=%d--%d--%d-%d-%d\r\n",choose_mode_falsg,W_PWM,C_PWM,driveMode,Brightness);
|
||||
|
||||
updateTimer(&timer1);
|
||||
updateTimer(&timer2);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Timer2_Callback
|
||||
* @details Timer2 handler callback function
|
||||
* @param void *context
|
||||
* @retval void
|
||||
* 100us模拟1kHZ PWM
|
||||
*/
|
||||
|
||||
uint8_t aa=0;
|
||||
__RAM_CODE void timer2_callback(void *context) //10us
|
||||
{
|
||||
rf433_receive();
|
||||
if(deviceStatus==POWERON){
|
||||
timeCnt++;
|
||||
|
||||
if(W_PWM_duty==0){deadTime_W=0;}
|
||||
else {deadTime_W=_deadTime;}
|
||||
|
||||
if(C_PWM_duty==0){deadTime_C=0;}
|
||||
else {deadTime_C=_deadTime;}
|
||||
uint16_t totalPhase = (W_PWM_duty+deadTime_W) + (C_PWM_duty+deadTime_C) + 2*deadTime; //看注释为什么要这样加(W_PWM_duty+deadTime_W) + (C_PWM_duty+deadTime_C)
|
||||
|
||||
switch (driveMode)
|
||||
{
|
||||
case 0: // 正+反交替(周期切换)
|
||||
if (timeCnt < deadTime)
|
||||
{
|
||||
xc_gpio_write_pin(IO_PWM_W, GPIO_PIN_RESET); // 互补输出低
|
||||
xc_gpio_write_pin(IO_PWM_C, GPIO_PIN_RESET); // 主输出高
|
||||
}
|
||||
else
|
||||
{
|
||||
if (timeCnt <(deadTime + W_PWM_duty)) // 正转段
|
||||
{
|
||||
xc_gpio_write_pin(IO_PWM_W, GPIO_PIN_SET); // 互补输出低
|
||||
xc_gpio_write_pin(IO_PWM_C, GPIO_PIN_RESET); // 主输出高
|
||||
}
|
||||
else if (timeCnt < deadTime + W_PWM_duty + deadTime) // 死区时间
|
||||
{
|
||||
xc_gpio_write_pin(IO_PWM_W, GPIO_PIN_RESET); // 互补输出低
|
||||
xc_gpio_write_pin(IO_PWM_C, GPIO_PIN_RESET); // 主输出高
|
||||
}
|
||||
else if (timeCnt < (totalPhase)) // 反转段
|
||||
{
|
||||
xc_gpio_write_pin(IO_PWM_C, GPIO_PIN_SET); // 互补输出低
|
||||
xc_gpio_write_pin(IO_PWM_W, GPIO_PIN_RESET); // 主输出高
|
||||
}
|
||||
else
|
||||
{
|
||||
xc_gpio_write_pin(IO_PWM_W, GPIO_PIN_RESET); // 互补输出低
|
||||
xc_gpio_write_pin(IO_PWM_C, GPIO_PIN_RESET); // 主输出高
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 1: // 只正转
|
||||
if(timeCnt<=W_PWM_duty){
|
||||
xc_gpio_write_pin(IO_PWM_C, GPIO_PIN_RESET); // 主输出高
|
||||
xc_gpio_write_pin(IO_PWM_W, GPIO_PIN_SET); // 互补输出低
|
||||
aa=0;
|
||||
}else{
|
||||
xc_gpio_write_pin(IO_PWM_W, GPIO_PIN_RESET); // 互补输出低
|
||||
xc_gpio_write_pin(IO_PWM_C, GPIO_PIN_RESET); // 主输出高
|
||||
aa=1;
|
||||
}
|
||||
|
||||
break;
|
||||
case 2: // 只反转
|
||||
if(timeCnt<=C_PWM_duty){
|
||||
xc_gpio_write_pin(IO_PWM_W, GPIO_PIN_RESET); // 互补输出低
|
||||
xc_gpio_write_pin(IO_PWM_C, GPIO_PIN_SET); // 主输出高
|
||||
}else{
|
||||
xc_gpio_write_pin(IO_PWM_W, GPIO_PIN_RESET); // 互补输出低
|
||||
xc_gpio_write_pin(IO_PWM_C, GPIO_PIN_RESET); // 主输出高
|
||||
}
|
||||
break;
|
||||
default:
|
||||
xc_gpio_write_pin(IO_PWM_W, GPIO_PIN_RESET); // 互补输出低
|
||||
xc_gpio_write_pin(IO_PWM_C, GPIO_PIN_RESET); // 主输出高
|
||||
break;
|
||||
}
|
||||
if (timeCnt >= PWMMAX){timeCnt = 0;}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user