求教大神:
我用STC90C516RD+单片机编写了个小程序,写内部继电器R1,用的是松下的计算机链接协议, “%01#WCSR00011**CR”,用串口助手调试C程序是可以实现相关功能的,可是连上PLC好像就没有反馈信号了,是什么原因?
,还是说我设置错了……期待大神的解答,下面是我写的C程序:
#include <reg51.h>
typedef unsigned char uint8 ;
typedef unsigned int uint16 ;
sbit GPIO_KEY1 = P0^0 ;
sbit GPIO_KEY2 = P0^1 ;
sbit GPIO_LED1 = P0^2 ;
sbit GPIO_LED2 = P0^3 ;
sbit GPIO_LED3 = P0^4 ;
sbit GPIO_LED4 = P0^5 ;
sbit GPIO_LED5 = P0^6 ;
sbit GPIO_LED6 = P0^7 ;
//sbit DIR = P3^7 ;
bit key_flag1 , key_flag2 , read_flag , success_flag , fail_flag ;
//====================== 0 1 2 3 4 5 6 7 8 9 % # W C S R D * CR
uint8 code send_code[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x25,0x23,0x57,0x43,0x53,0x52,0x44,0x2A,0x0D};
uint8 num[16]; //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
uint8 num1[9];
uint8 a = 0 ;
uint8 temp , cnt ;
uint8 k;
extern void Uart_Init();
extern void IO_Init();
extern void key_scan();
extern void delay_ms(uint16 X);
extern void Control_PLC();
void main ()
{
Uart_Init();
IO_Init();
while(1)
{
Control_PLC();
}
}
/*****串口初始化*****/
void Uart_Init()
{
PCON = 0x00 ; //波特率不加倍
TMOD = 0x20 ; //定时器1,工作方式2,8位自动重装初值
TH1 = 0xfd ; //晶振频率11.0592MHZ,波特率9600bps
TL1 = 0xfd ;
SCON = 0x50 ; //串口工作模式1,允许串行口接收
TR1 = 1 ; //开定时器1
ES = 1 ; //开串行口中断
EA = 1 ; //开全局中断
}
/*****IO初始化*****/
void IO_Init()
{
GPIO_LED1 = 0 ;
GPIO_LED2 = 0 ;
GPIO_LED3 = 0 ;
GPIO_LED4 = 0 ;
GPIO_LED5 = 0 ;
GPIO_LED6 = 0 ;
num[0]=10; //%
num[1]=0;
num[2]=1;
num[3]=11; //#
num[4]=12; //w
num[5]=13; //c
num[6]=14; //s
num[7]=15; //r
num[8]=0;
num[9]=0;
num[10]=0;
num[11]=1;
num[13]=17; //*
num[14]=17; //*
num[15]=18; //cr
}
/*****1ms延时*****/
void delay_ms(uint16 X)
{
uint8 i ;
while(X--)
{
for(i=0;i<120;i++) ;
}
}
/*****独立键盘扫描*****/
void key_scan()
{
uint8 i ;
if(GPIO_KEY1 == 0)
{
delay_ms(10);
if(GPIO_KEY1 == 0)
{
key_flag1 = 1 ;
}
i = 0;
while ((i<50) && (GPIO_KEY1 != 1)) //检测按键是否松开
{
delay_ms(10);
i++;
}
}
if(GPIO_KEY2 == 0)
{
delay_ms(10);
if(GPIO_KEY2 == 0)
{
key_flag2 = 1 ;
}
i = 0;
while ((i<50) && (GPIO_KEY2 != 1)) //检测按键是否松开
{
delay_ms(10);
i++;
}
}
}
/*****写PLC*****/
void Control_PLC()
{
key_scan();
if(key_flag1==1)
{
// DIR = 1 ;
key_flag1=0;
num[12]=1;
ES = 1 ;
for(k=0;k<16;k++)
{
SBUF = send_code[num[k]] ;
while(!TI) ;
TI = 0 ;
}
// DIR = 0 ;
}
if(key_flag2==1)
{
// DIR = 1 ;
key_flag2=0;
num[12]=0;
ES = 1 ;
for(k=0;k<16;k++)
{
SBUF = send_code[num[k]] ;
while(!TI);
TI = 0 ;
}
// DIR = 0 ;
}
if(success_flag)
GPIO_LED1 = 1 ;
if(fail_flag)
GPIO_LED2 = 1 ;
}
/*****通过中断接收响应并判断通讯是否成功*****/
void Uart_Interrupt () interrupt 4
{
EA = 0 ; //关闭全局中断,避免接收过程受到干扰
if(RI)
{
// DIR=0 ;
RI = 0 ;
temp=SBUF;
SBUF=temp;
GPIO_LED3 = 1;
switch(cnt)
{
case 0:num1[0]=temp;if(num1[0]==0x25){cnt=1;read_flag=0;}else cnt=0 ; break;
case 1:num1[1]=temp;cnt=2; break;
case 2:num1[2]=temp;cnt=3; break;
case 3:num1[3]=temp;cnt=4; break;
case 4:num1[4]=temp;cnt=5; break;
case 5:num1[5]=temp;cnt=6; break;
case 6:num1[6]=temp;cnt=7; break;
case 7:num1[7]=temp;cnt=8; break;
case 8:num1[8]=temp;if(num1[8]==0x0d) {cnt=0;read_flag=1;P2=0;} break;
}
if(read_flag)
{
GPIO_LED4 = 1;
read_flag=0;
if(num1[3]==0x24) //返回有“$”,即正常的应答
{
GPIO_LED5 = 1;
success_flag=1;
ES=0; //关闭串行口中断,避免干扰信息
}
else //if(num1[3]==0x21) 返回有“!”,即出现错误
{
GPIO_LED6 = 1;
fail_flag=1;
}
}
}
EA = 1 ;
}