找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3947|回复: 1
收起左侧

STM32F103ZET6手机蓝牙控制源程序

[复制链接]
ID:367409 发表于 2018-7-8 17:21 | 显示全部楼层 |阅读模式
STM32F103ZET6手机蓝牙控制程序,实现手机APP控制

单片机源程序如下:
  1. #include "led.h"
  2. #include "delay.h"
  3. #include "key.h"
  4. #include "sys.h"
  5. #include "usart.h"
  6. #include "timer.h"
  7. #include "extix.h"
  8. #include "wdg.h"
  9. #include "gpio.h"


  10. #define         ONE                                        1
  11. #define                TWO                                        2
  12. #define                THREE                                3
  13. #define                FOUR                                4
  14. #define                FIVE                                5
  15. #define                SIX                                        6
  16. #define                SEVEN                                7
  17. #define                EIGHT                                8
  18. #define                NINE                                9
  19. #define                TEN                                        10
  20. #define                ELEVEN                        11
  21. #define                TWELVE                        12
  22. #define                THIRTEEN                13
  23. #define                FOURTEEN                14
  24. #define                FIFTEEN                        15
  25. #define                SIXTEEN                        16
  26. #define                SEVENTEEN                17
  27. #define                EIGHTEEN                18
  28. #define                NINETEEN                19
  29. #define                TWENTY                        20

  30. volatile uint8_t USART_RX_BUF[200] = {0x00};
  31. uint8_t RxCounter = 0;                                                                                                                                        //数据长度标志位
  32. uint8_t ReceiveState = 0;                                                                                                                                //数据包接收完成标志位
  33. uint8_t RxCounter_last = 0;                                                                                                                        //数据长度标志位,用于当前学号验证错误,验证下一学号时用
  34. const uint8_t xuehao1[50][12] = {{52,55,48,50,49,55,48,48,55,51,13,10},\
  35.                                                                                                                                  {52,55,48,50,49,55,48,48,49,48,13,10}};
  36. /*——————————————————密码保存区域——————————————————*/
  37. const uint8_t password0[16] = {49,55,54,51,56,49,54,48,52,56,53,108,105,117,13,10};
  38. const uint8_t password1[16] = {49,53,55,49,51,54,54,53,50,51,55,108,105,117,13,10};
  39. const uint8_t password2[16];
  40. const uint8_t password3[16];
  41. const uint8_t password4[16];
  42. const uint8_t password5[16];
  43. const uint8_t password6[16];
  44. const uint8_t password7[16];
  45. const uint8_t password8[16];
  46. const uint8_t password9[16];
  47. const uint8_t password10[16];
  48. const uint8_t password11[16];
  49. const uint8_t password12[16];
  50. const uint8_t password13[16];
  51. const uint8_t password14[16];
  52. const uint8_t password15[16];
  53. const uint8_t password16[16];
  54. const uint8_t password17[16];
  55. const uint8_t password18[16];
  56. const uint8_t password19[16];
  57. const uint8_t password20[16];
  58. const uint8_t password21[16];
  59. const uint8_t password22[16];
  60. const uint8_t password23[16];
  61. const uint8_t password24[16];
  62. const uint8_t password25[16];
  63. const uint8_t password26[16];
  64. const uint8_t password27[16];
  65. const uint8_t password28[16];
  66. const uint8_t password29[16];
  67. const uint8_t password30[16];
  68. const uint8_t password31[16];
  69. const uint8_t password32[16];
  70. const uint8_t password33[16];
  71. const uint8_t password34[16];
  72. const uint8_t password35[16];
  73. const uint8_t password36[16];
  74. const uint8_t password37[16];
  75. const uint8_t password38[16];
  76. const uint8_t password39[16];
  77. const uint8_t password40[16];
  78. const uint8_t password41[16];
  79. const uint8_t password42[16];
  80. const uint8_t password43[16];
  81. const uint8_t password44[16];
  82. const uint8_t password45[16];
  83. const uint8_t password46[16];
  84. const uint8_t password47[16];
  85. const uint8_t password48[16];
  86. const uint8_t password49[16];

  87. int i =0,dengji = 0,j = 0;                                                                                                                        //i为输入信息与储存信息验证标志位//dengji为学号密码程序选择位//j为系统初始位//
  88. int member = 0;                                                                                                                                                                        //成员标志位,用于选择当前成员学号
  89. int password_err = 3;                                                                                                                                                //允许密码输入错误的最大次数
  90. int switch_successful = 0;                                                                                                                        //密码验证成功时,跳出switch选择语句
  91. int err_show = 0;
  92. int times = 0;

  93. int main(void)
  94. {               
  95.         u16 t;  
  96.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);                         //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
  97.         EXTIX_Init();                                                                                                                                                                        //初始化外部中断
  98.         TIM3_Int_Init(7199,9);                                                                                                                                //TIM定时器初始化
  99.         KEY_Init();                                                                                                                                                  //初始化与按键连接的硬件接口
  100.         delay_init();                                                                                                                                                             //延时函数初始化          
  101.         uart_init(115200);                                                                                                                                                 //串口初始化为115200
  102.         LED_Init();                                                                                                                                                              //LED端口初始化
  103.         WWDG_Init(0X7F,0X5F,WWDG_Prescaler_8);                                                                //计数器值为7f,窗口寄存器为5f,分频数为8          
  104.   j = 3;
  105.         while(1)
  106.         {
  107.                 if(j == 0)
  108.                 {
  109.                 if(dengji == 0)
  110.                 {
  111.                 printf("Please enter the student number\r\n");                        //请输入学号
  112.                         while(1)                                                                                                                                                                        //等待输入学号
  113.                 {
  114.                         if(ReceiveState==1)
  115.                                 {
  116.                                         ReceiveState=0;                                                                                                                                //数据帧标志位清零
  117.                                         t=0;
  118.                                         RxCounter_last = RxCounter;
  119.                                         member = 0;
  120.                                         break;
  121.                                 }
  122.                 }
  123.                
  124.                 while(1)
  125.                 {
  126.                         RxCounter = RxCounter_last;
  127.                         t=0;
  128.                         if(RxCounter != 12)                                                                                                                                //如果输入的数据长度不匹配
  129.                         {
  130.                                 delay_ms(50);
  131.                                 printf("student number format err\r\n");                                //学号格式错误
  132.                                 delay_ms(50);
  133.                                 printf("Please enter again\r\n");                                                                //请再次输入
  134.                                 RxCounter = 0;
  135.                                 break;                                                                                                                                                                        //跳出循环
  136.                         }
  137.                         printf("RxC_C:%d\r\n",RxCounter);
  138.                         while(RxCounter--)                                                                                                                                //学号验证程序
  139.                         {
  140.                                 printf("RxC:%d\r\n",RxCounter);
  141.                                 printf("读取值:%d\r\n",USART_RX_BUF[t]);
  142.                                 printf("设定值:%d\r\n",xuehao1[member][t]);
  143.                                
  144.                                 if(USART_RX_BUF[t] != xuehao1[member][t])
  145.                                         i++;
  146.                                 t++;
  147.                         }
  148.                         if(i!=0)                                                                                                                                                                        //如果学号验证未通过
  149.                         {
  150.                                 printf("I:%d\r\n",i);
  151.                                 i=0;                                                                                                                                                                                //清除验证标志位
  152.                                 member+=1;                                                                                                                                                        //成员加一,准备验证下一成员
  153.                                 //delay_ms(50);
  154.                                 //printf("student number err\r\n");
  155.                                 delay_ms(50);
  156.                                 if(member>50)                                                                                                                                                //最大人数限定
  157.                                 {
  158.                                         dengji = 0;
  159.                                         delay_ms(50);
  160.                                         printf("Without this member\r\n");                                                //没有这个成员
  161.                                         delay_ms(50);
  162.                                         printf("The system will exit\r\n");                                                //系统即将退出
  163.                                         member = 0;                                                                                                                                                //成员选择标志位清零
  164.                                         j = 1;                                                                                                                                                                //开锁失败标志位
  165.                                         break;
  166.                                 }
  167.                         }
  168.                         else
  169.                         {
  170.                                 printf("Please enter passWord\r\n");
  171.                                 dengji=1;                                                                                                                                                                //进入密码验证程序
  172.                                 i = 0;                                                                                                                                                                        //清除验证标志位
  173.                                 RxCounter=0;                                                                                                                                                //清除数据长度标志位
  174.                                 break;
  175.                         }
  176.                 }
  177.         }
  178.                
  179. /*——————————————————————————密码验证程序——————————————————————————*/
  180.                 else if(dengji == 1)                                                                                                                                //密码验证程序
  181.                 {
  182.                         switch(member)
  183.                         {
  184.                                 case 0:
  185.                                                                 while(password_err--)                                                                                                                                                        //密码输入错误时循环,共3次
  186.                                                                         {
  187.                                                                                         while(1)                                                                                                                                                                                //等待密码输入
  188.                                                                                                 {
  189.                                                                                                         if(ReceiveState==1)                                                                                                                       
  190.                                                                                                         {
  191.                                                                                                                 ReceiveState=0;                                                                                                                                //数据帧标志位清零
  192.                                                                                                                 t=0;
  193.                                                                                                                 //RxCounter_last = RxCounter;
  194.                                                                                                                 break;
  195.                                                                                                         }
  196.                                                                                                 }
  197.                                                                                                 if(RxCounter != 16)                                                                                                                                //如果输入的数据长度不匹配
  198.                                                                                                 {
  199.                                                                                                         delay_ms(50);
  200.                                                                                                         printf("Password err\r\n");
  201.                                                                                                         RxCounter = 0;
  202.                                                                                                         j = 1;
  203.                                                                                                         if(password_err != 0)
  204.                                                                                                         {
  205.                                                                                                                 printf("Please enter again\r\n");                                                        //请再次输入
  206.                                                                                                                 delay_ms(50);
  207.                                                                                                                 printf("The remaining %d opportunities\r\n",password_err);
  208.                                                                                                         }
  209.                                                                                                 }
  210.                                                                                                 else
  211.                                                                                                 {
  212.                                                                                                 printf("RxC_C:%d\r\n",RxCounter);
  213.                                                                                                 while(RxCounter--)
  214.                                                                                         {
  215.                                                                                                 printf("RxC:%d\r\n",RxCounter);
  216.                                                                                                 printf("读取值:%d\r\n",USART_RX_BUF[t]);
  217.                                                                                                 printf("设定值:%d\r\n",password0[t]);
  218.                                                                                                
  219.                                                                                                 if(USART_RX_BUF[t] != password0[t])
  220.                                                                                                         i++;
  221.                                                                                                 t++;
  222.                                                                                         }
  223.                                                                                         if(i!=0)                                                                                                                                                                                //密码输入错误判断程序
  224.                                                                                         {
  225.                                                                                                 printf("I:%d\r\n",i);
  226.                                                                                                 i=0;
  227.                                                                                                 delay_ms(50);
  228.                                                                                                 printf("passWord err\r\n");
  229.                                                                                                 delay_ms(50);
  230.                                                                                                 j = 1;                                                                                                                                                                                //开锁失败标志位
  231.                                                                                                 RxCounter = 0;
  232.                                                                                                 if(password_err != 0)
  233.                                                                                                 {
  234.                                                                                                         printf("Please enter again\r\n");                                                                //请再次输入
  235.                                                                                                         delay_ms(50);
  236.                                                                                                         printf("The remaining %d opportunities\r\n",password_err);
  237.                                                                                                 }
  238.                                                                                                 //dengji = 0;
  239.                                                                                                 else if(password_err == 0)                                                                                                //当密码三次都输入错误时,跳出switch选择程序
  240.                                                                                                 {
  241.                                                                                                         dengji = 0;
  242.                                                                                                         i = 0;
  243.                                                                                                         j = 1;                                                                                                                                                                        //标志位,使系统进入安全模式       
  244.                                                                                                         delay_ms(50);
  245.                                                                                                         printf("The system will exit\r\n");
  246.                                                                                                         delay_ms(50);
  247.                                                                                                 }
  248.                                                                                                         //switch_successful = 1;
  249.                                                                                         }
  250.                                                                                         else
  251.                                                                                         {
  252.                                                                                                 printf("Welcome to 444\r\n");
  253.                                                                                                 dengji = 0;
  254.                                                                                                 i = 0;
  255.                                                                                                 j = 2;
  256.                                                                                                 RxCounter=0;
  257.                                                                                                 break;
  258.                                                                                         }
  259.                                                                                 }//end-->else
  260.                                                                         }//end-->while(--password_err)
  261.                                                                         //if(switch_successful==1)
  262.                                                                                         //{
  263.                                                                                                 //switch_successful = 0;
  264.                                                                                                 break;
  265.                                                                                         //}
  266.                                                                        
  267.                                 case 1:
  268.                                                                 while(password_err--)                                                                                                                                                        //密码输入错误时循环,共3次
  269.                                                                         {
  270.                                                                                         while(1)                                                                                                                                                                                //等待密码输入
  271.                                                                                                 {
  272.                                                                                                         if(ReceiveState==1)                                                                                                                       
  273.                                                                                                         {
  274.                                                                                                                 ReceiveState=0;                                                                                                                                //数据帧标志位清零
  275.                                                                                                                 t=0;
  276.                                                                                                                 //RxCounter_last = RxCounter;
  277.                                                                                                                 break;
  278.                                                                                                         }
  279.                                                                                                 }
  280.                                                                                                 if(RxCounter != 16)                                                                                                                                //如果输入的数据长度不匹配
  281.                                                                                                 {
  282.                                                                                                         delay_ms(50);
  283.                                                                                                         printf("Password err\r\n");
  284.                                                                                                         RxCounter = 0;
  285.                                                                                                         j = 1;
  286.                                                                                                         if(password_err != 0)
  287.                                                                                                         {
  288.                                                                                                                 printf("Please enter again\r\n");                                                        //请再次输入
  289.                                                                                                                 delay_ms(50);
  290.                                                                                                                 printf("The remaining %d opportunities\r\n",password_err);
  291.                                                                                                         }
  292.                                                                                                 }
  293.                                                                                                 else                                                                                                                                                                                        //如果输入的数据长度匹配
  294.                                                                                                 {
  295.                                                                                                 printf("RxC_C:%d\r\n",RxCounter);
  296.                                                                                                 while(RxCounter--)
  297.                                                                                         {
  298.                                                                                                 printf("RxC:%d\r\n",RxCounter);
  299.                                                                                                 printf("读取值:%d\r\n",USART_RX_BUF[t]);
  300.                                                                                                 printf("设定值:%d\r\n",password0[t]);
  301.                                                                                                
  302.                                                                                                 if(USART_RX_BUF[t] != password1[t])
  303.                                                                                                         i++;
  304.                                                                                                 t++;
  305.                                                                                         }
  306.                                                                                         if(i!=0)                                                                                                                                                                                //密码输入错误判断程序
  307.                                                                                         {
  308.                                                                                                 printf("I:%d\r\n",i);
  309.                                                                                                 i=0;
  310.                                                                                                 delay_ms(50);
  311.                                                                                                 printf("passWord err\r\n");
  312.                                                                                                 delay_ms(50);
  313.                                                                                                 j = 1;                                                                                                                                                                                //开锁失败标志位
  314.                                                                                                 RxCounter = 0;
  315.                                                                                                 if(password_err != 0)
  316.                                                                                                 {
  317.                                                                                                         printf("Please enter again\r\n");                                                                //请再次输入
  318.                                                                                                         delay_ms(50);
  319.                                                                                                        
  320.                                                                                                         printf("The remaining %d opportunities\r\n",password_err);
  321.                                                                                                 }
  322.                                                                                                 //dengji = 0;
  323.                                                                                                 else if(password_err == 0)                                                                                                //当密码三次都输入错误时,跳出switch选择程序
  324.                                                                                                 {
  325.                                                                                                         dengji = 0;
  326.                                                                                                         i = 0;
  327.                                                                                                         j = 1;                                                                                                                                                                        //标志位,使系统进入安全模式       
  328.                                                                                                         delay_ms(50);
  329.                                                                                                         printf("The system will exit\r\n");
  330.                                                                                                         delay_ms(50);
  331.                                                                                                 }
  332.                                                                                                         //switch_successful = 1;
  333.                                                                                         }
  334.                                                                                         else
  335.                                                                                         {
  336.                                                                                                 printf("Welcome to 444\r\n");
  337.                                                                                                 dengji = 0;
  338.                                                                                                 i = 0;
  339.                                                                                                 j = 2;
  340.                                                                                                 RxCounter=0;
  341.                                                                                                 //switch_successful = 1;
  342.                                                                                                 break;
  343.                                                                                         }
  344.                                                                                 }
  345.                                                                         }//end-->while(--password_err)
  346.                                                                         break;

  347.                         }//end-->switch(member)
  348.                 }//end-->else if(dengji == 1)
  349.         }//end-->if(j == 0)
  350.                
  351.                 else if(j == 1)                                                                                                                                                        //开锁失败
  352.                 {
  353.                                 delay_ms(50);
  354.                                 printf("The system enters security mode\r\n");        //系统进入安全模式
  355.                                 delay_ms(50);
  356.                                 printf("Please disconnect\r\n");                                                                //请断开连接
  357.                         while(1)
  358.                         {
  359.                                 if(j != 1)
  360.                                         break;
  361.                         }
  362.                 }
  363.                 else if(j == 2)                                                                                                                                                        //开锁成功
  364.                 {
  365.                         times = 0;
  366.                         delay_ms(50);
  367.                         printf("The lock is Opening\r\n");                                                                //锁已开
  368.                         delay_ms(50);
  369.                         printf("Please Wait\r\n");                                                                                                //请等待
  370.                         while(1)
  371.                         {
  372.                                 times++;
  373.                                 if(times > 1000)
  374.                                 {
  375.                                         times = 0;
  376.                                         if(j != 2)
  377.                                                 break;
  378.                                 }
  379.                         }
  380.                 }
  381.                 else if(j == 3)
  382.                 {
  383.                         times = 0;
  384.                         while(1)
  385.                         {
  386.                                 times++;
  387.                                 if(times > 1000)
  388.                                 {
  389.                                         times = 0;
  390.                                         if(j != 3)
  391.                                                 break;//蓝牙断开连接时执行该函数,可以让控制器睡眠
  392.                                 }
  393.                         }
  394.                 }
  395.                
  396.         }//end-->while(1)
  397. }//end-->int main
复制代码

所有资料51hei提供下载:
STM32F103ZET6手机蓝牙控制程序.rar (308.04 KB, 下载次数: 49)


回复

使用道具 举报

ID:433444 发表于 2019-5-23 14:18 | 显示全部楼层
lanya.c中没有程序?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表