專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計實(shí)例 >> 瀏覽文章

一個簡單的AVR門禁系統(tǒng)代碼

作者:佚名   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時間:2011年10月23日   【字體:

1602和按鍵程序調(diào)試通過,編碼,密碼比較,看門,錯碼的一些處理完善后就不在上傳,這里只給自己留一個非常初級版本的備份。

#define F_CPU 4000000
#include<D:\WinAVR-20090313\avr\include\avr\io.h>
#include<D:\WinAVR-20090313\avr\include\util\delay.h>
#include<E:\AVR\LOCK\LCD1602.h> 
typedef unsigned char uchar;
typedef unsigned int uint;
static char str1[]={"begining"};//上電系統(tǒng)工作顯示
static char str2[]={"enter pass"};//開始輸入密碼開門
static char str3[]={"set pass"};//進(jìn)入編碼顯示
//端口方向,‘1’輸出,‘0’輸入。
#define BUTION_DDRN DDRD//按鍵端口方向
#define BUTION_PORT PORTD//按鍵端口
#define BUTION_DDRN_INT(); DDRD=0xf0;//按鍵端口方向初始化
#define BUTION_PORT_REL (PIND&0x0f)
static uchar sweep[4]={0xef,0xdf,0xbf,0x7f};//四行掃描端口值
uchar pass_buf[9];//密碼緩存單元
uchar bution(void);//矩陣按鍵程序
uchar set_scan(void);//編碼按鍵掃描程序
int main(void)
{uchar i,j;
Lcd_Init();//LCD初始化函數(shù) 
Lcd_SetXy(0,0);//設(shè)置顯示坐標(biāo)函數(shù) 
Lcd_PutStr(&str1[0]);
_delay_ms(500);//延時1秒
Lcd_Init();//LCD初始化函數(shù) 
Lcd_SetXy(0,0);//設(shè)置顯示坐標(biāo)函數(shù) 
Lcd_PutStr(&str2[0]);
Lcd_SetXy(0,1);//設(shè)置顯示坐標(biāo)函數(shù) 
for(;;)
  {for(i=0;i<9;i++)
    {do{j=set_scan();//編碼按鍵
     if(j==0x01)j=0xfe;break;//j=0xfe編碼按鍵按下
     j=bution();///按鍵掃描
     if((j!=0xff)&&(j!=0x0a))break;
     }while(1);
    if(j==0xfe)break;//j=0xfe編碼按鍵按下
    pass_buf=j;
    if(j==0x0b)break;
    }
  if()
  }
return 0x00;
}
uchar bution(void)//矩陣按鍵程序
{uchar i, key_buf;//按鍵值緩存單元
BUTION_DDRN_INT();//按鍵端口方向初始化
for(i=0;i<4;i++)//四行掃描
  {BUTION_PORT=sweep[i];//掃描行
  _delay_us(20);
  key_buf=BUTION_PORT_REL;
  if(key_buf!=0x0f)//有按鍵按下
    {_delay_ms(20);//延時去抖動
    if(key_buf==BUTION_PORT_REL)//按鍵確認(rèn)
      {key_buf=(~key_buf)&0x0f;
      switch(key_buf)
        {case 0x01:key_buf=0x00;break;
        case 0x02:key_buf=0x01;break;
        case 0x04:key_buf=0x02;break;
        case 0x08:key_buf=0x03;break;
        default:key_buf=0xff;
        }
      key_buf=i*4+key_buf;
      switch(key_buf)
        {case 0x00:key_buf='1';break;//"1"字按鍵
        case 0x01:key_buf='2';break;//"2"字按鍵
        case 0x02:key_buf='3';break;//"3"字按鍵
        case 0x03:key_buf=0xff;break;//""字按鍵

        case 0x04:key_buf='4';break;//"4"字按鍵
        case 0x05:key_buf='5';break;//"5"字按鍵
        case 0x06:key_buf='6';break;//"6"字按鍵
        case 0x07:key_buf=0xff;break;//""字按鍵

        case 0x08:key_buf='7';break;//"7"字按鍵
        case 0x09:key_buf='8';break;//"8"字按鍵
        case 0x0a:key_buf='9';break;//"9"字按鍵
        case 0x0b:key_buf=0xff;break;//""字按鍵

        case 0x0c:key_buf='*';break;//"*"字按鍵
        case 0x0d:key_buf='0';break;//"0"字按鍵
        case 0x0e:key_buf='#';break;//"#"字按鍵
        case 0x0f:key_buf=0xff;break;//""字按鍵
        default:key_buf=0xff;
        }
      while(BUTION_PORT_REL!=0x0f)
      {_delay_us(20);
      while(BUTION_PORT_REL!=0x0f);
      }//等待按鍵彈起
      break;//從四次掃描中挑出
      }
    else key_buf=0xff;
    }
  else key_buf=0xff;
  }
return key_buf;
}
uchar set_scan(void)
{
}

關(guān)閉窗口

相關(guān)文章