|
10黑幣
//手機(jī)APP藍(lán)牙遙控控制單片機(jī)LED燈的亮滅開關(guān)單片機(jī)源代碼:
#include<reg52.h>
#include <stdio.h>
unsigned char Rx_buf[4],Rxnum=0;
sbit LED1=P1^0;
sbit LED2=P1^1;
void InitUART(void) //這是串口的基本配置,配置他的波特率是9600.這些參數(shù)都是標(biāo)準(zhǔn)的。
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = TH1;
PCON = 0x00;
EA = 1;
ES = 1;
TR1 = 1;
}
void Delay_ms(unsigned int n)//n 毫秒延時(shí)
{
unsigned char j;
while(n--)
{
for(j=0;j<125;j++);
}
}
void main()
{
unsigned char Tx_Buf[10];
unsigned char LEDstatus;//燈的狀態(tài)
InitUART(); //初始化串口
while(1)
{
;
}
}
/*****************串口接收中斷函數(shù),接收藍(lán)牙模塊的數(shù)據(jù)*********************/
void UARTInterrupt(void) interrupt 4
{
ES=0; //關(guān)閉中斷
if(RI) //接收到數(shù)據(jù)
{
Rx_buf[Rxnum]=SBUF; //接收數(shù)據(jù)進(jìn)數(shù)組
if(Rx_buf[Rxnum]==0x08)
Rxnum=0;
else
Rxnum++;
if(Rx_buf[0]==0x01)//LED1燈
{
if(Rx_buf[1]==0x01)//開燈
LED1=0;//開燈
else
LED1=1; //關(guān)燈
}
if(Rx_buf[0]==0x02)//LED2燈
{
if(Rx_buf[1]==0x01)//開燈
LED2=0;//開燈
else
LED2=1; //關(guān)燈
}
}
RI=0;
ES=1; //關(guān)閉中斷
}
|
|