You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
2.6 KiB
123 lines
2.6 KiB
#include "t21_t8.h"
|
|
#include "led.h"
|
|
volatile unsigned char timer_cnt;
|
|
extern volatile int motor_jerk_state;
|
|
int32_t wait_send_to_motor_time1s=0;
|
|
int32_t recv_motor_data_time=0;
|
|
int32_t recv_lcd_data_time=0;
|
|
int32_t inquire_error_time=0;
|
|
volatile uint32_t g_ticket100us = 0;//一直再记录过去的时间
|
|
/**
|
|
* @brief 32M的情况下初始化时钟1作为ticket源
|
|
*
|
|
*/
|
|
void init_timer1_as_ticketustimer_in32M() {
|
|
// T11M
|
|
/**
|
|
* @brief
|
|
* 设定时钟为1ms一次中断
|
|
* T11CM = 0x0f 16分频 = 2MHZ
|
|
* T11CH = 0 后分频0
|
|
* T11PH T11PL = 200 //100us
|
|
* T11PH = 0x07
|
|
* T11PL = 0xD0
|
|
*/
|
|
|
|
T11CL = 0x00; //定时器模式
|
|
T11CM = 0x0F; //预分频1:16 //最大1:16
|
|
T11CH = 0x00; //后分频次数0+1
|
|
static_assert(configTicketUs <= 10000 && configTicketUs >= 100);
|
|
uint16_t count = 2000 / 1000 * configTicketUs;
|
|
T11PH = count >> 8; //周期值高8位
|
|
T11PL = count & 0xFF; // 200
|
|
T11VIE = 1; //打开T11溢出中断
|
|
T11VIF = 0; //清标志位
|
|
T11EN = 1; //使能T11
|
|
}
|
|
|
|
|
|
int32_t clear_time()
|
|
{
|
|
if(wait_send_to_motor_time1s>=UINT32_MAX)
|
|
{
|
|
wait_send_to_motor_time1s=0;
|
|
}
|
|
if(recv_motor_data_time>=UINT32_MAX)
|
|
{
|
|
recv_motor_data_time=0;
|
|
}
|
|
if(g_ticket100us>=UINT32_MAX)
|
|
{
|
|
g_ticket100us=0;
|
|
}
|
|
if(recv_lcd_data_time>=UINT32_MAX)
|
|
{
|
|
recv_lcd_data_time=0;
|
|
}
|
|
if(inquire_error_time>=UINT32_MAX)
|
|
{
|
|
inquire_error_time=0;
|
|
}
|
|
}
|
|
|
|
uint32_t hal_has_passed_ticket(uint32_t lastticket) {
|
|
uint32_t now = g_ticket100us;
|
|
if (now >= lastticket) {
|
|
return now - lastticket;
|
|
} else {
|
|
return UINT32_MAX - lastticket + now;
|
|
}
|
|
}
|
|
|
|
uint32_t hal_has_passedms(uint32_t lastticket) {
|
|
/**
|
|
* 根据ticket的数值进行修改.
|
|
*/
|
|
static_assert(configTicketUs == 1000);
|
|
return hal_has_passed_ticket(lastticket);
|
|
}
|
|
|
|
|
|
int32_t hal_get_ticket()
|
|
{
|
|
return g_ticket100us;
|
|
}
|
|
|
|
int32_t hal_get_irqticket()
|
|
{
|
|
return g_ticket100us;
|
|
}
|
|
|
|
int32_t sleep_ms(int32_t time)
|
|
{
|
|
/**
|
|
* @brief ms级的延时
|
|
*
|
|
*/
|
|
int32_t tickstart = hal_get_ticket();
|
|
int32_t wait =time;
|
|
while(hal_get_ticket()-tickstart<wait)
|
|
{
|
|
}
|
|
}
|
|
void hal_ticket_inc()
|
|
{
|
|
g_ticket100us++;
|
|
wait_send_to_motor_time1s++;
|
|
recv_motor_data_time++;
|
|
recv_lcd_data_time++;
|
|
inquire_error_time++;
|
|
}
|
|
|
|
|
|
void system_module_process_interrupt() {
|
|
/**
|
|
* @brief 定时器中断11中断处理
|
|
*/
|
|
if (T11VIE == 1 && T11VIF == 1) //定时器溢出中断
|
|
{
|
|
hal_ticket_inc();
|
|
T11VIF = 0; //清标志位
|
|
}
|
|
}
|
|
|