![]() |
![]() |
#3 | |
Đệ tử 5 túi
Tham gia ngày: Jan 2009
Bài gửi: 100
: |
Trích:
// Scan key 2 program ( Improve from Scan Key) // Author : pk // Date : 01/11/2009 /* Description : - User use keypad to put numberic data on LCD - Press "*" to clear screen - Press "#" to finish input process */ /* Functions : - int read_key() : scan key and return which key is pressed - void lcd_put_int(int num) : dispplay interger number on LCD - int input_key() : input the key which is pressed and display on LCD, return a number which was converted if finish, and 0 if not */ #include <16F887.h> #device *=16 #include <math.h> #use delay(clock = 4000000) #include <lcd.c> #use fast_io(c) #use fast_io(a) #use fast_io(b) #define K1 PIN_C0 #define K2 PIN_C1 #define K3 PIN_C2 #define KA PIN_C3 #define KB PIN_C4 #define KC PIN_C5 #define KD PIN_C6 int read_key(); void lcd_put_int(int16 num); int16 input_key(); int convert(int num[]); lcd_disp(char c[],int row); void main(){ int i; int16 num; set_tris_C(0x07); set_tris_A(0x00); set_tris_B(0x00); output_a(0); output_b(0x00); lcd_init(); lcd_putc("STARTING"); for(i = 1; i < 9; i++){ lcd_putc("."); delay_ms(100); } lcd_putc("\f"); num = input_key(); while(!num){ lcd_putc("\f"); num = input_key(); } lcd_putc("\f"); lcd_putc("GIA TRI VUA NHAP : "); lcd_putc("\n"); lcd_put_int(num); } int read_key(){ output_low(KA);output_high(KB);output_high(KC);out put_high(KD); if(input(K1) == 0) return 1; if(input(K2) == 0) return 2; if(input(K3) == 0) return 3; output_high(KA);output_low(KB);output_high(KC);out put_high(KD); if(input(K1) == 0) return 4; if(input(K2) == 0) return 5; if(input(K3) == 0) return 6; output_high(KA);output_high(KB);output_low(KC);out put_high(KD); if(input(K1) == 0) return 7; if(input(K2) == 0) return 8; if(input(K3) == 0) return 9; output_high(KA);output_high(KB);output_high(KC);ou tput_low(KD); if(input(K1) == 0) return 10; if(input(K2) == 0) return 0; if(input(K3) == 0) return 11; return 15; } // display interger number on LCD void lcd_put_int(int16 num){ int16 temp; unsigned char i = 0, c[5]; temp = num; if (temp != 0) { while(temp){ c[i++] = temp%10; temp /= 10; } while(i) lcd_putc(c[--i] + '0'); } else lcd_putc('0'); } int16 input_key(){ int a,b[16],i = 0; int16 c = 0; for(a = 0; a < 20; a++) b[a] = 0; lcd_putc("NHAP GIA TRI DAT : "); lcd_putc("\n"); while(1){ delay_ms(180); a = read_key(); if(a == 10){ return 0; } if((a != 10) && (a != 15) && (a != 11)){ lcd_put_int(a); b[i++] = a; } if(a == 11){ a = i - 1; while(1){ i--; c = c + b[i]*pow(10,a-i); if(i == 0) break; } return c; } } } Em ko dùng hàm atoi() nữa, thay vào đó, em lấy giá trị của các lần nhập, rồi hiển thịi thành 1 số, ví dụ : em nhập 3 lần, tương ứng là các chữ số : 1,2,3, thì em phải chuyển thành 1 số duy nhất là 123, em có dùng hàm pow(), nhưng ko hiểu sao nó lại hâm hâm kiểu gì ấy. Giả sử em nhập là 1,2,3 thì sau khi đổi, nó lại hiện lên là 122, hầu như đều bị giảm 1 đơn vị, nếu số chỉ có 2 chữ số thì ok, ko vấn đề gì, nhưng từ 3 chữ số trở lên là nó lại bị như vậy. Mục đích chương trình này của em chỉ là dùng bàn phím đề giao tiếp với vđk thôi, em đã nghĩ mấy ngày trời mà chưa đâu vào đâu cả. Bác nào có code về cái này cho em xin với nhé ! Em nghĩ đây là vấn đề về thuật toán, nên cũng hơi khó xem. Rất mong các cao thủ xem giúp rồi chỉ bảo cho em với. Em xin cảm ơn rất nhìu ! thay đổi nội dung bởi: pk178, 01-11-2009 lúc 11:45 PM. |
|
![]() |
![]() |
|
|