标题: 新人求问 串口密码锁 我写的单片机程序有问题 [打印本页]

作者: 身向榆关那畔行    时间: 2017-5-8 21:20
标题: 新人求问 串口密码锁 我写的单片机程序有问题
题目 串口密码锁
基本要求:串口发送正确密码,单片机会返回ok,并打开锁(用led亮灭代替),发送错误密码返回error

我照猫画虎的写了一串可是不知道哪里有问题

  1. #include "reg52.h"
  2. #include <string.h>

  3. typedef unsigned int u16;        
  4. typedef unsigned char u8;
  5. unsigned char mima[]="123456\r",a[10];
  6. unsigned char tab2[]=" the password is corret";
  7. unsigned char tab3[]=" the password is incorret ,please re-enter your password!";


  8. void UsartInit()
  9. {
  10.         SCON=0X50;                        
  11.         TMOD=0X20;               
  12.         PCON=0X80;               
  13.         TH1=0XF3;                        
  14.         TL1=0XF3;
  15.         ES=1;                                                
  16.         EA=1;                                                
  17.         TR1=1;                        
  18. }
  19. void delay(u16 i)
  20. {
  21.         while(i--);        
  22. }

  23. void main()
  24. {        
  25.         UsartInit();  
  26.         while(1);
  27. }
  28. void send_char(unsigned char c)
  29. {   SBUF=c;
  30.                 while(TI==0);
  31.                 TI=0;
  32. }
  33. void Usart() interrupt 4
  34. {
  35.         u16 i;
  36.         for(i=0;a[i]!='\r';i++)
  37.         {
  38.                 if(RI==1)
  39.                 {
  40.                         a[i]=SBUF;
  41.                 }
  42.         }
  43.         
  44.         if(strcmp(a,mima)==0)
  45.         {
  46.                 send_char(tab2);
  47.         }
  48.         else
  49.                 send_char(tab3);
  50.         RI=0;
  51. }
复制代码

作者: HUANGHFS    时间: 2017-5-8 22:09
应该是参数传递错误。
作者: HUANGHFS    时间: 2017-5-8 22:10
应该是参数传递错误。




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1