我是用的15单片机,串口一连接的蓝牙模块,串口二连接的红外遥控模块。
想实现,用手上的串口助手APP,给蓝牙模块发指令,串口一会接收到不同的六个指令,根据六个不同的指令,进而用串口二发出六组每组四个十六进制数的指令。下面是我的源代码,不知道为什么一直不好用。
#include <STC15F2K60S2.H>
#include <intrins.h>
#include <math.h> //Keil library
#include <stdio.h> //Keil library
#define uchar unsigned char
#define uint unsigned int
#define FOSC 11059200L //系统频率
#define BAUD 9600 //串口2波特率
#define S2RI 0x01 //S2CON.0 不能进行位寻址
#define S2TI 0x02 //S2CON.1
int code table1[]={0xff,0x02,0x00,0xfe};
int code table2[]={0xff,0x02,0x01,0xfe};
int code table3[]={0xff,0x02,0x02,0xfe};
int code table4[]={0xff,0x02,0x03,0xfe};
int code table5[]={0xff,0x02,0x04,0xfe};
int code table6[]={0xff,0x02,0x05,0xfe};
bit flag_KG=0,flag_LF=0,flag_FS=0,flag_DS=0,flag_FL=0,flag_BF=0;
bit busy;
uchar uart1_buff=0,uart2_buff=0,sending=1;
void Uart1_Init()
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xE0; //设定定时初值
TH1 = 0xFE; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
ES = 1; //开中断.
EA = 1;
}
//----------------------------------------------
void Uart2_Init(void)
{
S2CON = 0x50; //8位可变波特率
T2L = (65536 - (FOSC/4/BAUD)); //设置波特率重装值
T2H = (65536 - (FOSC/4/BAUD))>>8;
AUXR = 0x14; //T2为1T模式, 并启动定时器2
IE2 = 0x01; //使能串口2中断
EA = 1;
}
void Out_Data(int Data[])
{
uchar i;
for (i=0;i<=3;i++)//
{
uart2_buff=Data[i];
S2BUF = uart2_buff;
if (S2CON & S2TI)
{
S2CON &= ~S2TI; //清除S2TI位
}
}
sending=0;
}
void serial() interrupt 4
{
if(RI==1)
{
RI = 0; //清除串行接受标志位
uart1_buff = SBUF; //从串口缓冲区取得数据
switch(uart1_buff)
{
case 0x31: flag_KG=1;break;
case 0x32: flag_LF=1;break;
case 0x33: flag_FS=1;break;
case 0x34: flag_DS=1;break;
case 0x35: flag_FL=1;break;
case 0x36: flag_BF=1;break;
}
}
}
void main()
{
Uart1_Init();
Uart2_Init();
while(1)
{
if(flag_KG==1)
{
Out_Data(table1);
while(sending);
sending=1;
flag_KG=0;
}
if(flag_LF==1)
{
Out_Data(table2);
while(sending);
sending=1;
flag_LF=0;
}
if(flag_FS==1)
{
Out_Data(table3);
while(sending);
sending=1;
flag_FS=0;
}
if(flag_DS==1)
{
Out_Data(table4);
while(sending);
sending=1;
flag_DS=0;
}
if(flag_FL==1)
{
Out_Data(table5);
while(sending);
sending=1;
flag_FL=0;
}
if(flag_BF==1)
{
Out_Data(table6);
while(sending);
sending=1;
flag_BF=0;
}
}
}
|