//1秒=1000毫秒(ms), 1毫秒=1/1000秒(s);
//1秒=1000000 微秒(μs), 1微秒=1/1000000秒(s);
//1秒=1000000000 纳秒(ns),1纳秒=1/1000000000秒(s);
//1秒=1000000000000皮秒 1皮秒==1/1000000000000秒。
//1MHZ=1000KHZ=1000000HZ
//24Mhz = 24000khz = 24000000hz
//1秒运行24000000次 = 1us运行 1000000/24000000 =0.4166666666。。。 次
//24Mhz = 24000khz = 24000000hz
//1秒运行24000000次 = 1us运行 1000000/24000000 =0.4166666666。。。 次
//MCS51 (1机器周期 = 12时钟周期 )
//12Mhz时钟周期 = 1/12 us
//所以24Mhz时钟周期 = 1000000/24000000 us = 1/24 us
//24Mhz机器周期 = 12 * (1/24) us = 0.5us
//12Mhz时钟周期 = 1/12 us
//所以24Mhz时钟周期 = 1000000/24000000 us = 1/24 us
//24Mhz机器周期 = 12 * (1/24) us = 0.5us
//定时器0方式1(TMOD=0x01) 为16Bit 为1111111111111111 = 65536
//所以TH0=(65536-初值)%256 (FF-11111111);
// TL0=(65536-初值)/256;
//所以TH0=(65536-初值)%256 (FF-11111111);
// TL0=(65536-初值)/256;
//例如用24M晶振做1ms定时计算如下:
//M/机器周期=初值;
// 1000us / 0.5us = 2000;
//所以:TH0=(65536-2000)%256;
//TL0=(65536-2000)/256;
//将65536-1000=63536化为16进制为:0xF830
//TH0=0xF8;
//TL0=0X30;
//TL0=(65536-2000)/256;
//将65536-1000=63536化为16进制为:0xF830
//TH0=0xF8;
//TL0=0X30;
下面给个示例:(1秒)更新一次数码管
#include <AT89S53.H>
char led_map[11]=
{0x81, 0xB7,0xC2, 0x92, 0xB4, 0x98, 0x88, 0xB3, 0x80, 0x90, 0xA0};
//0,1,2,3,4,5,6,7,8,9,A
unsigned long t=0;
unsigned char n=0;
void main()
{
TMOD=0x01;
TH0=0xF8;
TL0=0x30;
ET0=1;
EA=1;
TR0=1;
while (1);
}
void timer0() interrupt 1
{
TH0 = 0xF8;
TL0 = 0x30;
t++;
if (t==1000)
{
n++;
if (n>9) n=0;
P0=led_map[n];
t=0;
}
}