//-------------------------------------------------------------------------- //来源: http://hi.baidu.com/mculove/home //程序名称: 用74HC165读8个按键状态 //晶振: 11.0592MHz; //MCU型号: STC89C52RC; //电路板型号: EBV-MV(51实验板); //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- //硬件连接: //EBVn-[VCC] -> MVn-[--VCC--] //EBVn-[GND] -> MVn-[//GND//] //MVn-{EX3}-[Y0] -> MVn-{EX2}-[L1] //MVn-{EX3}-[Y1] -> MVn-{EX2}-[L2] //MVn-{EX3}-[Y2] -> MVn-{EX2}-[L3] //MVn-{EX3}-[Y3] -> MVn-{EX2}-[L4] //MVn-{EX3}-[Y4] -> MVn-{EX2}-[L5] //MVn-{EX3}-[Y5] -> MVn-{EX2}-[L6] //MVn-{EX3}-[Y6] -> MVn-{EX2}-[L7] //MVn-{EX3}-[Y7] -> MVn-{EX2}-[L8] // -> MVn-{EX2}-(VCC) // -> MVn-{EX3}-(VCC) //EBVn-[P10] -> MVn-{EX3}-[A] //EBVn-[P11] -> MVn-{EX3}-[B] //EBVn-[P12] -> MVn-{EX3}-[C] //EBVn-[P13] -> MVn-{EX3}-[G1] //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- //实验结果: //控制74HC138的[A,B,C,G1]端口,使[Y0,Y1..Y7]输出预期的值,LED实现了流水灯显示 //-------------------------------------------------------------------------- //------------------------------------------------------ //头文件; #include <AT89X52.H> #define HC165_nPL P3_5 #define HC165_CK P3_6 #define HC165_OUT P3_7 //------------------------------------------------------ void delay(void) { unsigned char i,j; for(i = 250;i > 0;i--) for(j = 200;j > 0;j--); } unsigned char HC165(void) { unsigned char i; unsigned char Temp; HC165_CK = 1; HC165_nPL = 0; //HC165读按键 HC165_nPL = 1; //暂停HC165读按键 Temp = 0; if(HC165_OUT == 1) Temp |= 0x01; for(i = 0;i < 7;i++) { HC165_CK = 0; HC165_CK = 1; Temp <<= 1; if(HC165_OUT == 1) { Temp |= 0x01; } } HC165_CK = 0; return(Temp); } //------------------------------------------------------ //程序入口; void main(void) { while(1) { P1 = HC165(); delay(); } }
相关下载:KEIL原工程