从STC 89C52 上移植到 STC12C5A60S2
本来也是简单的事,
但是 12C 的定时器 上的记时时间和理论上的或弟三方提供的都不对
按 老妖说的 12C 1T 1Mhz 下 定时器每一脉冲因当为 1us
弟三方给出现 12C 1T 1Mhz 下 是 1.5us
写了代码
定时器 0 用 16位的方式 跑约一分钟 得出来的数值
定时器0 用 8位自装的方式 跑约 一分钟 得出来的数值
都和 上边的理论值出入比较大 ,为此俺就迷茫了~~~~ 看看 12C的PDF上也没有准确的说到这个定时器怎么样算的
看了他的示例代码是 完全按 1T 的理论方法来算的
#include "reg51.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
//-----------------------------------------------
/* define constants */
#define FOSC 18432000L
#define MODE1T //Timer clock mode, comment this line is 12T mode, uncomment is 1T mode
#ifdef MODE1T
#define T1MS (65536-FOSC/1000) //1ms timer calculation method in 1T mode
#else
#define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode
#endif
/* define SFR */
sfr AUXR = 0x8e; //Auxiliary register
sbit TEST_LED = P0^0; //work LED, flash once per second
/* define variables */
WORD count; //1000 times counter
//-----------------------------------------------
/* Timer0 interrupt routine */
void tm0_isr() interrupt 1 using 1
{
TL0 = T1MS; //reload timer0 low byte
TH0 = T1MS >> 8; //reload timer0 high byte
if (count-- == 0) //1ms * 1000 -> 1s
{
count = 1000; //reset counter
TEST_LED = ! TEST_LED; //work LED flash
}
}
//-----------------------------------------------
/* main program */
void main()
{
#ifdef MODE1T
AUXR = 0x80; //timer0 work in 1T mode
#endif
TMOD = 0x01; //set timer0 as mode1 (16-bit)
TL0 = T1MS; //initial timer0 low byte
TH0 = T1MS >> 8; //initial timer0 high byte
TR0 = 1; //timer0 start running
ET0 = 1; //enable timer0 interrupt
EA = 1; //open global interrupt switch
count = 0; //initial counter
while (1); //loop
}
#define T1MS (65536-FOSC/1000) //1ms timer calculation method in 1T mode
#else
#define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode
求提点 这 STC 12C 的定时器到底是多少的 , 我都怀疑他自己的代码有没验证过
|