主机
#include<reg52.h>
#include<stdio.h>
#include "delay.h"
#include "math.h"
#define TRUE 0x01
#define FALSE 0x00
sbit Echo=P2^0;
sbit Trip=P2^1;
char dis0[16];
unsigned long time_20ms;
unsigned char DisFlag=FALSE;
float JuLi;
long JlCoun=0;
void TIM2Inital(void);
void uartSendStr(unsigned char *s,unsigned char length);
void UART_Init(void);
void uartSendByte(unsigned char dat);
void main (void)
{
TMOD|=0x01;
TH0=0;
TL0=0;
ET0=1;
EA=1;
UART_Init();
sprintf(dis0,"*%05d#",(unsigned int)JuLi);
uartSendStr(dis0,7);
while (1)
{
Trip=1;
DelayUs2x(4);
Trip=0;
while(!Echo);
TR0=1;
JuLi=(float)(JlCoun*17)/100.0*1.08;
if((JlCoun>0)&&(JuLi>0))
{
uartSendStr(dis0,7);
}
DelayMs(200);
DelayMs(200);
}
}
void UART_Init(void)
{
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xFD;
TL1 = TH1;
TR1 = 1;
EA = 1;
ES = 1;
}
void uartSendByte(unsigned char dat)
{
unsigned char time_out;
time_out=0x00;
SBUF = dat;
while((!TI)&&(time_out<100))
{time_out++;DelayUs2x(10);}
TI = 0;
}
void uartSendStr(unsigned char *s,unsigned char length)
{
unsigned char NUM;
NUM=0x00;
while(NUM<length)
{
uartSendByte(*s);
s++;
NUM++;
}
}
void UART_SER (void) interrupt 4
{
if(RI)
{
RI=0;
}
if(TI)
TI=0;
}
从机
#include <reg52.h>
#include"delay.h"
#include "1602.h"
#include <stdio.h>
sbit shuibegn =P1^3;
sbit buzzer =P1^2;
unsigned long times_20ms=0;
unsigned char firstin =0;
unsigned char tab[8];
unsigned char Count=0;
unsigned char uartbusy =0;
unsigned char reportFlag = 0;
unsigned char dis1[16];
unsigned char i;
unsigned int juLi=12345;
unsigned char laterNum =0;
void Init_Timer0(void);
void UART_Init(void);
void SendByte(unsigned char dat);
void SendStr(unsigned char *s,unsigned char length);
void main (void)
{
Init_Timer0();
UART_Init();
LCD_Init();
DelayMs(20);
LCD_Clear();
LCD_Write_String(0,0,"My Designer! ");
sprintf(dis1,"Now:%05dmm ",juLi);
LCD_Write_String(0,1,dis1);
while(1)
{
LCD_Write_String(0,1,dis1);
if ( juLi>10&&juLi<20)
{buzzer=~buzzer;
DelayMs(10); }
DelayMs(200);
}
}
void Init_Timer0(void)
{
TMOD |= 0x01;
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
EA=1;
ET0=1;
TR0=1;
}
void UART_Init(void)
{
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xFD;
TL1 = TH1;
TR1 = 1;
EA = 1;
ES = 1;
}
void SendByte(unsigned char dat)
{
unsigned char time_out;
time_out=0x00;
SBUF = dat;
while((!TI)&&(time_out<100))
{time_out++;DelayUs2x(10);}
TI = 0;
}
void SendStr(unsigned char *s,unsigned char length)
{
unsigned char NUM;
NUM=0x00;
while(NUM<length)
{
SendByte(*s);
s++;
NUM++;
}
}
void Timer0_isr(void) interrupt 1
{
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
times_20ms++;
if(laterNum>0)
{laterNum--;}
else
{juLi = 0;}
if(uartbusy>0)
{uartbusy--;}
else
{
firstin =0;
Count=0;
}
}
void UART_SER (void) interrupt 4
{
unsigned char r_buf;
if(RI)
{
RI=0;
r_buf = SBUF;
uartbusy = 20;
if(r_buf=='*')
{
firstin = 1;
Count = 0;
tab[Count++]=r_buf;
}
else if(firstin == 1)
{
tab[Count++]=r_buf;
if(Count>=7)
{
if(tab[6]=='#')
{
juLi = (tab[1]-'0')*10000+ (tab[2]-'0')*1000+(tab[3]-'0')*100+(tab[4]-'0')*10+(tab[5]-'0');
laterNum = 150;
}
firstin =0;
Count=0;
}
}
}
if(TI)
TI=0;
}
以上怎么通讯不了,求大神请教
|