📄 protocol.c
字号:
#include <REG952.H>
#include <intrins.H>
#include "16C554.h"
#include "video_matrix.h"
#include "com_proto.h"
#include "uart.h"
#include "wdt.h"
extern cmdflag,cmdpos;
//*************************************************************************
//函数名: protocol
//功 能: 串口协议,实现串口扩展和视频矩阵切换
//入参数: cmd 为从串口得到的一个字符
//出参数: 无
//*************************************************************************
void protocol(unsigned char cmd)
{
uchar cmdlen,i,a,check_sum=0,command[SIZE];
uint *temp;
if(cmdflag == 0)
{
cmdpos = 0;
if(cmd == COM_START_CODE)
cmdflag = 1;
}
else
{
command[cmdpos++] = cmd;
if(cmdpos < SIZE) //表明数组没有溢出
{
if(cmdpos == command[0]) //即一串数据传送完毕
{
cmdlen = command[0];
for(i=4;i<cmdlen;i++)
{
check_sum += command[i-1];
}
temp = (unsigned int*)&command[1];
if(command[i-1] == check_sum)
{
switch(*temp) //为0003则为CMD_SwitchVideo 为0005则为CMD_UartData
{
case CMD_SwitchVideo:
{
matrix_write(command[3],command[4]); //对matrix进行输入输出配置或
matrix_write(command[5],command[4]); //命令配置
cmdflag = 0;
}
break;
case CMD_UartData:
{
uint j;
SC16C554_Write(command[3]|LCR,0x80); //设置UART1 波特率
SC16C554_Write(command[3]|DLL,DLL_DATA); //设置DLL 低位
SC16C554_Write(command[3]|DLM,DLM_DATA); //设置DLL 高位
SC16C554_Write(command[3]|LCR,0x03); //8位数据,1 位停止位,无奇偶校验位
SC16C554_Write(command[3]|FCR,0x07); //允许并复位FIFO
SC16C554_Write(command[3]|IER,0x00); //设置16C554禁止所有中断
// SC16C554_Write(command[3]|IER,0x02); //允许554发送终止中断Enable the transmitter empty interrupt
SC16C554_Write(command[3]|MCR,0x08); //打开554串口总开关
for(j=4;j<=cmdlen-2;j++) //从要选择的串口发送内容:从
{
a=SC16C554_Read(command[3]|LSR);
while(!a&(0x1<<5)); //等待THR空
SC16C554_Write(command[3]|THR, command[j]); //发送数据
}
cmdflag = 0;
}
break;
default:
{
cmdflag = 0;
}
}
}
else //若输入数据有错误
{
cmdflag = 0;
}
}
}
else //数组溢出
{
cmdflag = 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -