1、主要程序如下:#include “stc15.h“#define FOSC 11059200L /系统频率#define BAUD 115200 /串口波特率sbit P23 =P23; sbit P22 =P22;sbit P21 =P21;unsigned char dataH,dataL;unsigned int Volut,Shangxian,Xiaxian;unsigned char Recieve4;unsigned char i=0,m=0;unsigned char time0count;unsigned char qian,bai,shi,ge;bit thflag=0;vo
2、id SendData(unsigned char dat)SBUF = dat; /写数据到 UART 数据寄存器while(TI=0);TI=0;void SendString(char *s)while (*s) /检测字符串结束标志SendData(*s+); /发送当前字符void ClearAll(unsigned char *p)char n;for(n=0;n0;x-)for(y=10000;y0;y-);/*-*/void main(void)P21=1; /初始化 LED21 灭P22=1; /初始化 LED22 灭Shangxian=500;Xiaxian=100;/SC
3、ON = 0x50; /8 位可变波特率AUXR = 0x40; /定时器 1 为 1T 模式TMOD = 0x00; /定时器 1 为模式 0(16 位自动重载)TL1 = (65536 - (FOSC/4/BAUD); /设置波特率重装值TH1 = (65536 - (FOSC/4/BAUD)8;TR1 = 1; /定时器 1 开始启动ES = 1; /使能串口中断/ADC_CONTR|=0X80;delay(1);P1ASF=0x04; /P12 为模拟量输入口ADC_CONTR=0x82;CLK_DIV|=0x20;delay(1);/TH0 = (65536-50000)/256;
4、/定时 50msTL0 = (65536-50000)%256; /定时 50msTR0=1; /使能定时器 0ET0= 1; /使能定时器 0 中断time0count=0;EA = 1; /开总中断/while(1) if(thflag)thflag=0;ADC_CONTR|=0X08;/启动转换while(!(ADC_CONTRADC_CONTR /转换结束标志位清零dataL=ADC_RESL;dataH=ADC_RESVolut=(dataH*256+dataL);qian =Volut/1000 +48;bai=Volut/100%10+48;shi=Volut/10%10+48;
5、ge=Volut%10+48;SendData(qian); SendData(bai);SendData(shi); SendData(ge);SendData( ); /*-定时器 0 中断服务程序-*/void T0_ISR (void) interrupt 1i=0;thflag=1;time0count+;if(time0count=10)P23=P23;time0count=0;if(VolutShangxian)P21=P21;else if(VolutXiaxian)P22=P22;elseP21=1;P22=1; /*-UART 中断服务程序-*/void Uart() in
6、terrupt 4 if (RI)RI=0;Recievem=SBUF;m+;if(m=4)unsigned int tempp=(Recieve1-48)*100+(Recieve2-48)*10+(Recieve3-48); if(Recieve0=r)Shangxian=tempp;SendString(“n 设置上限为:“);SendData(Recieve1);SendData(Recieve2);SendData(Recieve3);SendData(n);if(Recieve0=d)Xiaxian=tempp;SendString(“n 设置下限为:“);SendData(Recieve1);SendData(Recieve2);SendData(Recieve3);SendData(n); m=0;ClearAll(Recieve);