-
RXD接TXD,TXD接RXD。通过手机的蓝牙助手可以完成亮灭LED1和LED2的 *** 作
-
当发送A时,LED1亮。发送B时,LED1灭。当发送C时,LED2亮,当发送D时,LED2灭
-
使用的STC89C52RC单片机
#include
#include "intrins.h"
#include "string.h"
char uart_dat[10];
int uart_temp=0;
sbit ledOne=P3^7;
sbit ledTwo=P3^6;
/**
*函数名称:UartInit
*函数功能:波特率初始化
*备 注:
*/
void UartInit(void) //9600bps@11.0592MHz
{
PCON &= 0x7F; //波特率不倍速
SCON = 0x50; //8位数据,可变波特率
AUXR &= 0xBF; //定时器时钟12T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TMOD |= 0x20; //设置定时器模式
TL1 = 0xFD; //设置定时初始值
TH1 = 0xFD; //设置定时重载值
ET1 = 0; //禁止定时器%d中断
TR1 = 1; //定时器1开始计时
ES=1;
EA=1;
}
/**
*函数名称:uartService
*函数功能:串口服务函数
*备 注:
*/
void uartService() interrupt 4
{
if(RI==1)
{
uart_dat[uart_temp]=SBUF;
uart_temp++;
RI=0;
}
}
/**
*函数名称:sendDate
*函数功能:发送一个字节
*备 注:
*/
void sendDate(unsigned char dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
/**
*函数名称:sendString
*函数功能:发送一个字符串
*备 注:
*/
void sendString(unsigned char *str)
{
while(*str!=')'sendDate
{
(*++str);}
}
/**
*函数名称:judgeInformation
*函数功能:判断接收到的数据,同时对LED1和LED2 *** 作
*备 注:
*/
void
judgeInformation ()if
{
(==uart_temp1)=
{
uart_temp0;if
(strcmp(,uart_dat"A")==0)=
{
ledOne0;sendString
("L1 is open");}
if
(strcmp(,uart_dat"B")==0)=
{
ledOne1;sendString
("L1 is close");}
if
(strcmp(,uart_dat"C")==0)=
{
ledTwo0;sendString
("L2 is open");}
if
(strcmp(,uart_dat"D")==0)=
{
ledTwo1;sendString
("L2 is close");}
}
}
/**
*函数名称:main
*函数功能:主函数
*备 注:
*/
void
main ()UartInit
{
();sendString
("welcome:\r\n");while
(1)judgeInformation
{
();}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)