?? keypad_4x4.c
字號:
#include <18F4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000,crystal)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C2, parity=N, bits=8,stop=1)
#include <lcd.c>
////define port
#byte portb = 0xf81
#byte porte = 0xf84
////end define port
#bit portb_2 = 0xf81.2
#bit portb_3 = 0xf81.3
#bit porte_1 = 0xf84.1
#bit porte_2 = 0xf84.2
////define tris
#byte tris_b = 0xf93
#byte tris_e = 0xf96
////end define tris
#use fast_io(B)
#use fast_io(E)
static int1 key_pressed = FALSE;
static int1 flag_idle = TRUE;
static int8 old_b_state;
static char key = 0;
void kbd_find_key(void) {
int pin, b_state, delay = 10;
delay_ms(50); // debounce
disable_interrupts(INT_RB);
//kiem tra ngat co phat sinh tren pin 4 khong?
pin = 4;
if (!bit_test(old_b_state,pin))
{
//xuat pin 0 la high
portb_3 = 0;
portb_2 = 0;
porte_2 = 0;
porte_1 = 1;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = 'D';
}
//xuat pin 1 la high
portb_3 = 0;
portb_2 = 0;
porte_2 = 1;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = 'C';
}
//xuat pin 2 la high
portb_3 = 0;
portb_2 = 1;
porte_2 = 0;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = 'B';
}
//xuat pin 3 la high
portb_3 = 1;
portb_2 = 0;
porte_2 = 0;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if(bit_test(b_state,pin))
{
key = 'A';
}
}
//kiem tra ngat co phat sinh tren pin 5 khong?
pin = 5;
if (!bit_test(old_b_state,pin)) {
//xuat pin 0 la high
portb_3 = 0;
portb_2 = 0;
porte_2 = 0;
porte_1 = 1;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = '#';
}
//xuat pin 1 la high
portb_3 = 0;
portb_2 = 0;
porte_2 = 1;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = '9';
}
//xuat pin 2 la high
portb_3 = 0;
portb_2 = 1;
porte_2 = 0;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = '6';
}
//xuat pin 3 la high
portb_3 = 1;
portb_2 = 0;
porte_2 = 0;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if(bit_test(b_state,pin))
{
key = '3';
}
}
//kiem tra ngat co phat sinh tren pin 6 khong?
pin = 6;
if (!bit_test(old_b_state,pin))
{
//xuat pin 0 la high
portb_3 = 0;
portb_2 = 0;
porte_2 = 0;
porte_1 = 1;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = '0';
}
//xuat pin 1 la high
portb_3 = 0;
portb_2 = 0;
porte_2 = 1;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = '8';
}
//xuat pin 2 la high
portb_3 = 0;
portb_2 = 1;
porte_2 = 0;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = '5';
}
//xuat pin 3 la high
portb_3 = 1;
portb_2 = 0;
porte_2 = 0;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if(bit_test(b_state,pin))
{
key = '2';
}
}
//kiem tra ngat co phat sinh tren pin 7 khong?
pin = 7;
if (!bit_test(old_b_state,pin))
{
//xuat pin 0 la high
portb_3 = 0;
portb_2 = 0;
porte_2 = 0;
porte_1 = 1;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = '*';
}
//xuat pin 1 la high
portb_3 = 0;
portb_2 = 0;
porte_2 = 1;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = '7';
}
//xuat pin 2 la high
portb_3 = 0;
portb_2 = 1;
porte_2 = 0;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if (bit_test(b_state,pin))
{
key = '4';
}
//xuat pin 3 la high
portb_3 = 1;
portb_2 = 0;
porte_2 = 0;
porte_1 = 0;
delay_us(delay);
b_state = portb;
if(bit_test(b_state,pin))
{
key = '1';
}
}
portb = old_b_state;
portb_3 = 0;
portb_2 = 0;
porte_2 = 0;
porte_1 = 0;
enable_interrupts(INT_RB);
}
void kbd_print_key(void){
kbd_find_key();
if (key != 0)
{
lcd_putc(key);
key = 0;
}
}
#INT_RB
void int_rb_function(void)
{
if ((old_b_state & 0xF0) != (portb & 0xF0))
{
old_b_state = portb;
if (flag_idle == true)
{
key_pressed = TRUE;
flag_idle = false;
}
if ((portb & 0xF0) == 0xF0)
{
flag_idle = true;
}
}
}
void kbd_init()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_RB);
//port_b_pullups(TRUE);
//set in out cho cac port B
//B7-B4 input B3-B2 output B1-B0 khong su dung
//1111 00 default
tris_b = tris_b & 0x03; //chi lay B1 va B0
tris_b = tris_b + 0xF0; //B4-B7 input, B2-B3 output
//set in out cho cac port C
//C7-C4 khong co C3 khong dung C2-C1 output C0 khong dung
//default default 00 default
tris_e = tris_e & 0xF9; //C1 va C2 la output, cac pin con lai no change
///////////
portb = portb & 0xF3; //xuat B2-B3 la low
porte = porte & 0xF9; //xuat C1-C2 la low
//luu trang thai B4-B7 cua portb
old_b_state = portb;
}
void main() {
lcd_init();
kbd_init();
lcd_putc("Ready");
while (TRUE)
{
restart_wdt();
if (key_pressed)
{
key_pressed = FALSE;
kbd_print_key();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -