适用于:stc15f2k60s2型号单片机(其它型号的51单片机如果想用这个程序,就得修改程序的波特率和某一些的地方才能用)
蓝牙模块:hc-05模块
你用手机给蓝牙发个0,led灯亮,发个1,led灯灭。
单片机源程序如下:
- #include <stc15f2k60s2.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar receiveData;
- sbit led=P0^0;
- void UartInit(void) //9600bps@ 11.0592MHz
- {
- PCON &= 0x7F; //波特率不倍速
- SCON = 0x50; //8位数据,可变波特率
- AUXR |= 0x40; //定时器1时钟为Fosc,即1T
- AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
- TMOD &= 0x0F; //清除定时器1模式位
- TMOD |= 0x20; //设定定时器1为8位自动重装方式
- TL1 = 0xDC; //设定定时初值
- TH1 = 0xDC; //设定定时器重装值
- ET1 = 0; //禁止定时器1中断
- TR1 = 1; //启动定时器1
- EA=1; //打开总中断
- ES=1; //打开接收中断
-
- }
- void send1_Byte(unsigned char byte)//发送字符
- {
- SBUF = byte;
- while(TI==0); //发送完会自动置1
- TI=0; //手动清零
- }
- void Send1_String(char *str) //发送字符串
- {
- while (*str!='\0') //检测字符串结束标志
- {
- send1_Byte(*str++);
- }
- }
- void Usart() interrupt 4 //串口中断函数接收程序
- {
-
- if (RI==1)
- {
- receiveData = SBUF;
- RI=0;
-
- }
- }
- void working()
- {
- if(receiveData == '0') //发现有数据输入0
- {
- send1_Byte('0'); led=0 ;
-
- receiveData = 'A';}
-
- if(receiveData == '1')
- {
- send1_Byte('1');
- led=1 ;
- receiveData = 'A';}
- }
- int main()
- {
- UartInit();
- while(1)
- {
- working();
- }
- }
复制代码
所有资料51hei提供下载:
51单片机hc-05蓝牙点灯程序.zip
(257.03 KB, 下载次数: 67)
|