I. Introduction to the environment
MCU model: STC89C52
Programming software: KEil5
Programming language: C language
** Mobile APP: ** Is designed with QT, and the program supports cross-platform compilation and operation (Android, IOS, Windows and Linux can be compiled and run, corresponding to the QT environment construction on the platform, the blog has posted an article to explain before)
Complete source code download address: download.csdn.net/download/xi…
The package contains: 51 MCU source code, Android mobile phone APP source code, executable file APK, all kinds of alarm sound, documentation, download tools.
Two, function and hardware introduction
MCU STC89C52, the data is very rich, keil construction project can choose AT89C52.
Bluetooth for communication with mobile phones: HC05 serial Bluetooth.
Function introduction:
The anti-loss function mainly depends on bluetooth to determine whether the connection is disconnected.
1. If bluetooth is not connected after the reset of the development board, the buzzer will alarm 5 seconds later
2. Once the Bluetooth connection is successful, it needs to send the specified data to the device at the frequency of 300ms to keep it alive.
3. If no data is sent to the device after bluetooth connection, the buzzer will alarm after 300ms.
4. The alarm can be triggered manually by clicking on the APP
5. The MCU can clear the alarm by pressing the reset button
APP interface:
If you do not want to welding, want to directly modular building environment, you can directly buy taobao system board + module:
Bluetooth HC05 is a master-slave Bluetooth serial port module. To put it simply, when a Bluetooth device is successfully paired with a Bluetooth device, we can ignore the internal communication protocol of Bluetooth and directly use Bluetooth as a serial port. When a connection is established, two devices share the same channel, that is, the same serial port. One device sends data to the channel, and the other device receives data from the channel.
In order to simplify the programming code of 51 microcontroller, you can first use the serial port debugging assistant of the computer, complete the parameter configuration of HC05, and then connect to 51 microcontroller. Set the HC05 Bluetooth to slave mode and set the pair password.
Three, 51 MCU code
#include <REG52.H>
#define uchar unsigned char
#define uint unsigned long
uchar buffer[8]; // Data cache array
uchar rec_flag=0; // Data processing flag
sbit BUZZER = P1^3;
sbit KEY = P3^4;
void delay1ms(void) / / error 0 us
{
unsigned char a,b,c;
for(c=1; c>0; c--)for(b=142; b>0; b--)for(a=2; a>0; a--); }void Delay(int ms)
{
while(ms--)
{
delay1ms();
}
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * function name: void InitUART (void) * * function function: serial port initialization function entered: * * no * * output: No ** says: Timer 1 mode 2 baud rate occurrence mode, baud rate 9600bps, crystal oscillator 11.0592MHZ, error 0% **TMOD timer counter register, SCON serial port register, PCON power control register, EA access external program memory control signal, ES serial port interrupt permit control bit, TR1 timer 1 operation control of a * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
void InitUART(void)
{
TMOD = 0x20;
SCON = 0x50;
TH1 = 0xFD;
TL1 = 0xFD;
PCON = 0x00;
ES = 1;
TR1 = 1;
EA = 1;
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * name: void TxChar uchar (ch) * * function: a serial port to send function * * input: uchar ch * * > send current data output: No * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
void TxChar(unsigned char ch)
{
SBUF=ch;
while(! TI); TI=0;
}
void putstring(uchar *puts) // Send data
{
for(; *puts! =199; puts++)// stop 199 is encountered
TxChar(*puts);
}
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * function name: void ser (void) 4 * * interrupt function function: serial port receiving interrupt function * * entered: No output: * * * * said: dealing with a serial port to send data string of * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
void ser(void) interrupt 4
{
static uchar i;
if(RI==1)
{
RI = 0;
if(rec_flag==0)
{
if(SBUF==0xff)
{
rec_flag=1;
i=0; }}else
{
if(SBUF==0xff)
{
rec_flag=0;
if(i==3)
{
//Communication_Decode();
}
i=0;
}
else{ buffer[i]=SBUF; i++; }}}}/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * function name: void main (void) function features: * * * * entered the main function: no * * output: No pet testing * * says: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
void main(void)
{
uint STOP=0,a=0,a1=0;
uchar bueezr_flag=0,bueezr_count=0,buzzer_time=3;// Buzzer alarm variable
uchar rxbuf[9];
InitUART(a); rxbuf[0] ='A';
rxbuf[1] =25;
rxbuf[2] ='B';
rxbuf[3] =99;
rxbuf[4] ='C';
rxbuf[5] =99;
rxbuf[6] ='D';
rxbuf[7] =99;
rxbuf[8] =199;
while(1)
{
putstring(rxbuf);// Send the data function
Delay(300); / / delay 300 ms
if(KEY==0 && !bueezr_flag){while(KEY==0); rxbuf[1] =22; bueezr_flag=1; }if(KEY==0 && bueezr_flag){while(KEY==0); rxbuf[1] =11; bueezr_flag=0; a=0; BUZZER=1; }if(! a)STOP++;//FF 00 00 01 FF
//
// Determine the data delivered by the APP
if(buffer[2] = =0x01) // Cancel the buzzer
{
STOP=0;
a1=1;
buffer[2] =0x00;
}
//15 300ms have not received the data from APP, continue to alarm
if(STOP>15 && a1)
{
bueezr_flag=1;
}
if(buffer[1] = =0x01) // Manual mode alarm
{
bueezr_flag=1;// The buzzer beeps directly
a=1; // Manual mode flag bit
}
/******* Buzzer alarm ********/
bueezr_count++;if(bueezr_count>buzzer_time*10)bueezr_count=buzzer_time+1;
if(bueezr_count%buzzer_time==0 && bueezr_flag)
{
BUZZER=~BUZZER;// When the buzzer is turned back, the sound prompts}}}Copy the code
Iv. Android phone APP code