📄 timer.c
字号:
#include "Timer.h"
volatile uint16_t WaitTimer;
extern volatile uint16_t waitFlag;
ISR(TIMER0_COMP_vect)
{
if(--WaitTimer == 0)
{
TCCR0 = 0;
TIMSK &= 0xFD;
}
}
ISR(TIMER2_COMP_vect)
{
if(--waitFlag == 0)
{
TCCR2 = 0;
TIMSK &= 0x7F;
}
}
void Timer_Set_us(uint16_t t)
{
waitFlag = 1;
TCNT2 = 0;
if(t < 5)
{
DELAY_8M_us;
DELAY_8M_us;
waitFlag = 0;
}
else if(t < 256)
{
OCR2 = t;
TCCR2 = 0x0A;
}
else if(t < 4096)
{
OCR2 = (t>>3);
TCCR2 = 0x0B;
}
else
{
OCR2 = (t>>8);
TCCR2 = 0x0C;
}
TIMSK |= 0x80;
}
void Timer_Set_ms(uint16_t t)
{
waitFlag = t*4;
OCR2 = 250;
TCCR2 = 0x0A;
TIMSK |= 0x80;
}
void Timer_Set_s(uint16_t t)
{
waitFlag = 4000 * t;
OCR2 = 250;
TCCR2 = 0x0A;
TIMSK |= 0x80;
}
void Timer_Wait_us(uint16_t t)
{
WaitTimer = 1;
TCNT0 = 0;
if(t < 5)
{
DELAY_8M_us;
DELAY_8M_us;
WaitTimer = 0;
}
else if(t < 256)
{
OCR0 = t;
TCCR0 = 0x0A;
}
else if(t < 4096)
{
OCR0 = (t>>3);
TCCR0 = 0x0B;
}
else
{
OCR0 = (t>>8);
TCCR0 = 0x0C;
}
TIMSK |= 0x02;
while(WaitTimer);
}
void Timer_Wait_ms(uint16_t t) //limited to 10000ms
{
WaitTimer = t*4;
OCR0 = 250;
TCCR0 = 0x0A;
TIMSK |= 0x02;
while(WaitTimer);
}
void Timer_Wait_s(uint16_t t) //limited to 16s
{
WaitTimer = 4000 * t;
OCR0 = 250;
TCCR0 = 0x0A;
TIMSK |= 0x02;
while(WaitTimer);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -