📄 serial.c
字号:
//-----------------------------------------------------------------------------
// Net SERIAL.C
//
// This module handles RS-232 messages and associated tasks
//-----------------------------------------------------------------------------
#include "reg52.h"
#include "string.h"
#include "intrins.h"
#include "net.h"
#include "serial.h"
#include "absacc.h"
unsigned int CommRecLength;//串口接受数据长度,同时用作地址标注
bit CommRecFlag;//串口接收标志,接收到后置为1
bit CommRecBlock;
#define REC_BLOCK0 0x4000
#define REC_BLOCK1 0x5000
void init_serial(void)
{
SCON=0x50;//将串口设置为方式1,8位数据
PCON=0x80;//不进行2分频
TMOD=0x20;//定时器1设为模式2,
TL1=0xff;//设置波特率为57600=0xff,SMOD=1,51200=0x0,9600(TL1=0xFA,SMOD=1),5760
TH1=0xff;//设置波特率f6
PS=1;//串行中断优先级高
TR1=1;//启动定时器1
RI=0; // Clear HW_UART receive and transmit
TI=0; // complete indicators.
ES=1; // allow the serial interrupt
CommRecLength=0;
CommRecFlag=0;
CommRecBlock=0;
}
void Serial_Send_Ch(unsigned char ch)
{
// ES=0;
SBUF=ch;
while(!TI){}
TI=0;
// ES=1;
}
void SendCommString(unsigned char *base)
{
unsigned char length=strlen(base);
unsigned char i;
for(i=0;i<length;i++)Serial_Send_Ch(*(base+i));
}
void CommISR(void) interrupt 4
{
/* if (TI)
{
return ;
}*/
if (RI)
{
RI=0;
if(CommRecBlock)XBYTE[CommRecLength+REC_BLOCK0]=SBUF;
else XBYTE[CommRecLength+REC_BLOCK1]=SBUF;
CommRecLength++;
CommRecFlag=1;
}
}
//------------------------------------------------------------------------
// This function converts an integer to an ASCII string. It is a
// normally provided as a standard library function but the Keil
// libraries do not include it. Caution: The string passed to this
// must be at least 12 bytes long
//------------------------------------------------------------------------
char * itoa(UINT value, char * buf, UCHAR radix)
{
UINT i;
char * ptr;
char * temphold;
temphold = buf;
ptr = buf + 12;
*--ptr = 0; // Insert NULL char
do
{
// First create string in reverse order
i = (value % radix) + 0x30;
if(i > 0x39) i += 7;
*--ptr = i;
value = value / radix;
} while(value != 0);
// Next, move the string 6 places to the left
// Include NULL character
for( ; (*buf++ = *ptr++); );
return(temphold);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -