接收串口发送过来的数字,0X80输出第一路方波,0X81输出第二路方波#include"reg51.h"
sbit OUT0=P1^0;
sbit OUT1=P1^1;
unsigned char T0_flag,Com_flag;
unsigned char ch;
void PWM0(unsigned char count);
void PWM1(unsigned char count);
void init_T0(void);
void init_Com(void);
main()
{
init_T0();
init_Com();
T0_flag=0;
Com_flag=0;
while(1)
{
while(Com_flag==0);
Com_flag=0;
switch(ch)
{
case 0x80: //0号方波输出
PWM0(20); // 50Hz
break;
case 0x81:
PWM1(10); //100Hz
break;
default:
break;
}
}
}
void PWM0(unsigned char count)
{
TR0=1;
while(1)
{
while(T0_flag==0);
T0_flag=0;
count--;
if(count==0) OUT0=~OUT0;
if(Com_flag==1)
{
TR0=0;
break;
}
}
}
void PWM1(unsigned char count)
{
TR0=1;
while(1)
{
while(T0_flag==0);
T0_flag=0;
count--;
if(count==0) OUT1=~OUT1;
if(Com_flag==1)
{
TR0=0;
break;
}
}
}
void init_T0(void)
{
TMOD|=0x01;
TH0=0xfc;
TL0=0x17;
EA=1;
ET0=1;
}
void isr_T0(void) interrupt 1 using 2 //1MS
{
TH0=0xfc;
TL0=0x17;
T0_flag=1;
}
void init_Com(void)
{
SCON=0x50;
TMOD|=0x20;
PCON|=0x80;
TH1=0xf4;
IE|=0x90;
TR1=1;
}
void isr_Com(void) interrupt 4 using 3 //串口中断
{
if(RI)
{
RI=0;
ch=SBUF;
Com_flag=1;
}
} |