Chương trình hiển thị phím số ra đèn 7 đoạn (DÙNG INTERRUPT)
Chương trình hiển thị phím số trên bàn phím 4x3 ra đèn 7 đoạn (DÙNG INTERRUPT).
#include <16F877A.h>
#fuses NOWDT, XT
#fuses NOLVP // important
#use delay(clock=4000000)
#include <kbd.c> // in PICC\Drivers
// 0 1 2 3 4 5 6 7 8 9
byte const DIGITS[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f };
///////////////////////////////////////////////////////////
/* private */void off_on_led_transistor() {
output_low(PIN_D1);
delay_ms(1);
output_high(PIN_D1);
}
///////////////////////////////////////////////////////////
void display(int8 digit) {
output_c(DIGITS[digit] ^ 0xff);
off_on_led_transistor();
}
///////////////////////////////////////////////////////////
int8 char_to_digit(char c) {
return c & 0b00001111; // first 4 bits only
}
///////////////////////////////////////////////////////////
int1 digit_key_pressed(char key) {
byte pattern;
pattern = 0b00110000;
return (key & pattern) == pattern;
}
///////////////////////////////////////////////////////////
#INT_RB
void RB_handler() {
int8 i, digit;
char key;
key = kbd_getc();
if (digit_key_pressed(key)) {
digit = char_to_digit(key);
for (i = 0; i < 200; i++) // repeat the display for human eyes
display(digit);
}
}
///////////////////////////////////////////////////////////
/**
* Echo digit-key presses (0 to 9) of a 4x3 keypad to the 7-segment LED
*
* Configuration:
* Use PORTB for keypad by uncommenting the following line in PICC\Drivers\KBDD.c
* #define use_portb_kbd TRUE
*
* Wiring: (TM Board)
* (1) PIC's B1-B7 to Matrix Keypad's R3-R0&C2-C0 (notice the reverse order)
* (2) PIC's C0-C6 to 7-segment LED's A-G
* PIC's D1 to 7-segment LED's C2
*/
void main() {
enable_interrupts(GLOBAL);
enable_interrupts(INT_RB);
kbd_init();
while (true) {
// do nothing
}
}
|