/********************************************************
程序名:main.C
版 本:Ver1.0
芯 片:AT89C51或STC89C51
晶 体:片外12MHz
编 程: Joey
日 期:2023-11-13
描 述:通过 74HC165 对 16 按键输入进行扩展
C编译器:Keil C51
注意事项:
重要事项:********************************************************/
#include "REG51.h"#define u8 unsigned char
#define u16 unsigned int/***************************************************************************/
sbit DIN=P1^0;
sbit CLK=P1^2;
sbit SH =P1^1;/***************************************************************************/
void Delay(u16 x)
{while(x--);
}
u16 Read_74HC165D(void)
{u16 i,ReadKey=0;DIN=1;CLK=1;/**** load the data for D0-D7 at onece**/SH =0;SH =1;//read form first level to the second level//first level: the directly output levelfor(i=0;i<16;i++){ReadKey<<=1; //the first level data will be moved to the fist 8 bitCLK=1;if(DIN) //read the data output from the first levelReadKey++;Delay(10);CLK=0;}return ReadKey;
}
/***************************************************************************/
void main(void)
{static u16 temp,templast;while(1){temp=Read_74HC165D();if(temp != templast){if(temp != 0xFFFF){P2=temp&0xFF; //second level to the P2P3=(temp>>8)&0xFF; //first level to the P3}templast=temp&0xFFFF;}else{P2=temp&0xFF;P3=(temp>>8)&0xFF;}Delay(100000);}
}
/***************************************************************************/