|
以下程序是把STC15F204EA单片机测量P1.3-P1.7几个管脚的电压得到V01-V05这5个电压,原来是发送到LCD1602显示的,现在想把这几个电压值组合成"V01,V02,V03,V04,V05;"这样的字符串发送到串口来显示。
现在串口可以显示u8 buf[11]="1,2,3,4,5;"的内容,尝试了很多次要把显示内容改为"V01,V02,V03,V04,V05;"都不成功,哪位高手能帮忙修改一下。
//本程序从串口发送字符串,单片机串口监控必须设置为9600bps
#define MAIN_Fosc 11059200L //定义主时钟
#include "STC15Fxxxx.H"
typedef bit BOOL; //定义别名
typedef unsigned char BYTE;
typedef unsigned int WORD;
sbit RXB = P3^0; //??????
sbit TXB = P3^1;
#define BAUD 0xFE80 // 9600bps @ 11 .0592MHz
BYTE TBUF,RBUF;
BYTE TDAT,RDAT;
BYTE TCNT,RCNT;
BYTE TBIT,RBIT;
BOOL TING,RING;
BOOL TEND,REND;
BOOL CT;
BYTE t, r;
u8 buf[11]="1,2,3,4,5;"; //从串口发送的内容
/************* 本地变量声明 **************/
u16 Bandgap,Get_ADC(u8 channel),j; //channel = 0~7
u8 m1,m2,m3,m4,stop=0,i;
u32 V0,V1,V2,V3,V4,V5,power,V01,V02,V03,V04,V05,V=1420;
sbit OUT=P3^4; //15脚P3.4控制外部
u16 Get_ADC(u8 channel) //channel = 0~7
{
ADC_RES = 0;
ADC_RESL = 0;
ADC_CONTR = (ADC_CONTR & 0xE0) | 0x08 | channel; //start the ADC
NOP(4);
while((ADC_CONTR & 0x10) == 0) ; //wait for ADC finish
ADC_CONTR &= ~0x10; //清除ADC结束标志
return (((u16)ADC_RES << 2) | (ADC_RESL & 3));
}
void ADC(X)
{
P1ASF =1<<X;
Get_ADC(X); //通道改变, 先读一次并丢弃结果, 让内部的采样电容的电压等于输入值.
for(j=0, i=0; i<16; i++) {j+= Get_ADC(X);}
j=j>>4;
}
void UART_INIT()
{
TMOD = 0x00;
AUXR = 0x80;
TL0 = BAUD;
TH0 = BAUD>>8;
TR0 = 1;
ET0 = 1;
PT0 = 1;
EA = 1;
TING = 0;
RING = 0;
TEND = 1;
REND = 0;
TCNT = 0;
RCNT = 0;
}
void main()
{
BYTE f=0;
WORD e=0;
UART_INIT(); //初始化串口
ADC_CONTR = 0xE0; //90T, ADC power on
while (1)
{
if (stop==0)
{
ADC(2); Bandgap = j;//TL431基准电压
ADC(3); V1 = (u16)((u32)j*758/ Bandgap);
ADC(4); V2 = (u16)((u32)j*1546/ Bandgap);
ADC(5); V3 = (u16)((u32)j*2317/ Bandgap);
ADC(6); V4 = (u16)((u32)j*3297/ Bandgap);
ADC(7); V5 = (u16)((u32)j*4050/ Bandgap);
V01=V1;
if(V2>V1){V02=V2-V1;}else{V02=0;}
if(V3>V1){V03=V3-V2;}else{V03=0;}
if(V4>V1){V04=V4-V3;}else{V04=0;}
if(V5>V1){V05=V5-V4;}else{V05=0;}
}
if(V01<V&&V02<V&&V03<V&&V04<V&&V05<V){}else{OUT=0;stop=1;} //5块电池
TING = 1;
TBUF = buf[f++];
e=6000;
while(e--); //延时
if (f>11){f=0;}
}
}
void tm0() interrupt 1 using 1
{
if (RING)
{
if (--RCNT == 0) //??---?????1
{
RCNT = 3;
if (--RBIT == 0) //????????---????---??9?--8????---1????
{
RBUF = RDAT; //?????
RING = 0;
REND = 1;
}
else//????9??????---????????--???
{
RDAT >>= 1;
if (RXB)
RDAT |= 0x80; //????????????0??1
}
}
}
else if (!RXB)
{
RING = 1;
RCNT = 4;
RBIT = 9;
}
if (--TCNT == 0) //??
{
TCNT = 3;
if (TING)
{
if (TBIT == 0)//9???????
{
TXB = 0; //?????
TDAT = TBUF; //??
TBIT = 9; //????????????8+1
TEND = 0;
}
else
{
TDAT >>= 1; //??---??????
if (--TBIT == 0)
{
TXB = 1;
TING = 0;
TEND = 1;
}
else
{
TXB = CY; //CY????????7?
}
}
}
}
}
|
|