⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serial.c

📁 一个分选系统的软件:用SmallRtos操作系统
💻 C
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************************************************
**--------------文件信息--------------------------------------------------------------------------------
**文   件   名: serial.c
**创   建   人: 刘宝贵
**最后修改日期: 2004年4月20日
**描        述: 串行口驱动
**
**--------------历史版本信息----------------------------------------------------------------------------
** 创建人: 刘宝贵
** 版  本: V1.00
** 日 期: 2004年4月20日
** 描 述: 原始版本
**
**------------------------------------------------------------------------------------------------------
** 修改人:
** 版  本:
** 日 期:
** 描 述:
**
**------------------------------------------------------------------------------------------------------
**--------------当前版本修订------------------------------------------------------------------------------
** 修改人: 刘宝贵
** 日 期: 2004年4月20日
** 描 述: 
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/

#include "config.h"

uint8 OS_Q_MEM_SEL NotUse _at_ 0x0000;      //指针的NULL为0,这个变量占用0地质避免出现有效的NULL指针
uint8 OS_Q_MEM_SEL SerialInData[130];        //给读串口消息队列分配的队列空间
uint8 OS_Q_MEM_SEL SerialOutData[200];       //给写串口消息队列分配的队列空间
void PutChar(uint8 Data);                   //发送一个字节
void Send(uint8 Data);                      //发送一个数据包
uint8 GetCharWithCheck(void);               //从读串口消息队列取一字节,并且对该字节校验

bit SerialCanSend = 1;
bit CommSending = 0;
//uint8 data test;
uint8 data inchk;          //校验和
uint8 data outchk;         //校验和
uint8 data cid;            //命令码
uint8 data rtn;            //返回码  RTN值  表示意义
uint8 xdata buf[130];      // 缓冲串口数据     
#define RIGHT         1    //         01H	正常
#define CHKSUM_ERROR  2    //         02H	CHKSUM 错
#define LENGTH_ERROR  3    //         03H	LENGTH 错
#define CID_ERROR     4    //         04H	CID 无效
#define TYPE_ERROR    5    //         05H	命令格式错
#define DATA_ERROR    6    //         06H	无效数据
#define LOGIC_ERROR   7    //         07H	逻辑错

#define OTHER_ERROR   0XE0 //         E0H~EFH	其他错误
/*********************************************************************************************************
** 函数名称: PutChar
** 功能描述: 发送一个字节
** 输 入: Data:发送的数据
** 输 出: 无
** 全局变量: 无
** 调用模块: 无
**
** 作 者: 陈明计
** 日 期: 2002年12月4日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void PutChar(uint8 Data)
{
    OS_ENTER_CRITICAL();
    if (SerialCanSend == 1)
    {
        SerialCanSend = 0;
        SBUF0 = Data;
    }
    else
    {
        OSQIntPost(SerialOutData,Data);
    }
    OS_EXIT_CRITICAL();
}  

/*********************************************************************************************************
** 函数名称: PutCharWithcheck
** 功能描述: 发送一个字节同时校验
** 输 入: Data:发送的数据
** 输 出: 无
** 全局变量: chk
** 调用模块: PutChar
**
** 作 者: 刘宝贵
** 日 期: 2004年4月21日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void PutCharWithCheck(uint8 Data)
{
    outchk ^= Data ;
    PutChar (Data);
}  

/*********************************************************************************************************
** 函数名称: CommSendInfo
** 功能描述: 发送一个报文
** 输   入: 无
** 输   出: 无
** 全局变量: address cid rtn chk
** 调用模块: OSQCreate PutCharWithCheck PutChar
**
** 作 者: 刘宝贵
** 日 期: 2004年4月25日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void CommSendInfo(void)
{
    OSQCreate(SerialOutData,200);
    outchk = 0;
    PutCharWithCheck(0xeb);
    PutCharWithCheck(0x90);
    PutCharWithCheck(0xeb);
    PutCharWithCheck(0x90);
    PutCharWithCheck(Address);
    PutCharWithCheck(cid);
    PutCharWithCheck(1);
    PutCharWithCheck(rtn);
    PutChar(outchk);
    PutChar(0x0d);
}
/*********************************************************************************************************
** 函数名称: CommSendParaInfo
** 功能描述: 发送参数报文
** 输   入: 无
** 输   出: 无
** 全局变量: address cid rtn outchk 
** 调用模块: OSQCreate PutCharWithCheck PutChar
**
** 作 者: 刘宝贵
** 日 期: 2004年4月25日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void CommSendParaInfo(void)
{
    OSQCreate(SerialOutData,200);
    outchk = 0;
    PutCharWithCheck(0xeb);
    PutCharWithCheck(0x90);
    PutCharWithCheck(0xeb);
    PutCharWithCheck(0x90);
    PutCharWithCheck(Address);
    PutCharWithCheck(cid);
    PutCharWithCheck(4);
    PutCharWithCheck(rtn);
    PutCharWithCheck(buf[0]);
    if(OSSemPend(IICSem,10)==OS_SEM_OK)
    {
        PutCharWithCheck( EepromReadByteA(CellParaIICBase+buf[0]*2) );
        PutCharWithCheck( EepromReadByteA(CellParaIICBase+buf[0]*2+1));
        OSSemPost(IICSem);
    }
    PutChar(outchk);
    PutChar(0x0d);
}
/*********************************************************************************************************
** 函数名称: CommSendCellDataInfo
** 功能描述: 发送电池数据报文
** 输   入: 无
** 输   出: 无
** 全局变量: address cid rtn outchk cell.u cell.i cell.c cell.s
** 调用模块: OSQCreate PutCharWithCheck PutChar
**
** 作 者: 刘宝贵
** 日 期: 2004年4月27日
**-------------------------------------------------------------------------------------------------------
** 修改人: 刘宝贵
** 日 期: 2004年4月29日22:17
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/

void CommSendCellDataInfo (void)
{
    uint8 data i;
    OSQCreate(SerialOutData,200);
    outchk = 0;
    OS_ENTER_CRITICAL();
    PutCharWithCheck(0xeb);
    PutCharWithCheck(0x90);
    PutCharWithCheck(0xeb);
    PutCharWithCheck(0x90);
    PutCharWithCheck(Address);
    PutCharWithCheck(cid);
    PutCharWithCheck(24*7+1);
    PutCharWithCheck(rtn);
    for (i=0;i<24;i++)
    {
        uint16 data temp;
        OS_ENTER_CRITICAL();
        temp=Cell[i].u;
        OS_EXIT_CRITICAL();
        PutCharWithCheck((uint8)(temp>>8));
        PutCharWithCheck((uint8)(temp));
    }
    for (i=0;i<24;i++)
    {
        uint16 data temp;
        OS_ENTER_CRITICAL();
        temp=Cell[i].i;
        OS_EXIT_CRITICAL();
        PutCharWithCheck((uint8)(temp>>8));
        PutCharWithCheck((uint8)(temp));
    }
    for (i=0;i<24;i++)
    {
        uint32 data temp;
        OS_ENTER_CRITICAL();
        temp = Cell[i].c;
        OS_ENTER_CRITICAL();
        temp/= 36000;     //0.1mAsecond--->mAhour
        PutCharWithCheck((uint8)(temp>>8));
        PutCharWithCheck((uint8)(temp));
    }
    if(CurrentWorkStepMax < CurrentWorkStep)
    {
        CurrentWorkStepMax = CurrentWorkStep;
    }
    for (i=0;i<24;i++)
    {
        PutCharWithCheck(Cell[i].s | CurrentWorkStepMax<<4);
    }
    PutChar(outchk);
    PutChar(0x0d);
    OS_ENTER_CRITICAL();
}

/*********************************************************************************************************
** 函数名称: CommSendFenRongInfo(uint16 referenceu)
** 功能描述: 发送分容信息报文
** 输   入: 无
** 输   出: 无
** 全局变量: address cid rtn outchk 
** 调用模块: OSQCreate PutCharWithCheck PutChar 
**
** 作 者: 刘宝贵
** 日 期: 2004年10月26日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void CommSendFenRongInfo(uint16 refeu)
{
    uint8 data i;
    OSQCreate(SerialOutData,200);
    outchk = 0;
    PutCharWithCheck(0xeb);
    PutCharWithCheck(0x90);
    PutCharWithCheck(0xeb);
    PutCharWithCheck(0x90);
    PutCharWithCheck(Address);
    PutCharWithCheck(cid);
    PutCharWithCheck(1+48+2);
    PutCharWithCheck(rtn);
    for(i=SumWorkStep;i>0;i--)
    {
        if(WorkStep[i].type == DISCHARGE)
        {
            PutCharWithCheck((uint8)(WorkStep[i].i>>8));
            PutCharWithCheck((uint8)(WorkStep[i].i));
            break;
        }
        else if(i==1)
        {
            PutCharWithCheck(0);
            PutCharWithCheck(0);
        }
    }
    for(i=0;i<24;i++)
    {
        OSSemPend(IICSem,0);
        PutCharWithCheck( EepromReadByte( ReferenceUTimeIICBase + ((3800-refeu)/100)*48 + i*2) );
        PutCharWithCheck( EepromReadByte( ReferenceUTimeIICBase + ((3800-refeu)/100)*48 + i*2 + 1) );
        OSSemPost(IICSem);
    }
    PutChar(outchk);
    PutChar(0x0d);
}
/*********************************************************************************************************
** 函数名称: GetCharWithCheck
** 功能描述: 从读串口消息队列取一字节,并且对该字节校验
** 输   入: 无
** 输   出: temp:从串口消息队列取的字节
** 全局变量: inchk:校验和
** 调用模块: OSQPend
**
** 作   者: 刘宝贵
** 日   期: 2004年4月21日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
uint8 GetCharWithCheck(void)
{
    uint8 data temp;
    OSQPend(&temp,SerialInData,0);     /* 接收一个字节       */
    inchk^=temp;
    return(temp);
}  

/*********************************************************************************************************
** 函数名称: GetCharWithoutCheck
** 功能描述: 从读串口消息队列取一字节
** 输   入: 无
** 输   出: temp:从串口消息队列取的字节
** 全局变量: 无
** 调用模块: OSQPend
**
** 作   者: 刘宝贵
** 日   期: 2004年4月21日
**-------------------------------------------------------------------------------------------------------
** 修改人:
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
uint8 GetCharWithoutCheck(void)
{
    uint8 data temp;
    OSQPend(&temp,SerialInData,0);     /* 接收一个字节       */
    return(temp);
}  

/*********************************************************************************************************
** 函数名称: TaskReceive
** 功能描述: 串口接收处理任务
** 输   入: 无
** 输   出: 无                     
** 全局变量: 无
** 调用模块: 无
**
** 作   者: 刘宝贵
** 日   期: 2004年4月20日
**-------------------------------------------------------------------------------------------------------
** 修改人:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -