怎样用定时器来延时?
# include<intrins.h>
# define ui unsigned int
# define uc unsigned char
uc temp, i, j, m;
int main(void)
{
while(1)
{
TMOD = 0x11;
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
for(i=0; i<7; i++)
{
temp = 0xfe;
P1 = temp;
temp = _crol_(temp, 1);
P1 = temp;
}
for(i=0; i<7; i++)
{
temp = 0x7f;
P1 = temp;
temp = _cror_(temp, 1);
P1 = temp;
}
}
}
return 0;
}
void timer0() interrupt 1
{
for(i=0; i<20; i++)
{
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
}
}
给你整理一下,看看是否有帮助!
#include<reg51.h>
# include<intrins.h>
# define ui unsigned int
# define uc unsigned char
uc temp, i;
void main()
{ i=0;
temp = 0xfe;
TMOD = 0x01;
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
P1=temp;
}
}
void timer0() interrupt 1
{ TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
i++;
if(i==20)
{
i=0;
temp = _crol_(temp, 1);
}
}