|
#include <stc15f2k60s2.h>
#include <intrins.h>
#include "uart.h"
unsigned char serial_data[400];
bit flag_recful=0;
//******************************************************************************************************
//串口初始化 9600波特率 串口1
//*******************************************************************************************************
void Uart_Init()
{
SCON = 0x50;
AUXR |= 0x04;
T2L = 0xE0;
T2H = 0xFE;
AUXR |= 0x01;
AUXR |= 0x10;
//IP=0x10;
ES=1;
RI=0;
EA=1;
}
/*=========================================串口1字符串發(fā)送函數(shù)=====================================
函數(shù)名:uart1_send_str
功能介紹:發(fā)送字符串
函數(shù)參數(shù):*s :發(fā)送的字符串首地址
返回值:無(wú)
===============================================================================================*/
void uart1_send_str(unsigned char *s) {
ES=0;
while(*s!='\0') {
SBUF=*s;
while(!TI);
TI=0;
s++;
}
ES=1;
return;
}
/**********************************************
函數(shù)功能;串口1接收客戶端發(fā)來(lái)的數(shù)據(jù)
數(shù)據(jù)格式:前:Direction:run! 后:Direction:back! 左:Direction:left! 右:Direction:right!
速度:Speed:xx 舵機(jī)角度:angle:xx
/**********************************************/
void UART_Interrupt_Receive(void) interrupt 4
{
static unsigned int i=0;
if(RI==1)
{
RI = 0;
serial_data[i] = SBUF;
i++;
if(serial_data[i-1]=='>' || serial_data[i-1]=='!'|| serial_data[i-1]=='K'|| serial_data[i-1]=='R'|| serial_data[i-1]=='k')
{
flag_recful=1;
serial_data[i]='\0';
i=0;
}
}
return;
}
//******************************************************************************************************
//串口初始化 9600波特率 串口2
//*******************************************************************************************************
void Uart_Init2(void) //9600bps@11.0592MHz
{
S2CON = 0x50;
AUXR |= 0x04;
T2L = 0xE0;
T2H = 0xFE;
AUXR |= 0x10;
IE2&=0xfe;
IE2|=0x01;
S2CON|=0x01;
}
/*=========================================串口1字符串發(fā)送函數(shù)=====================================
函數(shù)名:uart1_send_str
功能介紹:發(fā)送字符串
函數(shù)參數(shù):*s :發(fā)送的字符串首地址
返回值:無(wú)
===============================================================================================*/
void uart2_send_str(unsigned char *s) {
IE2&=0xfe;
while(*s!='\0') {
S2BUF=*s;
while(!(S2CON&0x02));
S2CON&=0xfd;
s++;
}
IE2|=0x01;
return;
}
/**********************************************
函數(shù)功能;串口2接收客戶端發(fā)來(lái)的數(shù)據(jù)
數(shù)據(jù)格式:前:Direction:run! 后:Direction:back! 左:Direction:left! 右:Direction:right!
速度:Speed:xx 舵機(jī)角度:angle:xx
/**********************************************/
void UART_Interrupt_Receive2(void) interrupt 8
{
static unsigned int j=0;
if(S2CON&0x01)
{
S2CON&=0xfe;
serial_data[j] = S2BUF;
j++;
if(serial_data[j-1]=='>' || serial_data[j-1]=='!'|| serial_data[j-1]=='K'|| serial_data[j-1]=='z')
{
flag_recful=1;
serial_data[j]='\0';
j=0;
}
}
return;
}
|
|