单片机时钟为12MHz 利用定时器T1 使P1.0引脚输出2ms 的矩形波,要求占空比例1:2 试编写程序

2024-11-23 03:46:39
推荐回答(3个)
回答1:

#include //12MHZ
sbit pause = P1^0;

void main(void)
{
EA = 1;
TMOD = 0x10;
TH1 = (65536-1000)/256; //1ms定时
TL1 = (65536-1000)%256;
ET1 = 1;
TR1 = 1;
pause = 0;
while(1);
}
void timeint(void) interrupt 3 //占空比例1:2
{
static unsigned char i;
TH1 = (65536-1000)/256;
TL1 = (65536-1000)%256;
if(i++ == 1)
{
P10= 1; //2ms高电平
}
else if(i == 3)
{
P10=0; //1ms低电平
i = 0;
}
}
不懂得还可以问我……
在中断中的变量i需是静态变量static,在中断结束时不释放,所以楼上的程序是不可以的!

回答2:

#include //12MHZ
sbit P10 = P1^0;
unsigned char i=0;
void main(void)
{
TMOD = 0x10;
TH1 = (65536-1000)/256; //1ms定时
TL1 = (65536-1000)%256;
ET1 = 1;
EA = 1;
TR1 = 1;
P10 = 1;
while(1);
}
void t0_int() interrupt 3 //占空比例1:2
{
TH1 = (65536-1000)/256;
TL1 = (65536-1000)%256;
i++;
if(i== 2)
{
P10= 0; //2ms高电平
}
if(i==3)
{
P10=1; //1ms低电平
i = 0;
}
}

回答3:

我想说你写的TMOD=OX10是什么鬼。应该是0x01吧