📄 time.c
字号:
#include "main.h"
unsigned int us_time,ms_time,s_time;
//1MHz
void timer0_init(void)
{
TCCR0 = 0x00; //stop
OSCCAL=0xB9; //标定8M晶振
TCNT0 = 0x38; //set count;200us
TCCR0 = 0x02; //start timer
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
us_time=0;
ms_time=0;
s_time=0;
SEI(); //re-enable interrupts
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x38; //时钟节拍200us
us_time++;
if(us_time==5) //1ms定时
{
us_time=0;
ms_time++;
if(ms_time==1000) //1s定时
{
ms_time=0;
s_time++;
if(s_time==60) //1分钟定时
s_time=0;
}
}
}
void delay_s(unsigned char n)
{
unsigned int time_now,time_set;
time_now=s_time;
time_set=time_now+n;
if(time_set>=60)
time_set=time_set-60;
while(time_now!=time_set)
{
time_now=s_time;
}
}
void delay_ms(unsigned int n)
{
unsigned int time_now,time_set;
time_now=ms_time;
time_set=time_now+n;
if(time_set>=1000)
time_set=time_set-1000;
while(time_now!=time_set)
{
time_now=ms_time;
}
}
asm("_delay_us:: \n"
"loop: subi R16,1 \n"//1
" sbci R17,0 \n"//1
" breq end \n"//1~2
" nop \n"//1
" nop \n"//1
" jmp loop \n"//3
" end: ret \n");//4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -