问个c51的程序 是按键扫描的
程序代码:
#include #define N 5 sbit SEG_En = P1^0; sbit DIG_En = P1^1; sbit LED_En = P1^2; sbit Key_En = P1^3; unsigned char Key_Value; /***********************************************/ /*延时函数。 */ /***********************************************/ void delay_ms(unsigned int TData) { unsigned int i,j; for(j=0; j<TDATA; J++) for(i=0; i<120; i++); } /***********************************************/ /*按键扫描函数 */ /***********************************************/ void key_scan() { unsigned char TD; P0 = 0x00; SEG_En = 1; DIG_En = 1; SEG_En = 0; DIG_En = 0; P0 = 0xFE; //扫描第一行 DIG_En = 1; DIG_En = 0; Key_En = 0; TD = P0; switch(TD) { case 0xFE : Key_Value = 0x00; break; case 0xFD : Key_Value = 0x01; break; case 0xFB : Key_Value = 0x02; break; case 0xF7 : Key_Value = 0x03; break; default : break; } Key_En = 1; P0 = 0xFD; //扫描第二行 DIG_En = 1; DIG_En = 0; Key_En = 0; TD = P0; switch(TD) { case 0xFE : Key_Value = 0x04; break; case 0xFD : Key_Value = 0x05; break; case 0xFB : Key_Value = 0x06; break; case 0xF7 : Key_Value = 0x07; break; default : break; } Key_En = 1; P0 = 0xFB; //扫描第三行 DIG_En = 1; DIG_En = 0; Key_En = 0; TD = P0; switch(TD) { case 0xFE : Key_Value = 0x08; break; case 0xFD : Key_Value = 0x09; break; case 0xFB : Key_Value = 0x0A; break; case 0xF7 : Key_Value = 0x0B; break; default : break; } Key_En = 1; P0 = 0xF7; //扫描第四行 DIG_En = 1; DIG_En = 0; Key_En = 0; TD = P0; switch(TD) { case 0xFE : Key_Value = 0x0C; break; case 0xFD : Key_Value = 0x0D; break; case 0xFB : Key_Value = 0x0E; break; case 0xF7 : Key_Value = 0x0F; break; default : break; } Key_En = 1; } void main() { unsigned char temp,i,j; unsigned char a[5]; SEG_En = 0; DIG_En = 0; Key_En = 1; LED_En = 1; Key_Value = 0xFF; temp = 0xFF; i = 0x00; j = N-1; while(1) { key_scan(); if(i == N) { P0 = a[j] << 4; LED_En = 0; delay_ms(500); LED_En = 1; delay_ms(500); j++; if(j == N) j = 0x00; } else { if(Key_Value != 0xFF) { P0 = Key_Value << 4; LED_En = 0; delay_ms(20); LED_En = 1; if(temp != Key_Value) { temp = Key_Value; a[i] = Key_Value; i++; } } } } } 这里面while(1) { key_scan(); if(i == N) { P0 = a[j] << 4; LED_En = 0; delay_ms(500); LED_En = 1; delay_ms(500); j++; if(j == N) j = 0x00; } else { if(Key_Value != 0xFF) { P0 = Key_Value << 4; LED_En = 0; delay_ms(20); LED_En = 1; if(temp != Key_Value) { temp = Key_Value; a[i] = Key_Value; i++; } } } } }尤其的不理解是什么意思啊 谁能帮我解释下