如题,新手问题~
问题一:
错误:
#define MCLK 11059200UL
uint16 tmp = 0;
tmp = MCLK/12/100;
正确:
#define MCLK 11059200UL
uint32 tmp = 0;
tmp = MCLK/12/100;
如上面两个例子,为什么将tmp定义为int型不行呢?MCLK/12/100的计算值明明没有超过65536呢?
问题二:用定时器0定时1ms,为什么用keil debug显示相邻两次进入中断间隔了整整20ms呢?
#include<reg52.h>
unsigned char T0RH = 0,T0RL = 0;
#define MCLK 11059200UL
void
timer0_init(unsigned char ms)
{
unsigned long tms;
tms = ms*MCLK/12/1000;
tms = 65536 - tms;
T0RH = (unsigned char)(tms >> 8);
T0RL = (unsigned char)tms;
TH0 = T0RH;
TL0 = T0RL;
TMOD &= 0xF0;
TMOD |= 0x01;
TR0 = 1;
ET0 = 1;
}
void main()
{
EA = 1;
timer0_init(1);
while(1);
}
void interrupt_timer0()interrupt 1
{
P0 = 0;
}
MCLK/12/100也是长整型
uint16 tmp = 0;
tmp = (uint16)(MCLK/12/100);
第二 、用定时器0定时1ms,为什么用keil debug显示相邻两次进入中断间隔了整整20ms呢。你按错键了。
一周热门 更多>