?? keyscan119.c
字號:
//**************************************************************/
/*鍵盤源程序,源文件名KEYSCAN.c。使用MPLAB C18 編譯器。*/
//使用PIC18F6527高性能單片機
//**************************************************************/
//#include <pic18fxx20.h> /* head file*/
#include <p18f6527.h> //PIC18F452 head file
/*********************DEFINE****************************/
//RA0低壓檢測報警,RA2 光耦OPEN,RA3 光耦CLOSE,RA4 光耦LOCK
//RA5 SW17 R, RA1不知做什么
#define low_voltage PORTAbits.RA0
#define pos_open PORTAbits.RA2
#define pos_close PORTAbits.RA3
#define pos_lock PORTAbits.RA4
#define key_R PORTAbits.RA5
//RB0 SW3 OPEN/CLO, RB1 SW2 D , RB3 SW4 N
//RB4 指紋開關, RB2 鍵盤開關
//RB5 外部強行闖入 , RB6 仿真,RB7 仿真
#define key_open_close PORTBbits.RB0
#define key_D PORTBbits.RB1
#define key_N PORTBbits.RB3
#define outkey_finger PORTBbits.RB4
#define outkey_keyboard PORTBbits.RB2
#define force_in PORTBbits.RB5
//OUT RC0 讓dsp重置 刪除已有指紋 /reset
//OUT RC1 spi開始(猜測) spiss
//OUT RC2 外蓋打開 從sleep恢復 /pwr_on
//IN/OUT RC3 spi clock sck
//IN RC4 master in slave out miso
#define dsp_reset PORTCbits.RC0
#define dsp_spiss PORTCbits.RC1
#define dsp_pwr_on PORTCbits.RC2
#define dsp_sck PORTCbits.RC3
#define dsp_miso PORTCbits.RC4
#define dsp_mosi PORTCbits.RC5
//IN RD0 成功標志 錄入或登入成功標志
//OUT RD7 MOT-
#define dsp_succes PORTDbits.RD0
#define mot_down PORTDbits.RD7
#define confim_key 9
//RE0 VCC_IO 接通 與VCC_6V 高有效
//RE7 VCC_IO2 接通 與VCC_6V 高有效
//RE2 KID電源 接通 與VCC_6V
//RE1 MOT+
//RE3 BUZZ聲音控制 沒有用到
//RE4 鍵盤面板燈
//RE5 鍵盤藍色燈
//RE6 下拉了 沒有用
#define vcc_io PORTEbits.RE0
#define vcc_io2 PORTEbits.RE7
#define vcc_kid PORTEbits.RE2
#define mot_up PORTEbits.RE1
//#define buzz2 PORTEbits.RE3
#define keyboard_red PORTEbits.RE5
#define keyboard_blue PORTEbits.RE4
//RG0 buzz聲音控制,RG1 指紋RED, RG2 指紋BLUE
#define buzz PORTGbits.RG0
#define finger_red PORTGbits.RG1
#define finger_blue PORTGbits.RG2
//*********************DEFINE*********************//
void initialize_io(void); //初始化i/o口
void initialize(void); //初始化其他寄存器
void initialize_eeprom(void); //初始化eeprom
void delay(int delaytime); //延時
char key_scan(void); //鍵盤掃描
char keyserve(void); //鍵盤辨別
char epprom_read(int epprom_r_addr); //讀epprom
void epprom_write(int epprom_w_addr,int epprom_w_data); //寫epprom
void sound_buzzer(int sound); //發聲
void key_stop(void); //判斷鍵盤是否松開
void mot_move_open(void); //驅動電機開門
void mot_move_close(void); //驅動電機關門
char password_confirm(void); //密碼驗證
char password_input(void); //密碼輸入
void sleep_prepare(void); //sleep準備,低功耗設置
void finger_confirm(void); //指紋確認
void finger_input(void); //指紋輸入
void finger_del(void); //指紋刪除
void force_in_alarm(void);
void interrupt_handler_high(void); //高級中斷
void interrupt_handler_low(void);
//全局變量設置
char current_key; //當前得到按下去的key
int current_epprom; //當前從epprom中得到的參數
int current_password[9]; //當前password
int sleep_cycle=0; //設置睡眠時間
int password_confirm_times=0; //設置錯誤次數
char sound_enable=1; //
///* Set configuration bits for use with ICD2 / PICDEM2 PLUS Demo Board:*/
//#pragma romdata CONFIG
//_CONFIG_DECL(_CONFIG1H_DEFAULT,// & HS,
//_CONFIG2L_DEFAULT,
//_CONFIG2H_DEFAULT ,//& _WDT_OFF_2H,
//_CONFIG3H_DEFAULT,
//_CONFIG4L_DEFAULT ,//& _LVP_OFF_4L,
//_CONFIG5L_DEFAULT,
//_CONFIG5H_DEFAULT,
//_CONFIG6L_DEFAULT,
//_CONFIG6H_DEFAULT,
//_CONFIG7L_DEFAULT,
//_CONFIG7H_DEFAULT);
//#pragma romdata
//high_vector_section AT 0x8,
#pragma code high_vector_section=0x8
void high_vector (void)
{
_asm
GOTO interrupt_handler_high
_endasm
}
#pragma code
//low_vector_section AT 0x18
#pragma code low_vector_section=0x18
void
low_vector (void)
{
_asm
GOTO interrupt_handler_low
_endasm
}
#pragma code
void main(void)
{
initialize ();
initialize_io ();
initialize_eeprom();
INTCONbits.GIE=1; //高優先級中斷允許
INTCONbits.PEIE=1; //低優先級中斷允許
while(1)
;
}
char key_scan(void) //掃描鍵盤輸入的一個字符,返回值
{
/* RF4 COL0 input
RF5 COL1
RF6 COL2
RF7 COL3
RF3 ROW0 output
RF1 ROW1
RF2 ROW2
*/
int i,j;
PORTFbits.RF1 = 1;
PORTFbits.RF2 = 1;
PORTFbits.RF3 = 1;
// for(i=0;i<10000000;i++)
while(outkey_keyboard)
{
if(PORTFbits.RF4||PORTFbits.RF5||
PORTFbits.RF6||PORTFbits.RF7)
{
delay(10000);
if(PORTFbits.RF4||PORTFbits.RF5||
PORTFbits.RF6||PORTFbits.RF7)
{
current_key=keyserve();
PORTFbits.RF1 = 0;
PORTFbits.RF2 = 0;
PORTFbits.RF3 = 0;
return(1);
}
else
{
delay(100);
PORTFbits.RF1 = 0;
PORTFbits.RF2 = 0;
PORTFbits.RF3 = 0;
}
}
else
delay(100);
}
return(0);
}
char keyserve(void)
{
char keyserve_key;
PORTFbits.RF1 = 0;
PORTFbits.RF2 = 0;
PORTFbits.RF3 = 1;
if(PORTFbits.RF4 == 1)
{
keyserve_key=1;
key_stop();
}
else if(PORTFbits.RF5 == 1)
{
keyserve_key=4;
key_stop();
}
else if(PORTFbits.RF6 == 1)
{
keyserve_key=7;
key_stop();
}
else if(PORTFbits.RF7 == 1)
{
keyserve_key=10;
key_stop();
} //error;
PORTFbits.RF1 = 1;
PORTFbits.RF2 = 0;
PORTFbits.RF3 = 0;
if(PORTFbits.RF4 == 1)
{
keyserve_key=2;
key_stop();
}
else if(PORTFbits.RF5 == 1)
{
keyserve_key=5;
key_stop();
}
else if(PORTFbits.RF6 == 1)
{
keyserve_key=8;
key_stop();
}
else if(PORTFbits.RF7 == 1)
{
keyserve_key=0;
key_stop();
}
PORTFbits.RF1 = 0;
PORTFbits.RF2 = 1;
PORTFbits.RF3 = 0;
if(PORTFbits.RF4 == 1)
{
keyserve_key=3;
key_stop();
}
if(PORTFbits.RF5 == 1)
{
keyserve_key=6;
key_stop();
}
if(PORTFbits.RF6 == 1)
{
keyserve_key=9;
key_stop();
}
if(PORTFbits.RF7 == 1)
{
keyserve_key=20;
key_stop();
}
return(keyserve_key);
}
void key_stop()
{
while(1)
{
if(PORTFbits.RF4||PORTFbits.RF5||
PORTFbits.RF6||PORTFbits.RF7)
delay(100);
else
{
delay(10000);
if(PORTFbits.RF4||PORTFbits.RF5||
PORTFbits.RF6||PORTFbits.RF7)
delay(100);
else
break;
}
}
}
char epprom_read(int epprom_r_addr)
{
EEADRH = 0; //Upper bits of Data Memory Address to write
EEADR = epprom_r_addr; //Lower bits of Data Memory Address to write
EECON1bits.EEPGD = 0; //Point to DATA memory
EECON1bits.CFGS = 0; // Access EEPROM
EECON1bits.RD = 1;; //EEPROM Read
return(EEDATA);
}
void epprom_write(int epprom_w_addr,int epprom_w_data)
{
EEADRH = 0; //Upper bits of Data Memory Address to write
EEADR = epprom_w_addr; //Lower bits of Data Memory Address to write
EEDATA = epprom_w_data; //Data Memory Value to write
EECON1bits.EEPGD = 0; //Point to DATA memory
EECON1bits.CFGS = 0; // Access EEPROM
EECON1bits.WREN = 1; // Enable writes
INTCONbits.GIE = 0; // Disable Interrupts
INTCONbits.PEIE=0;
EECON2 = 0x55; //Write 55h
EECON2 = 0xAA; //Write 0AAh
EECON1bits.WR = 1 ; //Set WR bit to begin write
INTCONbits.GIE = 1; //Enable Interrupts
INTCONbits.PEIE=1;
while (!PIR2bits.EEIF) // WRITE step #8
;
PIR2bits.EEIF = 0; // WRITE step #9
EECON1bits.WREN = 0; //Disable writes on write complete (EEIF set)
}
void sound_buzzer(int sound)
{
unsigned char fre_repeat,s_max,s_min;
int i,j;
//同一頻率重復次數,最高頻率,最低頻率,循環變量i,j
vcc_io2 = 1; //buzz and dsp
fre_repeat= sound; //同一頻率循環4次
s_max=0x93; //聲音最高頻率
s_min=0x47; //聲音最低頻率
if(sound==0)
sound=1;
if(sound_enable)
for(i=s_max-s_min;i>0;i--) //輸出最高頻率與最低頻率內的每一個頻率的聲音
{
for(j=fre_repeat;j>0;j--) //每個聲音循環4次
{
buzz=1; //輸出聲音
delay(s_min+i); //延長一段時間
buzz=0; //關閉聲音
delay(s_min+i); //延長一段時間
}
}
}
void mot_move_open()
{
//判斷是否打開到位
int i;
if(pos_open == 0&&pos_lock==0) //沒有到位
{
for(i=0;i<10;i++)
{
if(pos_open == 0 )
{
vcc_kid=1; //kid 電源打開
mot_down=1; //反轉
mot_up = 0; //正轉
sound_buzzer(4);
delay(100000); //需要調整的時間
}
}
if(pos_open == 0) //沒有到位報警
{
for(i=0;i<3;i++)
sound_buzzer(10);
}
}
sound_buzzer(2);
sound_buzzer(1);
mot_down=0; //反轉
mot_up=0; //正轉
vcc_kid=0;
}
void mot_move_close()
{
//判斷是否到位
int i;
if(pos_close == 0&&pos_lock==0) //沒有到位
{
for(i=0;i<10;i++)
{
if(pos_close == 0 )
{
vcc_kid=1; //kid 電源打開
mot_down=0; //反轉
mot_up = 1; //正轉
sound_buzzer(4);
delay(100000);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -