74HC595输出接按键,怎么读取键码
原理图上边Q1-Q7接了四个按键 595输出,还要一个输入IO来配合,就可以读了。
仿真没有什么问题,实际运用要考虑按键消抖问题。
- #include <reg51.H>
- #define uint unsigned int
- #define uchar unsigned char
- sbit P_HC595_SER = P2^0;
- sbit P_HC595_SRCLK = P2^1;
- sbit P_HC595_RCLK = P2^2;
- sbit KEY=P2^3;
- unsigned char code table[]={0x70,0x79,0xee};//KEY.
- unsigned char code display[]={//0~f-.
- 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40,0x80};
- unsigned char code wela[]={
- 0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- /**************** 向HC595发送一个字节函数 ******************/
- void Send_595(uchar dat)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- dat <<= 1;
- P_HC595_SER = CY; //溢出位赋值数据输出端
- P_HC595_SRCLK = 1; //移位时钟
- P_HC595_SRCLK = 0;
- }
- }
- /**************** 向HC595发送2个字节驱动码 ******************/
- void DisplayScan()
- {
- static uchar i,j;
- Send_595(wela[i]);//向HC595发送位码
- /**********根据位码识别按键********/
- if(!KEY)
- {
- switch(i)
- {
- case 0: j= 4; break;
- case 1: j= 1; break;
- case 2: j= 2; break;
- case 3: j= 3; break;
- }
- }
- /******************************/
- if(i==3)
- Send_595(display[j]);//向HC595发送段码显示键值
- else
- Send_595(table[i]); //向HC595发送段码显示KEY.
- P_HC595_RCLK = 1; //锁存输出数据
- P_HC595_RCLK = 0;
- i++;
- i%=4;
- }
- void main()
- {
- P_HC595_SRCLK = 0;//初始化移位时钟端
- P_HC595_RCLK = 0;//初始化锁存输出端
- while(1)
- {
- DisplayScan();
- }
- }
复制代码
|