51单片机的蓝牙模块,是在蓝牙通讯的基础上,进行蓝牙51模块与外部蓝牙发射接收设备之间,相互收发数据。并且其引脚为VCC,GND,TXD,RXD,可以通过串口通信与外部上位机或单片机通信。
代码如下(注意蓝牙模块是5V供电)
#include<reg52.h>
void init();
void delay(unsigned int ms);
unsigned char input;
void display(unsigned char num_decimal);
unsigned char code character[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90};
sbit SegSelection1=P2^0;
sbit SegSelection2=P2^1;
sbit SegSelection3=P2^2;
sbit SegSelection4=P2^3;
sbit p1=P1^0;
sbit p2=P1^1;
void main()
{
p1=1;
p2=0;
init();
while(1)
{
display(input);
}
}
void init(void)
{
EA = 1;
ES = 1;
PCON = 0x00;
SCON = 0x50; //串口工作在方式1,并且启动串行接收
TMOD = 0x20;
TH1 = 0xFd; //设置波特率 9600
TL1 = 0xFd;
TR1 = 1;
}
void rt() interrupt 4 //中断4:串行收发数据
{
EA=0;
if(RI==1)
{
input=SBUF-48;
RI=0;
SBUF=input+48;
while(!TI);
TI=0;
}
EA=1;
}
void display(unsigned char num_decimal) //在数码管显示传输的数据
{
unsigned int thousand,hundred,tens,unit;
thousand=num_decimal/1000;
hundred=(num_decimal-thousand*1000)/100;
tens=(num_decimal-thousand*1000-hundred*100)/10;
unit=num_decimal%10;
SegSelection1=0;
P0=character[thousand];
delay(5);
SegSelection1=1;
SegSelection2=0;
P0=character[hundred];
delay(5);
SegSelection2=1;
SegSelection3=0;
P0=character[tens];
delay(5);
SegSelection3=1;
SegSelection4=0;
P0=character[unit];
delay(5);
SegSelection4=1;
}
void delay(unsigned int ms)
{
unsigned int x;
while(ms--)
for(x=110;x>0;x--);
}