标签:
1 #include ? <reg52.h> 2 #define uchar unsigned char 3 #define uint unsigned int 4 //Led数码管数 5 sbit DIO = P2^0;//串行数据输入 6 sbit LRCLK = P2^1;//时钟脉冲信号——上升沿有效 7 sbit LSCLK = P2^2;//打入信号————上升沿有效 8 unsigned char code LED_0F[] = 9 {// 0 1 2 34 5 6 78 9 A bC d E F - 10 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x8C,0xBF,0xC6,0xA1,0x86,0xFF,0xbf 11 }; 12 uchar scan_keyboard(void); // 扫描键盘 13 void delay_ms(int n); //毫秒级延时函数 14 void get_LED(uchar key); //获得LED对应的数组值 15 void LED_ON(uchar x); //点亮led数码管 16 void main() 17 { 18 while(1){ 19 uchar key = scan_keyboard(); 20 get_LED(key); 21 } 22 } 23 void delay_ms(int n) 24 { 25 int i = 0, j = 0; 26 for(i = 0; i < n; i++) 27 for(j = 0; j < 100; j++); 28 } 29 void LED_ON(uchar lcode) 30 { 31 uint i; 32 for(i = 8;i >= 1; i--) 33 { 34 if (lcode & 0x80) 35 DIO = 1; 36 else 37 DIO = 0; 38 lcode <<= 1; 39 LSCLK = 0; 40 LSCLK = 1; 41 } 42 } 43 void LED_code(uchar value) 44 { 45 uchar code *led_table; // 查表指针 46 uchar lcode; //要得到的显示码 47 //显示第1位 48 led_table = LED_0F + value; 49 lcode = *led_table; 50 LED_ON(lcode); 51 LED_ON(0x01); 52 LRCLK = 0; 53 LRCLK = 1; 54 //显示第2位 55 } 56 void get_LED(uchar key) 57 { 58 if(key == 0x00) 59 return; //如果为0x00,说明没有扫描到值,返回 60 switch(key) 61 { 62 case 0xee: LED_code(0);break;//0按下相应的键显示相对应的码值 63 case 0xde: LED_code(1);break;//1 按下相应的键显示相对应的码值 64 case 0xbe: LED_code(2);break;//2 65 case 0x7e: LED_code(3);break;//3 66 case 0xed: LED_code(4);break;//4 67 case 0xdd: LED_code(5);break;//5 68 case 0xbd: LED_code(6);break;//6 69 case 0x7d: LED_code(7);break;//7 70 case 0xeb: LED_code(8);break;//8 71 case 0xdb: LED_code(9);break;//9 72 case 0xbb: LED_code(10);break;//a 73 case 0x7b: LED_code(11);break;//b 74 case 0xe7: LED_code(12);break;//c 75 case 0xd7: LED_code(13);break;//d 76 case 0xb7: LED_code(14);break;//e 77 case 0x77: LED_code(15);break;//f 78 } 79 } 80 uchar scan_keyboard() 81 { 82 uchar row_wire, col_wire; //定义行线和列线,行线0-3,列线4-7 83 P1 = 0xf0; //行线输出全为0,列线输出全为1 84 col_wire = P1 & 0xf0; //读入列线值 85 if(col_wire != 0xf0) 86 { 87 delay_ms(1); //去抖延时 88 if(col_wire != 0xf0) 89 { 90 col_wire = P1 & 0xf0; //读入列线值 91 P1 = col_wire | 0x0f; //输出当前列线值 92 row_wire = P1 & 0x0f; //读入行线值 93 return(col_wire + row_wire);//键盘最后组合码值 94 } 95 } 96 return 0x00; 97 }
标签:
原文地址:http://www.cnblogs.com/runhang/p/4885608.html