📄 keypad decode.c
字号:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//~ To test PIC16F877 io ports A,B,C,D,E; using RE2 as switch. ~//
//~ Created by Henry Tan for EE2001. All rights reserved. ~//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
#include "io16f877.h" // The hardware register definition file
// Define the pins to use
#define Kpad1 RB0
#define Kpad2 RB1
#define Kpad3 RB2
#define Kpad4 RB3
//Global variables declare for the interrupt service routine
int value = 0x8000;
volatile int inst_count = 0, inst_code = 0, test_value = 0;
volatile unsigned char out0 = 0, out1 = 0, out2 = 0, out3 = 0;
volatile char key;
//Functions prototype
void delayUs(int count);
void portIO(void);
char Keypad_decode(void);
// Initialize all port I/O configuration and enable all the needed interrupt signal
void portIO(void)
{
TRISB = 0x8F; // set port B pin 7 as input, others as output
TRISC = 0x00; // set port C as output
PORTC = 0x00; // clear port C
GIE = 1; // global interrupt enabled
PEIE = 1; // peripheral interrupt enabled
RBIE = 1; // port B interrupt enabled
RBIF = 0; // clear port B interrupt flag
}
//This function is called to cause a delay of count Us.
void delayUs(int count)
{
int i, j;
for (i=0; i<count; i++)
{
for (j=0; j<5; j++);
}
}
char Keypad_decode(void)
{
int key_value = 0;
key_value = Kpad4*8 + Kpad3*4 + Kpad2*2 + Kpad1;
switch (key_value)
{
case 0:
return '1';
case 1:
return '2';
case 2:
return '3';
case 3:
return 'A';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return 'B';
case 8:
return '7';
case 9:
return '8';
case 10:
return '9';
case 11:
return 'C';
case 12:
return '*';
case 13:
return '0';
case 14:
return '#';
default:
return 'D';
}
}
#pragma vector=0x04
__interrupt void isr(void)
{
if (RBIF == 1) // if pushbutton is pressed
{
RBIF = 0;
key = Keypad_decode();
switch (key)
{
case '1':
out0 = 1;
out1 = 0;
out2 = 0;
out3 = 0;
break;
case '2':
out0 = 0;
out1 = 1;
out2 = 0;
out3 = 0;
break;
case '3':
out0 = 1;
out1 = 1;
out2 = 0;
out3 = 0;
break;
case '4':
out0 = 0;
out1 = 0;
out2 = 1;
out3 = 0;
break;
case '5':
out0 = 1;
out1 = 0;
out2 = 1;
out3 = 0;
break;
case '6':
out0 = 0;
out1 = 1;
out2 = 1;
out3 = 0;
break;
case '7':
out0 = 1;
out1 = 1;
out2 = 1;
out3 = 0;
break;
case '8':
out0 = 0;
out1 = 0;
out2 = 0;
out3 = 1;
break;
case '9':
out0 = 1;
out1 = 0;
out2 = 0;
out3 = 1;
break;
case '0':
out0 = 0;
out1 = 0;
out2 = 0;
out3 = 0;
break;
default:
out0 = 1;
out1 = 1;
out2 = 1;
out3 = 1;
}
} r
}
void main (void)
{
portIO();
while (1)
{
RC0 = out0;
RC1 = out1;
RC2 = out2;
RC3 = out3;
}//endWhile
}//endMain
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -