HC-05蓝牙模块图片:
引脚连接:STATE-->P2.6
RXD-->TXD
TXD-->RXD
GND-->GND
VCC-->5V
EN-->P2.5
SG90舵机图片:
引脚连接:棕色-->GND
红色-->VCC
橙色-->P1.0
刚学习完了舵机,想着做一个远程控制舵机的,选来选取还是用蓝牙模块,因为不耽误联WIFI上网。源码我贴在下面,具体连线方法在最后,包括手机端控制软件。
单片机源码如下:
- #include "reg52.h"
- typedef unsigned int u16;
- typedef unsigned char u8;
- sbit pwm =P1^0 ;
- sbit led1=P2^0;
- sbit led2=P2^1;
- sbit beep=P2^4;
- sbit STA=P2^5;
- #define USART_MAX_RECV_LEN 40
- u8 USART_RX_BUF[USART_MAX_RECV_LEN];
- u16 USART_RX_STA=0;
- unsigned char jd;
- unsigned char count;
- void delay(unsigned char i)
- {
- unsigned char j,k;
- for(j=i;j>0;j--)
- for(k=125;k>0;k--);
- }
- void UsartInit()
- {
- SCON=0X50;
- TMOD=0X21;
- PCON=0X80;
- TH1=0XFa;
- TL1=0XFa;
- TH0 = 0xfe;
- TL0 = 0x33;
- IE = 0x82;
- TR0=1;
- ES=1;
- EA=1;
- TR1=1;
-
- }
- void USART_SendData(u8 datbuf[])
- {
- u8 i=0;
- for(i=0;i<1;i++)
- {
- SBUF=datbuf[i];
- while(!TI);
- TI=0;
- }
- }
- void main()
- {
- u8 i=0,j=0;
- STA=0;
- led1=led2=1;
- UsartInit();
- while(1)
- {
- if(USART_RX_BUF[0]==0x11)
- {
- led1=0;
- led2=1;
- jd=2;
- delay(200);
- jd=3;
- }
- else if(USART_RX_BUF[0]==0x10)
- {
- led1=1;
- led2=0;
- jd=4;
- delay(200);
- jd=3;
- }
-
- }
- }
- void Usart() interrupt 4
- {
- u8 res;
- res=SBUF;
- USART_RX_BUF[0]=res;
- TH0 = 0xfe;
- TL0 = 0x33;
- RI = 0;
- if(count< jd)
- pwm=1;
- else
- pwm=0;
- count=(count+1);
- count=count%40;
- }
复制代码
|