STM32F103ZET6手机蓝牙控制程序,实现手机APP控制
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "usart.h"
- #include "timer.h"
- #include "extix.h"
- #include "wdg.h"
- #include "gpio.h"
- #define ONE 1
- #define TWO 2
- #define THREE 3
- #define FOUR 4
- #define FIVE 5
- #define SIX 6
- #define SEVEN 7
- #define EIGHT 8
- #define NINE 9
- #define TEN 10
- #define ELEVEN 11
- #define TWELVE 12
- #define THIRTEEN 13
- #define FOURTEEN 14
- #define FIFTEEN 15
- #define SIXTEEN 16
- #define SEVENTEEN 17
- #define EIGHTEEN 18
- #define NINETEEN 19
- #define TWENTY 20
- volatile uint8_t USART_RX_BUF[200] = {0x00};
- uint8_t RxCounter = 0; //数据长度标志位
- uint8_t ReceiveState = 0; //数据包接收完成标志位
- uint8_t RxCounter_last = 0; //数据长度标志位,用于当前学号验证错误,验证下一学号时用
- const uint8_t xuehao1[50][12] = {{52,55,48,50,49,55,48,48,55,51,13,10},\
- {52,55,48,50,49,55,48,48,49,48,13,10}};
- /*——————————————————密码保存区域——————————————————*/
- const uint8_t password0[16] = {49,55,54,51,56,49,54,48,52,56,53,108,105,117,13,10};
- const uint8_t password1[16] = {49,53,55,49,51,54,54,53,50,51,55,108,105,117,13,10};
- const uint8_t password2[16];
- const uint8_t password3[16];
- const uint8_t password4[16];
- const uint8_t password5[16];
- const uint8_t password6[16];
- const uint8_t password7[16];
- const uint8_t password8[16];
- const uint8_t password9[16];
- const uint8_t password10[16];
- const uint8_t password11[16];
- const uint8_t password12[16];
- const uint8_t password13[16];
- const uint8_t password14[16];
- const uint8_t password15[16];
- const uint8_t password16[16];
- const uint8_t password17[16];
- const uint8_t password18[16];
- const uint8_t password19[16];
- const uint8_t password20[16];
- const uint8_t password21[16];
- const uint8_t password22[16];
- const uint8_t password23[16];
- const uint8_t password24[16];
- const uint8_t password25[16];
- const uint8_t password26[16];
- const uint8_t password27[16];
- const uint8_t password28[16];
- const uint8_t password29[16];
- const uint8_t password30[16];
- const uint8_t password31[16];
- const uint8_t password32[16];
- const uint8_t password33[16];
- const uint8_t password34[16];
- const uint8_t password35[16];
- const uint8_t password36[16];
- const uint8_t password37[16];
- const uint8_t password38[16];
- const uint8_t password39[16];
- const uint8_t password40[16];
- const uint8_t password41[16];
- const uint8_t password42[16];
- const uint8_t password43[16];
- const uint8_t password44[16];
- const uint8_t password45[16];
- const uint8_t password46[16];
- const uint8_t password47[16];
- const uint8_t password48[16];
- const uint8_t password49[16];
- int i =0,dengji = 0,j = 0; //i为输入信息与储存信息验证标志位//dengji为学号密码程序选择位//j为系统初始位//
- int member = 0; //成员标志位,用于选择当前成员学号
- int password_err = 3; //允许密码输入错误的最大次数
- int switch_successful = 0; //密码验证成功时,跳出switch选择语句
- int err_show = 0;
- int times = 0;
- int main(void)
- {
- u16 t;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- EXTIX_Init(); //初始化外部中断
- TIM3_Int_Init(7199,9); //TIM定时器初始化
- KEY_Init(); //初始化与按键连接的硬件接口
- delay_init(); //延时函数初始化
- uart_init(115200); //串口初始化为115200
- LED_Init(); //LED端口初始化
- WWDG_Init(0X7F,0X5F,WWDG_Prescaler_8); //计数器值为7f,窗口寄存器为5f,分频数为8
- j = 3;
- while(1)
- {
- if(j == 0)
- {
- if(dengji == 0)
- {
- printf("Please enter the student number\r\n"); //请输入学号
- while(1) //等待输入学号
- {
- if(ReceiveState==1)
- {
- ReceiveState=0; //数据帧标志位清零
- t=0;
- RxCounter_last = RxCounter;
- member = 0;
- break;
- }
- }
-
- while(1)
- {
- RxCounter = RxCounter_last;
- t=0;
- if(RxCounter != 12) //如果输入的数据长度不匹配
- {
- delay_ms(50);
- printf("student number format err\r\n"); //学号格式错误
- delay_ms(50);
- printf("Please enter again\r\n"); //请再次输入
- RxCounter = 0;
- break; //跳出循环
- }
- printf("RxC_C:%d\r\n",RxCounter);
- while(RxCounter--) //学号验证程序
- {
- printf("RxC:%d\r\n",RxCounter);
- printf("读取值:%d\r\n",USART_RX_BUF[t]);
- printf("设定值:%d\r\n",xuehao1[member][t]);
-
- if(USART_RX_BUF[t] != xuehao1[member][t])
- i++;
- t++;
- }
- if(i!=0) //如果学号验证未通过
- {
- printf("I:%d\r\n",i);
- i=0; //清除验证标志位
- member+=1; //成员加一,准备验证下一成员
- //delay_ms(50);
- //printf("student number err\r\n");
- delay_ms(50);
- if(member>50) //最大人数限定
- {
- dengji = 0;
- delay_ms(50);
- printf("Without this member\r\n"); //没有这个成员
- delay_ms(50);
- printf("The system will exit\r\n"); //系统即将退出
- member = 0; //成员选择标志位清零
- j = 1; //开锁失败标志位
- break;
- }
- }
- else
- {
- printf("Please enter passWord\r\n");
- dengji=1; //进入密码验证程序
- i = 0; //清除验证标志位
- RxCounter=0; //清除数据长度标志位
- break;
- }
- }
- }
-
- /*——————————————————————————密码验证程序——————————————————————————*/
- else if(dengji == 1) //密码验证程序
- {
- switch(member)
- {
- case 0:
- while(password_err--) //密码输入错误时循环,共3次
- {
- while(1) //等待密码输入
- {
- if(ReceiveState==1)
- {
- ReceiveState=0; //数据帧标志位清零
- t=0;
- //RxCounter_last = RxCounter;
- break;
- }
- }
- if(RxCounter != 16) //如果输入的数据长度不匹配
- {
- delay_ms(50);
- printf("Password err\r\n");
- RxCounter = 0;
- j = 1;
- if(password_err != 0)
- {
- printf("Please enter again\r\n"); //请再次输入
- delay_ms(50);
- printf("The remaining %d opportunities\r\n",password_err);
- }
- }
- else
- {
- printf("RxC_C:%d\r\n",RxCounter);
- while(RxCounter--)
- {
- printf("RxC:%d\r\n",RxCounter);
- printf("读取值:%d\r\n",USART_RX_BUF[t]);
- printf("设定值:%d\r\n",password0[t]);
-
- if(USART_RX_BUF[t] != password0[t])
- i++;
- t++;
- }
- if(i!=0) //密码输入错误判断程序
- {
- printf("I:%d\r\n",i);
- i=0;
- delay_ms(50);
- printf("passWord err\r\n");
- delay_ms(50);
- j = 1; //开锁失败标志位
- RxCounter = 0;
- if(password_err != 0)
- {
- printf("Please enter again\r\n"); //请再次输入
- delay_ms(50);
- printf("The remaining %d opportunities\r\n",password_err);
- }
- //dengji = 0;
- else if(password_err == 0) //当密码三次都输入错误时,跳出switch选择程序
- {
- dengji = 0;
- i = 0;
- j = 1; //标志位,使系统进入安全模式
- delay_ms(50);
- printf("The system will exit\r\n");
- delay_ms(50);
- }
- //switch_successful = 1;
- }
- else
- {
- printf("Welcome to 444\r\n");
- dengji = 0;
- i = 0;
- j = 2;
- RxCounter=0;
- break;
- }
- }//end-->else
- }//end-->while(--password_err)
- //if(switch_successful==1)
- //{
- //switch_successful = 0;
- break;
- //}
-
- case 1:
- while(password_err--) //密码输入错误时循环,共3次
- {
- while(1) //等待密码输入
- {
- if(ReceiveState==1)
- {
- ReceiveState=0; //数据帧标志位清零
- t=0;
- //RxCounter_last = RxCounter;
- break;
- }
- }
- if(RxCounter != 16) //如果输入的数据长度不匹配
- {
- delay_ms(50);
- printf("Password err\r\n");
- RxCounter = 0;
- j = 1;
- if(password_err != 0)
- {
- printf("Please enter again\r\n"); //请再次输入
- delay_ms(50);
- printf("The remaining %d opportunities\r\n",password_err);
- }
- }
- else //如果输入的数据长度匹配
- {
- printf("RxC_C:%d\r\n",RxCounter);
- while(RxCounter--)
- {
- printf("RxC:%d\r\n",RxCounter);
- printf("读取值:%d\r\n",USART_RX_BUF[t]);
- printf("设定值:%d\r\n",password0[t]);
-
- if(USART_RX_BUF[t] != password1[t])
- i++;
- t++;
- }
- if(i!=0) //密码输入错误判断程序
- {
- printf("I:%d\r\n",i);
- i=0;
- delay_ms(50);
- printf("passWord err\r\n");
- delay_ms(50);
- j = 1; //开锁失败标志位
- RxCounter = 0;
- if(password_err != 0)
- {
- printf("Please enter again\r\n"); //请再次输入
- delay_ms(50);
-
- printf("The remaining %d opportunities\r\n",password_err);
- }
- //dengji = 0;
- else if(password_err == 0) //当密码三次都输入错误时,跳出switch选择程序
- {
- dengji = 0;
- i = 0;
- j = 1; //标志位,使系统进入安全模式
- delay_ms(50);
- printf("The system will exit\r\n");
- delay_ms(50);
- }
- //switch_successful = 1;
- }
- else
- {
- printf("Welcome to 444\r\n");
- dengji = 0;
- i = 0;
- j = 2;
- RxCounter=0;
- //switch_successful = 1;
- break;
- }
- }
- }//end-->while(--password_err)
- break;
- }//end-->switch(member)
- }//end-->else if(dengji == 1)
- }//end-->if(j == 0)
-
- else if(j == 1) //开锁失败
- {
- delay_ms(50);
- printf("The system enters security mode\r\n"); //系统进入安全模式
- delay_ms(50);
- printf("Please disconnect\r\n"); //请断开连接
- while(1)
- {
- if(j != 1)
- break;
- }
- }
- else if(j == 2) //开锁成功
- {
- times = 0;
- delay_ms(50);
- printf("The lock is Opening\r\n"); //锁已开
- delay_ms(50);
- printf("Please Wait\r\n"); //请等待
- while(1)
- {
- times++;
- if(times > 1000)
- {
- times = 0;
- if(j != 2)
- break;
- }
- }
- }
- else if(j == 3)
- {
- times = 0;
- while(1)
- {
- times++;
- if(times > 1000)
- {
- times = 0;
- if(j != 3)
- break;//蓝牙断开连接时执行该函数,可以让控制器睡眠
- }
- }
- }
-
- }//end-->while(1)
- }//end-->int main
复制代码
所有资料51hei提供下载:
STM32F103ZET6手机蓝牙控制程序.rar
(308.04 KB, 下载次数: 49)
|