📄 rs232.c
字号:
#include "oaks_sfr.h"
#include "rs232.h"
/*function define*/
void receive(); /* Receive interruption function */
#pragma INTERRUPT receive
/*UART communicate sign*/
#define TRANS_SIGN 'T'
#define READY_SIGN 'K'
#define FINISH_SIGN 'F'
/*UART state */
#define RECEIVING 0xff
#define WATINGSIZE_H 0x0f
#define WATINGSIZE_L 0x10
#define READY 0x01
#define WATING 0x00
#define TRANSING 0x20
#define WATINGTRANS 0x21
/*Globle variable define*/
static unsigned char g_FrameOk; // /*是否接收到一个数据帧*/
static unsigned char g_UartStat; ///*是否正在从串口接受数据接受*/
static unsigned int g_FrameCount; // /*已接收数据包大小*/
static unsigned int g_FrameSize; // /*接收到的数据包大小*/
unsigned char g_RecvFrame[MAX_FRAME_SIZE]; // /*接受缓存*/
static unsigned int g_Tmp;
static unsigned char g_Led;
#define DELAY(x) g_Tmp=x;while(g_Tmp--)
#define SEND_BYTE(b) u0tb=b;while(!ti_u0c1) //串口发送一字节数据
void Rs232Init()
{
u0mr = 0x05; /* Transmit/Receive Mode register*/
/* asynchronous, 8bit,non-parity */
/* non-sleep */
u0c0 = 0x10; /* UART0 Transmit/Receive Control register0 f1 is chosen*/
u0brg = UART_SPEED; /* UART0 Bit Rate generator */
s0ric = 0x06; /* Set interruption level */
u0c1 = 0x05; /* UART0 Transmit/Receive Control register1 Set to start transmit/receive */
/* _asm( "\tFSET I"); Set interruption enable */
g_FrameOk=NULL; /*初始化全局变量*/
g_UartStat=WATING;
g_FrameCount=NULL;
g_FrameSize=NULL;
g_Led=0x01;
}
/**************************************************************************
* Transmit/Receive Interruption function : receive()
**************************************************************************/
void receive()
{
unsigned char rdata; /* Receive buffer */
rdata = u0rb; /* Receiving data */
/*rdata &= 0x0ff; Transmitting received data
u0tb = rdata;*/
//*根据全局变量标识 确定对接收到的数据的处理*/
switch(g_UartStat)
{//*接受状态时继续接收一个字节数据*/
// case RECEIVING:
/*g_FrameCount++; 计数已接收数据的全局变量*/
/*if(g_FrameCount==g_FrameSize) 接受到对方指定数量的数据后停止*/
// g_FrameOk=1;
// break;
//*接受数据帧大小的量 的高8位*/
case WATINGSIZE_H:
g_FrameSize=rdata;
g_FrameSize=g_FrameSize<<8;
g_UartStat=WATINGSIZE_L;
break;
//*接受数据帧大小的量 的低8位*/
case WATINGSIZE_L:
g_FrameSize |=rdata;
g_UartStat=RECEIVING;
break;
//*等待中*/
case WATING:
if(rdata==TRANS_SIGN) /*收到发送请求 */
{
g_UartStat=WATINGSIZE_H;
g_FrameCount=NULL;
/*反馈信号 标识ok */
SEND_BYTE(READY_SIGN);
}
break;
/*发送状态时 等待对方的ok信息*/
case WATINGTRANS:
if(rdata==READY_SIGN)
{
g_UartStat=TRANSING;//ok 开始传送 和sendframe函数配合
}
break;
}
}
int RecvFrame(unsigned char **buf)/*接收数据帧*/
{
g_FrameOk=NULL;
/*g_FrameCount=NULL;
g_UartStat=WATING;*/
while(g_UartStat!=RECEIVING)
;
s0ric = 0x00;
for(;g_FrameCount<g_FrameSize;g_FrameCount++)
{
while(!ri_u0c1)
;
g_RecvFrame[g_FrameCount]=u0rb;
/*rdata = u0rb; Receiving data */
}
/*while(!g_FrameOk)在数据帧接受完毕之前 处于等待状态*/
g_UartStat=WATING;
s0ric = 0x06;
*buf=g_RecvFrame;
return g_FrameSize;
}
int SendFrame(const unsigned char *buf,int len)
{
int i;
unsigned char tmp;
g_UartStat=WATINGTRANS;
g_FrameOk=NULL;
SEND_BYTE(TRANS_SIGN);
while(g_UartStat!=TRANSING)/*收到ok标识之前等待*/
;
tmp=len>>8; //发送数据帧大小 先高位后低位
SEND_BYTE(tmp);
tmp=len;
SEND_BYTE(tmp);
for(i=0;i<len;i++)/*发送数据帧*/
{
SEND_BYTE(buf[i]);
//DELAY(500);
}
g_UartStat=WATING;
p7=0xff^g_Led;
g_Led=g_Led<<1;
if(g_Led==0)
g_Led=0x01;
return len;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -