Bluetooth Controlled Fan Using 51 Microcontroller

First, buy a Bluetooth module and download a Bluetooth serial debugging assistant on your phone, which is equivalent to the UART serial communication of the microcontroller. Use BUFF to obtain the received data. Here, we only need to focus on one issue, which is testing the encoding. If the APP sends 0xFF, (the APP Bluetooth serial debugging assistant already supports manually setting button names and sending characters) but the Bluetooth module is likely to receive 0xF8, 0xF2, or 0xFE. Therefore, we should first test the encoding. The HC-06 generally has five ports: RX, TX, VCC, GND, and AT. The AT port is not used here, as it is used to change the password, which is usually 1234 or 0000 by default. Connect VCC and GND properly, and then connect RX and TX to the microcontroller’s TX and RX respectively (which are P3^0 and P3^1, remember to connect them in reverse). Then we can proceed with the usual UART serial communication, detect the received BUFF data, and use switch statements to achieve the corresponding functions.

Bluetooth Controlled Fan Using 51 Microcontroller

#include
#define uchar unsigned char
#define uint unsigned int
sbit PWM=P1^0;
sbit DSPORT=P3^7;
void Ds18b20ReadTemp();
void UsartConfiguration();
unsigned int temp=0;
unsigned char zhuanshu=20;
unsigned int cnt=0;
unsigned char receiveData;
unsigned int dingshi=0;
unsigned char min=1;
void main(void)
{
unsigned int i=3000;
UsartConfiguration();
while(1)
{
if(cnt>=3000)
{
cnt=0;
Ds18b20ReadTemp(); // Check temperature every 3 seconds
}
switch(receiveData)
{
case 177:ET0=1;zhuanshu=20;receiveData=0;break; // Number 1 Fan starts
case 178:zhuanshu=0;while(i--);ET0=0;receiveData=0;break; // Number 2 Fan stops, since the IO port defaults to high level, set zhuanshu=0
case 179:zhuanshu=30;break; // Number 3 Low speed
case 180:zhuanshu=50;break; // Number 4 Medium speed
case 181:zhuanshu=70;break; // Number 5 High speed
case 182: // Number 6 Automatically adjusts speed based on temperature
{
if(temp<3200)
{
zhuanshu=25;
}
if((temp>3200)&&(temp<3300))
{
zhuanshu=30;
}
if((temp>3400)&&(temp<3600))
{
zhuanshu=50;
}
if((temp>3200)&&(temp<3300))
{
zhuanshu=60;
}
} break;
case 183: // Timer setting Number 7
{
static dingshi=0;
if(dingshi>=60000) // 1 minute benchmark
{
min--;
dingshi=0;
if(min==0)
{
ET0=0;
zhuanshu=0;
}
}
break;
}
case 184: receiveData=0;min++;break; // Increase time Number 8
case 185: receiveData=0;min--;break; // Decrease time Number 9
}
}
}
}
void UsartConfiguration()
{
SCON=0X50; // Set to working mode 1
TMOD=0X21; // Set counter working mode 2
PCON=0X80; // Baud rate doubled
TH0 = 0xFC;
TL0 = 0x66;
TH1=0XF9; // Initial value settings for the counter, note that the baud rate is 9600
TL1=0XF9;
ET0=1;
ES=1; // Enable receive interrupt
EA=1; // Enable global interrupt
TR1=1;
TR0=1; // Start the counter
}
void Usart() interrupt 4
{
receiveData=SBUF; // Get the received data
RI = 0; // Clear the receive interrupt flag
}
void Time1(void) interrupt 1 // 3 is the interrupt number for timer 1, 1 is the interrupt number for timer 0, 0 is external interrupt 1, 2 is external interrupt 2, 4 is serial interrupt
{
static unsigned char timer1=0;
TH0 = 0xF6; // Reassign initial value
TL0 = 0x66;
timer1++;
cnt++;
dingshi++;
if(dingshi>=60050)
{
dingshi=0;
}
if(timer1>100) // PWM cycle is 100*0.5ms
{
timer1=0;
}
if(timer1
{
PWM=1;
}
else
{
PWM=0;
}
}
void Delay1ms(uint y) // Delay program
{
uint x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
uchar Ds18b20Init() // Temperature sub-function
{
uchar i;
DSPORT = 0; // Pull down the bus for 480us~960us

i = 70;
while(i--); // Delay 642us
DSPORT = 1; // Then pull the bus high, if DS18B20 responds, it will pull the bus low after 15us~60us

i = 0;
while(DSPORT) // Wait for DS18B20 to pull the bus low
{
i++;
if(i>5)// Wait >5MS
{
return 0;// Initialization failed
}
Delay1ms(1);
}
return 1;// Initialization successful
}
void Ds18b20WriteByte(uchar dat)
{
uint i, j;
for(j=0; j<8; j++)
{
DSPORT = 0; // Before writing a bit of data, pull the bus low for 1us

i++;
DSPORT = dat & 0x01; // Then write a data bit, starting from the lowest bit

i=6;
while(i--); // Delay 68us, the duration must be at least 60us
DSPORT = 1; // Then release the bus, at least 1us for the bus recovery time to write the second value

dat >>= 1;
}
}
uchar Ds18b20ReadByte()
{
uchar byte, bi;
uint i, j;
for(j=8; j>0; j--)
{
DSPORT = 0;// Pull the bus low for 1us

i++;
DSPORT = 1;// Then release the bus

i++;
i++;// Delay 6us to wait for data stabilization
bi = DSPORT; // Read data, starting from the lowest bit
/* Shift byte left by one, then OR with bi shifted right by 7, note to fill 0 after shifting off that bit.*/
byte = (byte >> 1) | (bi << 7);
i = 4; // After reading, wait 48us before continuing to read the next number
while(i--);
}
return byte;
}
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);// Skip ROM operation command
Ds18b20WriteByte(0x44); // Temperature conversion command
// Delay1ms(100);// Wait for conversion success, but if you are continuously refreshing, this delay is not needed
}
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);// Skip ROM operation command
Ds18b20WriteByte(0xbe);// Send read temperature command
}
void Ds18b20ReadTemp()
{
float tp;
int temp1 = 0;
uchar tmh, tml;
Ds18b20ChangTemp();// First send conversion command
Ds18b20ReadTempCom();// Then wait for conversion to finish and send read temperature command
tml = Ds18b20ReadByte();// Read temperature value, 16 bits, first read low byte

tmh = Ds18b20ReadByte();// Then read high byte
temp1 = tmh;
temp1 <<= 8;
temp1 |= tml;
if(temp1< 0)
{
temp1=temp1-1;
temp1=~temp1;
tp=temp1;
temp1=tp*0.0625*100+0.5;
}
else
{
tp=temp1;
temp1=tp*0.0625*100+0.5;
}
temp=temp1%10000;
}

Bluetooth Controlled Fan Using 51 Microcontroller

Leave a Comment