小弟想用STC12C5A60S2做一个多机通信系统,期望是主机发一个字符给从机,从机接收后发PWM波,现在问题是,从机的PWM初始化函数不知道出了什么问题,一调用了PWM初始化函数,串口通信就不行了,不调用则通信正常,以下是主从机程序,劳各位给帮忙看看
主机
#include <STC12C5A60S2.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar idata sendsbuf[4]=0;
uchar idata resbuf[4]=0;
float idata cj_data0[6]=0;
char idata cj_data1[6]=0;
uchar addrss[6]={0x10,0x31,0x32,0x33,0x34,0x35};
uchar num,h=0,num1,num12,num13,jj,iii,flag=0,mkno=0,e;
void serialport_init()
{
SCON = 0xd8; //串口方式3 ,允许接收
TMOD = 0x20; //T1工作于方式2
PCON = 0x00;//波特率不倍增
AUXR=0x15;
IPH=0x18;
IP=0x10;
TL1 = 0xfd; //波特率设置
TH1 = 0xfd; //
EA = 1; //开总中断
ES = 1; //开串口接收中断
//TI = 0;
TR1 = 1; //定时器开启
}
void error()
{
SBUF=0xff;
while(TI!=1);
TI=0;
}
void SerialPort_deal(uchar addr,uchar command)
{
uchar sb,p,j,q=0xff,as=0xff;
while(1)
{
SBUF=addr;
while(TI!=1);
TI=0;
while(RI!=1)
RI=0;
as=SBUF;
if(as!=0x30)
error();
else
{
TB8=0;
SBUF=command;
while(TI!=1)
TI=0;
while(RI!=1)
RI=0;
sb=SBUF;//接受从机传来的状态信号
if((sb&0x08)==0x08)
{
TB8=1;
error();
}
else
{
if((sb&0x30)==0x30)
{
do
{
p=0;
for(j=0;j<4;j++)
{
SBUF=sendsbuf[j];
p+=sendsbuf[j];
while(TI!=1)
TI=0;
}
SBUF=p;
while(TI!=1)
TI=0;
while(RI!=1);
RI=0;
q=SBUF;
}while(q!=0);
TB8=1;
}
}
}
}
}
void main()
{
uchar j;
e=25;
jj=0;
num12=1;
serialport_init();
while(1)
{
num1=20;
if(num1==20)
{
if(e>200)
e=200;
sendsbuf[0]=e/100;
sendsbuf[1]=e/10%10;
sendsbuf[2]=e%10;
sendsbuf[3]='v';
RL1=0;
for(j=0;j<1;j++)
SerialPort_deal(addrss[j],0x01);
sendsbuf[0]='s';
SerialPort_deal(addrss[0],0x01);
sendsbuf[4]=0;
for(j=0;j<1;)
{
SerialPort_deal(addrss[j],0x02);//要求接收反馈数据
if(resbuf[3]=='v')
{
cj_data1[j]=resbuf[0]*100+resbuf[1]*10+resbuf[2];
j++;
}
else
error();
}
}
}
}
从机
#include <STC12C5A60S2.h>
#define uchar unsigned char
#define uint unsigned int
bit tready;
bit rready;
uchar idata sendsbuf[4]=0;
uchar idata resbuf[4]=0;
uchar num,h,num1,ii,num2,d,e;
uchar num1,ii,num3;
float I4,ZKB,e1,e2;
bit readend;
bit sendend;
void pwm_init()
{
CCON=0;
CL=0;
CH=0;
CMOD=0x0b;//0x0a;
CCAPM0=0x42;
TMOD=0x10;
AUXR=0x01;
CR=1;
}
/*--------------------------
接收函数
---------------------------*/
void sre()
{
uchar p,j,q=0xff;
rready=0;
while(1)
{
p=0;
for(j=0;j<3;j++)
{
while(RI!=1);
RI=0;
resbuf[j]=SBUF;
p+=resbuf[j];
}
while(RI!=1)
RI=0;
q=SBUF;
if(q==p)
{
SBUF=0x00;
break;
}
else
{
SBUF=0xff;
while(TI!=0);
TI=0;
}
}
if(resbuf[3]=='v')
num1=resbuf[0]*100+resbuf[1]*10+resbuf[2];
else if(resbuf[0]=='s')
num3=1;
else if(resbuf[0]=='o')
num3=0;
else
num2=resbuf[0]+(float)(resbuf[1]/10)+(float)(resbuf[2]/100)+(float)(resbuf[3]/1000);
if(num2>1)
num2=1;
SM2=1;
ES=1;
readend=1;
}
/*--------------------------
发送函数
---------------------------*/
void str()
{
uchar p,j,q=0xff;
tready=0;
do
{
p=0;
for(j=0;j<3;j++)
{
SBUF=sendsbuf[j];
p+=sendsbuf[j];
while(TI!=1);
TI=0;
}
SBUF=p;
while(TI!=1);
TI=0;
while(RI!=1);
RI=0;
q=SBUF;
}while(q!=0);
SM2=1;
ES=1;
sendend=1;
}
void main(void)
{
TMOD=0x20;
AUXR=0x15;
IPH=0x18;
IP=0x10;
TL1=0xfd;
TH1=0xfd;
PCON=0x00;
TR1=1;
SCON=0xf0;
ES=1;
EA=1;
num3=0;
d=24;
I4=0.945;
P1M1=0xf7;
P1M0=0x08;
pwm_init();
while(1)
{
rready=1;
tready=1;
if((num1<=200&&num1!=0)&&num3==1)
{
e=num1;
e1=e+(float)(e)*2/1000;
e2=e-(float)(e)*2/1000;
if(e!=ii)
{
ii=e;
ZKB=1-(float)(e)/50+0.02;
ZKB*=256;
CCAP0H=CCAP0L=ZKB;
}
}
if(readend==1)
{
sendsbuf[0]=(uint)(I4);
sendsbuf[1]=(uint)(I4*10)%10;
sendsbuf[2]=(uint)(I4*100)%10;
sendsbuf[3]=(uint)(I4*1000)%10;
readend=0;
if(sendend==1)
{
sendend=0;
sendsbuf[0]=d/100;
sendsbuf[1]=d/10%10;
sendsbuf[2]=d%10;
sendsbuf[3]='v';
tready=1;
}
tready=1;
}
}
}
void ssio(void) interrupt 4
{
uchar sb;
P0=0xff;
RI=0;
ES=0; /*关串行口中断*/
if(SBUF!=0x10)
{
ES=1;
goto reti;
} /*非本机地址,继续监听*/
SM2=0; /* 取消监听状态 */
SBUF=0x30; /* 从本地址发回 */
while(TI!=1);
TI=0;
while(RI!=1);
RI=0;
if(RB8==1)
{
SM2=1;
ES=1;
goto reti;
}
sb=SBUF;
if(sb==0x0f)
{
if(rready==1)
SBUF=0x01;
else
SBUF=0x00;
while(TI!=1);
TI=0;
while(RI!=1);
RI=0;
if(RB8==1)
{
SM2=1;
ES=1;
goto reti;
}
sre();
}
else
{
if(sb==0x02)
{
if(tready==1)
SBUF=0x02;
else
SBUF=0x00;
while(TI!=1);
TI=0;
while(RI!=1);
RI=0;
if(RB8==1)
{
SM2=1;
ES=1;
goto reti;
}
str();
}
else
{
SBUF=0x08;
while(TI!=1);
TI=0;
SM2=1;
ES=1;
}
}
reti:;
}
|