#include <reg51.h>
#include <intrins.h>
UINT32 system_tick;
UINT16 led_tick;
//延时程序,需要根据不同的晶振大小调整
//////////////// 定时器T0
//11.0592M (50ms 4C00, 5ms EE00, 1ms FC66, 0.5ms FE33, 0.2ms FF47, 0.1ms FFA3, 0.05ms FFD1, 0.01ms FFF6)
//22.1184M (50ms xxxx, 5ms DC00, 1ms F8CD, 0.5ms FC66, 0.2ms FE8F, 0.1ms FF48, 0.05ms FFA4, 0.01ms FFEE)
#define T0_TH0 0xF8
#define T0_TL0 0xCD
void timer0_init(){
TMOD &= 0xf0; //定时器0,工作方式1
TMOD |= 0x01;
TH0 = T0_TH0;
TL0 = T0_TL0;
ET0=1; //开定时中断允许
TR0 = 1;//启动定时器0
EA=1; //开总中断
system_tick = 0;
led_tick = 0;
}
sbit led=P1^2;
sbit led_0 = P1^0;
sbit led_1 = P1^1;
//每tick 1ms
void timer0_interrupt(void) interrupt 1
{
TH0 = T0_TH0; //必须重新赋值才能让定时器循环工作
TL0 = T0_TL0;
++system_tick;
if((UINT16)system_tick-led_tick > 1000){
led_tick = system_tick;
led_1 = !led_1;
}
}
//1个tick为 1ms
void Delayms(UINT16 ms){
UINT16 ct16;
ct16 = (UINT16)system_tick;
while((UINT16)system_tick-ct16 < ms);
}
|