📄 timer.c.bak
字号:
#include "timer.h"
#include "schedule.h"
struct Timer_mul {
unsigned char num;
unsigned char on;
unsigned char type; //类型,reapeat=1 单次=0
unsigned long ticks; //执行周期
unsigned long tickleft; //剩余时间,单位为秒
unsigned long adjustedticks;
}T0,T1,T2,T3;
/*
//Timer接口
//Timer0
void start_T0(char type, unsigned int count);
void stop_T0(void);
//Timer1
void start_T1(char type, unsigned int count);
void stop_T1(void);
//Timer2
void start_T2(char type, unsigned int count);
void stop_T2(void);
//Timer3
void start_T3(char type, unsigned int count);
void stop_T3(void);
//init
void T0_init();
void T1_init();
void T2_init();
void T3_init();
*/
//Timer接口
//Timer0
void start_T0(char type, unsigned int count)
{
//128分频,外部时钟,最大900ms计数,精度0.2
unsigned char t_count;
if (type==1)
T0.type = 1;
else
T0.type = 0;
T0.ticks = count;
T0.tickleft = count;
if (T0.tickleft > 900)
{
T0.tickleft -= 900;
TCNT0 = 0x0; //setup
}
else
{
t_count = T0.tickleft*256/1000 -1;
T0.tickleft =0;
TCNT0 = 0xff - (t_count & 0xff); //setup
}
TCCR0 = 0x05; //start timer
TIMSK |= 0x01;
}
void stop_T0(void)
{
TCCR0 = 0x0;
TIMSK &=~0x01;
}
//Timer1
void start_T1(char type, unsigned int count)
{
unsigned int t_count;
if (type==1)
T1.type = 1;
else
T1.type = 0;
T1.ticks = count;
T1.tickleft = count;
if (T1.tickleft > 9000)
{
T1.tickleft -= 9000;
TCNT1H = 0x0; //setup
TCNT1L = 0x0;
}
else
{
t_count = T1.tickleft*72/10 -1;
T1.tickleft =0;
TCNT1H = 0xff - ((t_count>>8) & 0xff); //setup
TCNT1L = 0xff - (t_count & 0xff);
}
TCCR1B = 0x05; //1024分频
TIMSK |= 0x04;
// led1On();
}
void stop_T1(void)
{
TCCR1B = 0x0;
TIMSK &=~(0x04);
}
//Timer2
void start_T2(char type, unsigned int count)
{
//1024分频,最大35ms
unsigned char t_count;
if (type==1)
T2.type = 1;
else
T2.type = 0;
T2.ticks = count;
T2.tickleft = count;
if (T2.tickleft > 35)
{
T2.tickleft -= 35;
TCNT2 = 0x0; //setup
}
else
{
t_count = T2.tickleft*72/10 -1;
T2.tickleft =0;
TCNT2 = 0xff - (t_count & 0xff); //setup
}
TCCR2 = 0x05; //start timer
TIMSK |= 0x40;
}
void stop_T2(void)
{
TCCR2 = 0x0;
TIMSK &= ~0x40;;
}
//Timer3
void start_T3(char type, unsigned int count)
{
unsigned int t_count;
if (type==1)
T3.type = 1;
else
T3.type = 0;
T3.ticks = count;
T3.tickleft = count;
if (T3.tickleft > 9000)
{
T3.tickleft -= 9000;
TCNT3H = 0x0; //setup
TCNT3L = 0x0;
}
else
{
t_count = T3.tickleft*72/10 -1;
T3.tickleft =0;
TCNT3H = 0xff - ((t_count>>8) & 0xff); //setup
TCNT3L = 0xff - (t_count & 0xff);
}
TCCR3B = 0x05; //1024分频
ETIMSK |= 0x04;
}
void stop_T3(void)
{
TCCR3B = 0x0;
ETIMSK &= ~0x04;
}
//init
void T0_init()
{
T0.num = 0;
T0.on = 0;
T0.type = 0; //类型,reapeat=1 单次=0
T0.ticks = 0; //执行周期
T0.tickleft = 0; //剩余时间,单位为秒
T0.adjustedticks = 0;
TCCR0 = 0x00; //stop
ASSR = 0x08; //set async mode
// TCCR0 = 0x05; //start timer
}
void T1_init(void)
{
T1.num = 0;
T1.on = 0;
T1.type = 0; //类型,reapeat=1 单次=0
T1.ticks = 0; //执行周期
T1.tickleft = 0; //剩余时间,单位为秒
T1.adjustedticks = 0;
TCCR1B = 0x00; //stop
//TCNT1H = 0xE3; //setup
//TCNT1L = 0xE1;
TCCR1A = 0x00;
//TCCR1B = 0x05; //start Timer
}
void T2_init()
{
T2.num = 0;
T2.on = 0;
T2.type = 0; //类型,reapeat=1 单次=0
T2.ticks = 0; //执行周期
T2.tickleft = 0; //剩余时间,单位为秒
T2.adjustedticks = 0;
TCCR2 = 0x00; //stop
// TCCR2 = 0x05; //start
}
void T3_init()
{
T3.num = 0;
T3.on = 0;
T3.type = 0; //类型,reapeat=1 单次=0
T3.ticks = 0; //执行周期
T3.tickleft = 0; //剩余时间,单位为秒
T3.adjustedticks = 0;
TCCR3B = 0x00; //stop
TCCR3A = 0x00;
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
unsigned char t_count;
// printf("T0\n");
if (T0.tickleft == 0)
{
post(T0_timeout);
if (T0.type==1)
T0.tickleft = T0.ticks;
else
{
TIMSK &= ~0x01;
return ;
}
}
if (T0.tickleft > 900)
{
T0.tickleft -= 900;
TCNT0 = 0x0; //setup
}
else
{
t_count = T0.tickleft*256/1000 -1;
T0.tickleft =0;
TCNT0 = 0xff - (t_count & 0xff); //setup
}
TCCR0 = 0x05; //1024分频
TIMSK |= 0x01;
}
#pragma interrupt_handler timer1_ovf_isr:15
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
unsigned int t_count;
// printf("T1\n");
if (T1.tickleft == 0)
{
post(T1_timeout);
if (T1.type==1)
T1.tickleft = T1.ticks;
else
{
TIMSK &= ~0x04;
return;
}
}
if (T1.tickleft > 9000)
{
T1.tickleft -= 9000;
TCNT1H = 0x0; //setup
TCNT1L = 0x0;
}
else
{
t_count = T1.tickleft*72/10 -1;
T1.tickleft =0;
TCNT1H = 0xff - ((t_count>>8) & 0xff); //setup
TCNT1L = 0xff - (t_count & 0xff);
}
TCCR1B = 0x05; //1024分频
TIMSK |= 0x04;
}
#pragma interrupt_handler timer2_ovf_isr:11
void timer2_ovf_isr(void)
{
unsigned char t_count;
// printf("T2\n");
if (T2.tickleft == 0)
{
post(T2_timeout);
if (T2.type==1)
T2.tickleft = T2.ticks;
else
{
TIMSK &= ~0x40;
return ;
}
}
if (T2.tickleft > 35)
{
T2.tickleft -= 35;
TCNT2 = 0x0; //setup
}
else
{
t_count = T2.tickleft*256/1000 -1;
T2.tickleft =0;
TCNT2 = 0xff - (t_count & 0xff); //setup
}
TCCR2 = 0x05; //1024分频
TIMSK |= 0x40;
}
#pragma interrupt_handler timer3_ovf_isr:30
void timer3_ovf_isr(void)
{
//TIMER3 has overflowed
unsigned int t_count;
if (T3.tickleft == 0)
{ led0Toggle();
if( post(T3_timeout))
// printf("post success\n");
if (T3.type==1)
T3.tickleft = T3.ticks;
else
{
ETIMSK &= ~0x04;
return ;
}
}
if (T3.tickleft > 9000)
{
T3.tickleft -= 9000;
TCNT3H = 0x0; //setup
TCNT3L = 0x0;
}
else
{
t_count = T3.tickleft*72/10 -1;
T3.tickleft =0;
TCNT3H = 0xff - ((t_count>>8) & 0xff); //setup
TCNT3L = 0xff - (t_count & 0xff);
}
TCCR3B = 0x05; //1024分频
ETIMSK |= 0x04;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -