51单片机定时器初值用变量设置定时不准确,为什么,TH0=(65536-X)/256,TL0=(65536-X)%256晶振12MTH0=(65536-1000)%256;TL0=(65536-1000)%256;这种定时方式是准确的但是如果我用变量x=1000;TH0=(65536-x)%256;TL0=(65536-x)%256;
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/04 01:37:30
51单片机定时器初值用变量设置定时不准确,为什么,TH0=(65536-X)/256,TL0=(65536-X)%256晶振12MTH0=(65536-1000)%256;TL0=(65536-1000)%256;这种定时方式是准确的但是如果我用变量x=1000;TH0=(65536-x)%256;TL0=(65536-x)%256;
51单片机定时器初值用变量设置定时不准确,为什么,TH0=(65536-X)/256,TL0=(65536-X)%256
晶振12M
TH0=(65536-1000)%256;
TL0=(65536-1000)%256;
这种定时方式是准确的
但是如果我用变量
x=1000;
TH0=(65536-x)%256;
TL0=(65536-x)%256;
定时时间就不对了,这是不是正常现象,还是我程序有问题,全部如下
#include
#define uchar unsigned char
#define uint unsigned int
uchar pwm;uint a,b,c,d;
sbit out=P0^0;
void main(void)
{
out=0;
pwm=50;
a=2000;
TMOD=0x11;
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
TH1=(65536-1000)/256;
TL1=(65536-1000)%256;
TR0=1;
TR1=0;
ET0=1;
ET1=1;
EA=1;
while(1);}
void timer0(void)interrupt 1
{
a=2000;
TR0=0;
TR1=0;
TH0=(65536-2000)/256; TL0=(65536-2000)%256;
TH1=(65536-1000)/256; TL1=(65536-1000)%256;
TR0=1;
TR1=1;
out=1;
}
void timer1(void)interrupt 3
{
TR1=0;
out=0;
}
51单片机定时器初值用变量设置定时不准确,为什么,TH0=(65536-X)/256,TL0=(65536-X)%256晶振12MTH0=(65536-1000)%256;TL0=(65536-1000)%256;这种定时方式是准确的但是如果我用变量x=1000;TH0=(65536-x)%256;TL0=(65536-x)%256;
看一下C代码编译后的汇编代码就知道了,用变量方式很耗时,而直接用数字,编译器已经将算式的值算好了,代码里就是一个MOV指令而已,只需1us!
TH0=(65536-2000)/256; TL0=(65536-2000)%256;
汇编后,只要2us就完事了.而用变量方式,单片机很忙,计算几十乃至上百微妙才完事!
15:TH0=(65536-a)/256;
C:0x0190 AE09 MOV R6,a(0x09)
C:0x0192 AF0A MOV R7,0x0A
C:0x0194 AB07 MOV R3,0x07
C:0x0196 AA06 MOV R2,0x06
C:0x0198 E4 CLR A
C:0x0199 C3 CLR C
C:0x019A 9B SUBB A,R3
C:0x019B FF MOV R7,A
C:0x019C E4 CLR A
C:0x019D 9A SUBB A,R2
C:0x019E FE MOV R6,A
C:0x019F 7401 MOV A,#0x01
C:0x01A1 9400 SUBB A,#0x00
C:0x01A3 FD MOV R5,A
C:0x01A4 E4 CLR A
C:0x01A5 9400 SUBB A,#0x00
C:0x01A7 FC MOV R4,A
C:0x01A8 E4 CLR A
C:0x01A9 FB MOV R3,A
C:0x01AA 7A01 MOV R2,#0x01
C:0x01AC F9 MOV R1,A
C:0x01AD F8 MOV R0,A
C:0x01AE 1201EA LCALL SLDIV(C:01EA)
C:0x01B1 8F8C MOV TH0(0x8C),R7
16:TL0=(65536-a)%256;
C:0x01B3 AE09 MOV R6,a(0x09)
C:0x01B5 AF0A MOV R7,0x0A
C:0x01B7 AB07 MOV R3,0x07
C:0x01B9 AA06 MOV R2,0x06
C:0x01BB E4 CLR A
C:0x01BC C3 CLR C
C:0x01BD 9B SUBB A,R3
C:0x01BE FF MOV R7,A
C:0x01BF E4 CLR A
C:0x01C0 9A SUBB A,R2
C:0x01C1 FE MOV R6,A
C:0x01C2 7401 MOV A,#0x01
C:0x01C4 9400 SUBB A,#0x00
C:0x01C6 FD MOV R5,A
C:0x01C7 E4 CLR A
C:0x01C8 9400 SUBB A,#0x00
C:0x01CA FC MOV R4,A
C:0x01CB E4 CLR A
C:0x01CC FB MOV R3,A
C:0x01CD 7A01 MOV R2,#0x01
C:0x01CF F9 MOV R1,A
C:0x01D0 F8 MOV R0,A
C:0x01D1 1201EA LCALL SLDIV(C:01EA)
C:0x01D4 AF03 MOV R7,0x03
C:0x01D6 8F8A MOV TL0(0x8A),R7
17:TH1=(65536-1000)/256;
C:0x01D8 758DFC MOV TH1(0x8D),#0xFC
18:TL1=(65536-1000)%256;
C:0x01DB 758B18 MOV TL1(0x8B),#0x18