Thì đoạn chương trình đấy từ bài viết về hiển thị led7 thanh,post lại vậy:
Chương trình hiển thị phím số trên ma trận phím 4x3 ra đèn 7 đoạn (không 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;
}
///////////////////////////////////////////////////////////
void main() {
int8 i, digit;
char key;
kbd_init();
while (true) {
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);
}
}
}
|