标题: 51单片机无线模块 [打印本页]

作者: 愤怒的蜗牛    时间: 2017-9-16 15:42
标题: 51单片机无线模块
#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字符串发送函数=====================================
函数名:uart1_send_str
功能介绍:发送字符串
函数参数:*s :发送的字符串首地址
返回值:无
===============================================================================================*/
void uart1_send_str(unsigned char *s) {   
   ES=0;
    while(*s!='\0') {
  SBUF=*s;
  while(!TI);
  TI=0;
  s++;
}
ES=1;
return;
}
/**********************************************
函数功能;串口1接收客户端发来的数据
数据格式:前:Direction:run!  后:Direction:back! 左:Direction:left! 右:Direction:right!
          速度:Speed:xx     舵机角度: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字符串发送函数=====================================
函数名:uart1_send_str
功能介绍:发送字符串
函数参数:*s :发送的字符串首地址
返回值:无
===============================================================================================*/
void uart2_send_str(unsigned char *s) {   
   IE2&=0xfe;
    while(*s!='\0') {
  S2BUF=*s;
  while(!(S2CON&0x02));
  S2CON&=0xfd;
  s++;
}
  IE2|=0x01;
return;
}
/**********************************************
函数功能;串口2接收客户端发来的数据
数据格式:前:Direction:run!  后:Direction:back! 左:Direction:left! 右:Direction:right!
          速度:Speed:xx     舵机角度: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;
}






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1