溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

基于51單片機(jī)、串口通信、ASP.NET的密碼鎖系統(tǒng)

發(fā)布時間:2020-07-11 00:09:47 來源:網(wǎng)絡(luò) 閱讀:394 作者:蓬萊仙羽 欄目:編程語言

實(shí)現(xiàn)的功能:密碼鎖,紅外控制,溫度計功能等

連線:鍵盤接P0,P2接數(shù)碼管,接上LCD,P1.0接蜂鳴器,P1.2-P1.7接步進(jìn)電機(jī)

#include <reg52.h>
  #include <intrins.h>
typedef unsigned int uint;
typedef unsigned char uchar;


bit FlagC=1;    //接受和發(fā)送的標(biāo)志位
sbit P10=P1^0;   //控制蜂鳴器響
sbit IRIN = P3^2;         //紅外接收器數(shù)據(jù)線
uchar IRCOM[7];
uint nn=11;//用來判斷紅外發(fā)送的信號是數(shù)字幾
//////溫度液晶////////////////
typedef bit BOOL;


sbit DQ = P3 ^ 7;


uchar Temp_Value[] = { 0x00, 0x00 };
uchar Display_Digit[] = { 0, 0, 0, 0 };


sbit LCD_RW = P2 ^ 5; //寫數(shù)據(jù)?
sbit LCD_RS = P2 ^ 6;
sbit LCD_EP = P2 ^ 7; //使能?
uchar num;
int temper = 0;
int CurrentT = 0;
bit flag;


void delayT(uchar t) {
    while (t--)
        ;
}


void init_ds18b20() {
    //    uchar n;
DQ = 1;
    _nop_();
    DQ = 0;
    delayT(80);
    _nop_();
    DQ = 1;
    delayT(14);
    _nop_();
    _nop_();
    _nop_();


    if (DQ == 0)
        flag = 1; //detect 1820 success!
    else
        flag = 0; //detect 1820 fail!
    delayT(20); //20
    _nop_();
    _nop_();
    DQ = 1;
}


void write_byte(uchar dat) {
    uchar i;
    for (i = 0; i < 8; i++) {
        DQ = 0;
        _nop_();
        DQ = dat & 0x01; //從最低位開始寫
        delayT(3);
        _nop_();
        _nop_();
        DQ = 1;
        dat >>= 1;
    }
    //    delay(4);
}


uchar read_byte() {
    uchar i, value = 0;
    for (i = 0; i < 8; i++) {
        DQ = 0;
        value >>= 1;
        DQ = 1;
        if (DQ)
            value |= 0x80;
        delayT(2);
        _nop_();
    }
    return value;
}


int readtemperature() {
    uchar templ, temph;
    //    int temp;
    init_ds18b20();
    write_byte(0xcc); //跳過ROM
    write_byte(0x44); //啟動溫度測量
    delayT(300);


    init_ds18b20();
    write_byte(0xcc);
    write_byte(0xbe);
    //    Temp_Value[0] = ReadOneByte();
    //    Temp_Value[1] = ReadOneByte();
    templ = read_byte();
    temph = read_byte();
    //CurrentT = ((templ&0xf0)>>4) | ((temph&0x07)<<4);
    CurrentT = temph*256 +templ;
    CurrentT = CurrentT * 62.5;
    return CurrentT;
}


/****************側(cè)忙函數(shù)************************/
BOOL lcd_bz() {
    BOOL result;
    LCD_RS = 0;
    LCD_RW = 1;
    LCD_EP = 1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    result = (BOOL) (P0 & 0x80); //檢測P0最高位是否為1
    LCD_EP = 0;
    return result;
}
/****************寫命令函數(shù)************************/
void write_com(uchar cmd) {
    while (lcd_bz())
        ;
    LCD_RS = 0;
    LCD_RW = 0;
    //    LCD_EP = 0;
    _nop_();
    _nop_();
    P0 = cmd;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    LCD_EP = 1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    LCD_EP = 0;
}


void lcd_pos(uchar pos) {
    write_com(pos | 0x80);
}


/****************寫數(shù)據(jù)函數(shù)************************/
void write_data(uchar dat) { //寫入字符顯示數(shù)據(jù)到LCD
    while (lcd_bz())
        ;
    LCD_RS = 1;
    LCD_RW = 0;
    //    LCD_EP = 0;
    _nop_();
    _nop_();
    P0 = dat;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    LCD_EP = 1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    LCD_EP = 0;
}


void initT() {
    //    dula=0;
    //    wela=0;
    LCD_EP = 0;
    write_com(0x38); //16*2顯示
    write_com(0x0e); //開顯示;顯示光標(biāo),光標(biāo)不閃爍
    write_com(0x06); //光標(biāo)自動增加111
    write_com(0x01); //顯示清屏
    //    write_com(0x80+0x10);
}
//////////////////////////////


unsigned int ReData;
uchar num=0;
const char Message[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};    //要發(fā)送的數(shù)據(jù)
char Get_Mes[8]={0x03};               //獲取到的數(shù)據(jù)
uchar i=0;
uchar Chose;//判斷選擇哪一個功能選項(xiàng)
//此表為 LED 的字模, 共陰數(shù)碼管 0-F  
uchar code   LEDMAP[] = {~0x3f, ~0x06, ~0x5b, ~0x4f, ~0x66, ~0x6d, ~0x7d, ~0x07,~0x7f, ~0x6f, ~0x77, ~0x7c, ~0x39, ~0x5e, ~0x79, ~0x71};  //段碼控制
uint LEDN[16];
int n=0;
void delay(uint);


void Send_data(char p[])   //發(fā)送數(shù)據(jù)
{
    uchar i;
    for(i=0;i<1;i++)
    {
        SBUF = p[i];            //SUBF接受/發(fā)送緩沖器(又叫串行通信特殊功能寄存器)
        while(!TI);                // 等特數(shù)據(jù)傳送    (TI發(fā)送中斷標(biāo)志)
        TI = 0;                    // 清除數(shù)據(jù)傳送標(biāo)志    
    } 



void zhuanhuan(char c)     //將PC傳過來的16進(jìn)制數(shù)轉(zhuǎn)化成對應(yīng)的十進(jìn)制數(shù)
{
       switch(c)
      {
        case 0x00:
        {
          LEDN[0]=0;
          break;
        }
      }
}


void Get_data(char p[])    //接受數(shù)據(jù)到數(shù)組中
{
    
    i=i%8;
    i++;
    P2=LEDMAP[SBUF];
    p[i]=P2;
    FlagC=1;
    while(!RI);                // 等特數(shù)據(jù)傳送    (TI發(fā)送中斷標(biāo)志)
    RI = 0;                    // 清除數(shù)據(jù)傳送標(biāo)志    
}
//返回一個選擇項(xiàng)
char Get_Chose()
{
    return SBUF;
}


void delay(uint z)
{
    uchar j;
    for(z;z>0;z--)
        for(j=0;j<200;j++)
            ;    
}


void init()
{
    SCON = 0x50;      //REN=1允許串行接受狀態(tài),串口工作模式1                      
                    
    PCON|= 0x80;
                                                             
    TMOD|= 0x20;      //定時器工作方式2
    TH1 = 0xF3;                    // //baud*2  /*  波特率4800、數(shù)據(jù)位8、停止位1。效驗(yàn)位無 (12M)
    TL1 = 0xF3;         
    TR1  = 1;                                                             
    ES   = 1;        //開串口中斷                  
    EA   = 1;        // 開總中斷 
}


//蜂鳴器響
void beep(uint t)
{
    uint time=0;
    for(time;time<t;time++)
    {
        P10=1;
        delay(100);
        P10=0;
        delay(100);
    }
}


/////////紅外//////////////////




void beeph()
{
  unsigned char i;
  for (i=0;i<100;i++)
   {
   delay(4);
   P10=!P10;                 //BEEP取反
   } 
  P10=1;                      //關(guān)閉蜂鳴器
}


void delayh(unsigned char x)    //x*0.14MS
{
 unsigned char i;
  while(x--)
 {
  for (i = 0; i<13; i++) {}
 }
}


void delay1h(int ms)
{
 unsigned char y;
  while(ms--)
 {
  for(y = 0; y<250; y++)
  {
   _nop_();
   _nop_();
   _nop_();
   _nop_();
  }
 }
}




void IR_IN() interrupt 0 using 0
{
  unsigned char j,k,N=0;
     EX0 = 0;   
     delayh(15);
     if (IRIN==1) 
     { EX0 =1;
       return;
      } 
                           //確認(rèn)IR信號出現(xiàn)
  while (!IRIN)            //等IR變?yōu)楦唠娖?,跳過9ms的前導(dǎo)低電平信號。
    {delayh(1);}


 for (j=0;j<4;j++)         //收集四組數(shù)據(jù)
 { 
  for (k=0;k<8;k++)        //每組數(shù)據(jù)有8位
  {
   while (IRIN)            //等 IR 變?yōu)榈碗娖?,跳過4.5ms的前導(dǎo)高電平信號。
     {delayh(1);}
    while (!IRIN)          //等 IR 變?yōu)楦唠娖?br>      {delayh(1);}
     while (IRIN)           //計算IR高電平時長
      {
    delayh(1);
    N++;           
    if (N>=30)
     { EX0=1;
     return;}                  //0.14ms計數(shù)過長自動離開。
      }                        //高電平計數(shù)完畢                
     IRCOM[j]=IRCOM[j] >> 1;                  //數(shù)據(jù)最高位補(bǔ)“0”
     if (N>=8) {IRCOM[j] = IRCOM[j] | 0x80;}  //數(shù)據(jù)最高位補(bǔ)“1”
     N=0;
  }//end for k
 }//end for j
   
   if (IRCOM[2]!=~IRCOM[3])
   { EX0=1;
     return; }


   IRCOM[5]=IRCOM[2] & 0x0F;     //取鍵碼的低四位
   IRCOM[6]=IRCOM[2] >> 4;       //右移4次,高四位變?yōu)榈退奈?br>

   if(IRCOM[5]>9)
    { IRCOM[5]=IRCOM[5]+0x37;}
   else
      IRCOM[5]=IRCOM[5]+0x30;


   if(IRCOM[6]>9)
    { IRCOM[6]=IRCOM[6]+0x37;}
   else
      IRCOM[6]=IRCOM[6]+0x30;


    //將紅外信號轉(zhuǎn)換成數(shù)字
    if(IRCOM[6]=='4'&&IRCOM[5]=='5')
    {
         nn=10;//關(guān)閉
    }
    if(IRCOM[6]=='1'&&IRCOM[5]=='6')
    {
         nn=0;
    }
    if(IRCOM[6]=='0'&&IRCOM[5]=='C')
    {
         nn=1;
    }
    if(IRCOM[6]=='1'&&IRCOM[5]=='8')
    {
         nn=2;
    }
    if(IRCOM[6]=='5'&&IRCOM[5]=='E')
    {
         nn=3;
    }
    if(IRCOM[6]=='0'&&IRCOM[5]=='8')
    {
         nn=4;
    }
    if(IRCOM[6]=='1'&&IRCOM[5]=='C')
    {
         nn=5;
    }
    if(IRCOM[6]=='5'&&IRCOM[5]=='A')
    {
         nn=6;
    }
    if(IRCOM[6]=='4'&&IRCOM[5]=='2')
    {
         nn=7;
    }
    if(IRCOM[6]=='5'&&IRCOM[5]=='2')
    {
         nn=8;
    }
    if(IRCOM[6]=='4'&&IRCOM[5]=='A')
    {
         nn=9;//關(guān)閉
    }


     beeph();
     EX0 = 1; 



/////////////////////////////////


void main (void) 
{
    init();
    Chose=Get_Chose();
    if(Chose=='0')          //chose為0的時候是懸空狀態(tài)
    {
      P2=LEDMAP[0];
      delay(5000);
    }
    if(Chose=='P')
    {
        while(1)
        { 
          if(P2!=~0x5e)       //如果按下的不是d(退出鍵),則繼續(xù),如果是d(退出鍵),則退出,繼續(xù)選擇其他的功能
          {
              if (FlagC==1)
              {    
                  Send_data(Message);
                 FlagC=0;
              }
              else
              {
                  Get_data(Get_Mes);
                //beeph();
                  FlagC=1;
              }
          }
          else
          {
              P2=0x00;
               break;
          }
         }
      }
     if(Chose=='T')
     {
        
           initT();
        while (1) 
        {
            temper = readtemperature();
            lcd_pos(0);
            if (temper < 0) {
                write_data('-');
                temper = 0 - temper;
            } else
                write_data('+');
    
            lcd_pos(1);
            write_data((temper) / 10000 + 0x30);
    
            lcd_pos(2);
            write_data(((temper) % 10000 )/ 1000 + 0x30);
    
            lcd_pos(3);
            write_data('.');
    
            lcd_pos(4);
            write_data(((temper) % 10000 % 1000 )/ 100 + 0x30);
    
            lcd_pos(5);
            write_data(((temper) %10000 %1000 % 100 )/10 + 0x30);
            /*lcd_pos(6);
            write_data((temper) %10000 %1000 % 100 % 10 + 0x30);  */
            lcd_pos(6);
            write_data(0xdf);
            lcd_pos(7);
            write_data('C');
            if(Chose!='T')
            {
                /* uint jj=0;
                for(jj;jj<8;jj++)
                {
                     write_data(' '); 
                }
                */
                 break;
            }
        }
     }
     if(Chose=='R')        //紅外控制
     {
          /*P2=LEDMAP[1];
        delay(5000); */
        IRIN=1;//初始化紅外P3.2
        P10=1;//初始化蜂鳴器
        delay1h(10);
        IE = 0x81;                 //允許總中斷中斷,使能 INT0 外部中斷
        TCON = 0x01;               //觸發(fā)方式為脈沖負(fù)邊沿觸發(fā)
        nn=0;
        P2=LEDMAP[nn];
        while(1)
        {
            if(nn==10)
            {
                 EA=0;//關(guān)閉中斷
                break;    
            }
            else{
                 P2=LEDMAP[nn];
            }
            if(Chose!='R')
            {break;}
        }
     }
}
/****************************************************
               串口中斷程序
******************************************************/
void ser_int (void) interrupt 4 using 1
{
    
}


asp.net頁面

插圖

基于51單片機(jī)、串口通信、ASP.NET的密碼鎖系統(tǒng)

論文及詳細(xì)源碼:http://download.csdn.net/detail/s10141303/5614395


==================== 迂者 丁小未 CSDN博客專欄=================

MyBlog:http://blog.csdn.net/dingxiaowei2013              MyQQ:1213250243

MyTel:13262983383 

====================== 相互學(xué)習(xí),共同進(jìn)步 ===================


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI