📄 comm.c
字号:
/*********************************************************************************************************
**相控充电机主控器系统 软件设计:刘宝贵,硬件设计:刘宝贵 调试:刘宝贵
**本系统使用编译器为 keil c51.exe V7.06
**本系统使用汇编器为 keil A51.exe V7.07
**本系统使用联接器为 keil BL51.exe V5.03
**本系统使用 RTX_TINY
**如果等程序有问题或者是Bug请与作者联系
**与作者联系方法:邮箱:baoguiliu@163.com,baoguiliu@sohu.com 电话:0451-6677970-6738
**
**
**VER1.0
**--------------文件信息--------------------------------------------------------------------------------
**文 件 名: Comm.C
**创 建 人: 刘宝贵
**最后修改日期:
**描 述: 通信处理
**
**--------------历史版本信息----------------------------------------------------------------------------
** 修改人:
** 版 本:
** 日 期:
** 描 述:
**
**--------------当前版本修订------------------------------------------------------------------------------
** 修改人:
** 日 期:
** 描 述:
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#include"includes.h"
sbit COMMEN = P1^4;
#define COMMEN_OFF 1
#define COMMEN_ON 0
#define RIGHT 1 //正常
#define CHKSUM_ERROR 2 //CHKSUM 错
#define CID_ERROR 3 //CID 无效
#define TYPE_ERROR 4 //命令格式错
#define DATA_ERROR 5 //无效果数据
#define LOGIC_ERROR 6 //逻辑错
#define OTHER_ERROR 0XE0 //其他错误
uint8 code CommHead[4] = {0xEB,0x90,0xEB,0x90};
/*********************************************************************************************************
** 函数名称: comm
** 功能描述: 串口中断处理程序
** 输 入: 无
** 输 出: 无
** 全局变量: 无
** 调用模块: 无
**
** 作 者: 刘宝贵
** 日 期: 2004年4月20日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void comm(void) interrupt 4
{
if (RI == 1)
{
uint8 idata RxSbuf;
RxSbuf = SBUF;
RI = 0;
switch(CommState)
{
case 0:
case 1:
case 2:
case 3://接收报文头
{
if(RxSbuf == CommHead[CommState])
{
CommState++;
}
else
{
CommState = 0;
}
}break;
case 4://接收地址码
{
if(RxSbuf == Address)
{
CommState++;
}
else
{
CommState = 0;
}
}break;
case 5://接收命令码
{
CommState++;
CommCid = RxSbuf;
}break;
case 6://接收数据区长度
{
CommState++;
CommRxLength = RxSbuf;
CommRxPointer = 0;
if(CommRxLength >= RX_BUF_MAX-1)//如果数据包长度过长则不接收
{
CommState = 0;
}
if(CommRxLength == 0)
{
CommState++;
}
}break;
case 7://接收数据区
{
CommRxBuf[CommRxPointer] = RxSbuf;
CommRxPointer++;
if(CommRxPointer == CommRxLength)//数据区接收完,转下一状态
{
CommState++;
}
}break;
case 8://接收效验和
{
CommChkIn = RxSbuf;
CommState++;
}break;
case 9://接收报文尾
{
CommState = 0;
CommRxBuf[CommRxLength] = RxSbuf;
CommNewMessage = 1;
}break;
}
}//处理通信接收
if (TI == 1)
{
TI = 0;
if(CommTxPointer < CommTxLength)
{
SBUF = CommTxBuf[CommTxPointer];
CommTxPointer++;
}
else
{
COMMEN = COMMEN_OFF;
}
CommNewMessage = 0;
}
}
/*********************************************************************************************************
** 函数名称: PutChar
** 功能描述: 发送一个字节到串口缓冲区
** 输 入: Data:发送的数据
** 输 出: 无
** 全局变量: 无
** 调用模块: 无
**
** 作 者: 刘宝贵
** 日 期: 2006年4月26日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void PutChar(uint8 SendData)
{
if(CommTxPointer < TX_BUF_MAX)
{
CommTxBuf[CommTxPointer++] = SendData;
}
}
/*********************************************************************************************************
** 函数名称: PutCharWithcheck
** 功能描述: 发送一个字节同时校验
** 输 入: Data:发送的数据
** 输 出: 无
** 全局变量: chk
** 调用模块: PutChar
**
** 作 者: 刘宝贵
** 日 期: 2004年4月21日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void PutCharWithCheck(uint8 SendData)
{
CommChkOut ^= SendData ;
PutChar (SendData);
}
/*********************************************************************************************************
** 函数名称: CommSendInfo
** 功能描述: 发送一个报文
** 输 入: 无
** 输 出: 无
** 全局变量: address CommCid ReturnCode chk
** 调用模块: OSQCreate PutCharWithCheck PutChar
**
** 作 者: 刘宝贵
** 日 期: 2004年4月25日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void CommSendInfo(void)
{
CommTxPointer = 0;
CommTxLength = 9;
CommChkOut = 0xEB;
PutCharWithCheck(0x90);
PutCharWithCheck(0xEB);
PutCharWithCheck(0x90);
PutCharWithCheck(Address);
PutCharWithCheck(CommCid);
PutCharWithCheck(1);
PutCharWithCheck(ReturnCode);
PutChar(CommChkOut);
PutChar(0x0D);
CommTxPointer = 0;
COMMEN = COMMEN_ON;
os_wait(K_TMO,20/OS_MSEL_PER_TICKS,0);
SBUF = 0xEB;//启动发送
}
/*********************************************************************************************************
** 函数名称: CommSendParaInfo
** 功能描述: 发送参数报文
** 输 入: 无
** 输 出: 无
** 全局变量: address CommCid ReturnCode outchk
** 调用模块: OSQCreate PutCharWithCheck PutChar
**
** 作 者: 刘宝贵
** 日 期: 2004年4月25日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void CommSendParaInfo(void)
{
uint16 idata address_temp;
CommTxPointer = 0;
CommTxLength = 12;
CommChkOut = 0xEB;
PutCharWithCheck(0x90);
PutCharWithCheck(0xEB);
PutCharWithCheck(0x90);
PutCharWithCheck(Address);
PutCharWithCheck(CommCid);
PutCharWithCheck(4);
PutCharWithCheck(ReturnCode);
PutCharWithCheck(CommRxBuf[0]);//CommRxBuf[0]内容为参数序号
switch(CommRxBuf[0])
{
case 0:address_temp = E2Uk ;break;
case 1:address_temp = E2Ub ;break;
case 2:address_temp = E2Ik ;break;
case 3:address_temp = E2Ib ;break;
case 4:address_temp = E2DAk;break;
case 5:address_temp = E2DAb;break;
case 6:address_temp = E2Tk ;break;
case 7:address_temp = E2Tb ;break;
default:address_temp = 0;
}
PutCharWithCheck( EepromReadByteA(address_temp) );
PutCharWithCheck( EepromReadByteA(address_temp+1) );
PutChar(CommChkOut);
PutChar(0x0D);
CommTxPointer = 0;
COMMEN = COMMEN_ON;
os_wait(K_TMO,20/OS_MSEL_PER_TICKS,0);
SBUF = 0xEB;//启动发送
}
/*********************************************************************************************************
** 函数名称: CommSendParaInfo
** 功能描述: 发送指定工步信息报文
** 输 入: 无
** 输 出: 无
** 全局变量: address CommCid ReturnCode outchk
** 调用模块: OSQCreate PutCharWithCheck PutChar
**
** 作 者: 刘宝贵
** 日 期: 2006年10月25日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void CommSendWorkStepInfo(void)
{
uint8 idata ProgramNoTemp;
uint8 idata StepTemp;
uint8 xdata ModeTemp;
uint16 xdata SetupITemp;
uint16 xdata SetupUTemp;
uint16 xdata JumpTimeMTemp;
uint16 xdata JumpUTemp;
uint16 xdata JumpITemp;
uint16 xdata JumpQTemp;
ProgramNoTemp = CommRxBuf[0];
StepTemp = CommRxBuf[1];
ModeTemp = EepromReadByte(ProgramNoTemp*600 + StepTemp*20 + 2*0);
// LoopTimes = EepromReadByte(ProgramNoTemp*600 + StepTemp*20 + 2*1);
SetupITemp = EepromReadWord(ProgramNoTemp*600 + StepTemp*20 + 2*2);
SetupUTemp = EepromReadWord(ProgramNoTemp*600 + StepTemp*20 + 2*3);
JumpTimeMTemp = EepromReadWord(ProgramNoTemp*600 + StepTemp*20 + 2*5);
JumpUTemp = EepromReadWord(ProgramNoTemp*600 + StepTemp*20 + 2*6);
JumpQTemp = EepromReadWord(ProgramNoTemp*600 + StepTemp*20 + 2*7);
CommTxPointer = 0;
CommTxLength = 24;
CommChkOut = 0xEB;
PutCharWithCheck(0x90);
PutCharWithCheck(0xEB);
PutCharWithCheck(0x90);
PutCharWithCheck(Address);
PutCharWithCheck(CommCid);
PutCharWithCheck(16);
PutCharWithCheck(ReturnCode);
PutCharWithCheck(ProgramNoTemp);
PutCharWithCheck(StepTemp);
PutCharWithCheck(ModeTemp);
PutCharWithCheck((unsigned char)(SetupUTemp>>8));PutCharWithCheck((unsigned char)SetupUTemp);
PutCharWithCheck((unsigned char)(SetupITemp>>8));PutCharWithCheck((unsigned char)SetupITemp);
PutCharWithCheck((unsigned char)(JumpTimeMTemp>>8));PutCharWithCheck((unsigned char)JumpTimeMTemp);
PutCharWithCheck((unsigned char)(JumpUTemp>>8));PutCharWithCheck((unsigned char)JumpUTemp);
PutCharWithCheck((unsigned char)(JumpITemp>>8));PutCharWithCheck((unsigned char)JumpITemp);
PutCharWithCheck((unsigned char)(JumpQTemp>>8));PutCharWithCheck((unsigned char)JumpQTemp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -