| |
| |
| |
| |
| #include<REG51.H> |
| #define Main_Fosc 12000000L |
| #define T1MS (65536-Main_Fosc/12/1000) |
| #define Key_Debounce 40 |
| sbit Key_Line1_Output=P1^7; |
| sbit Key_Line2_Output=P1^6; |
| sbit Key_Line3_Output=P1^5; |
| sbit Key_Line4_Output=P1^4; |
| sbit Key_Row1_Input=P1^3; |
| sbit Key_Row2_Input=P1^2; |
| sbit Key_Row3_Input=P1^1; |
| sbit Key_Row4_Input=P1^0; |
| sbit LED1=P2^0; |
| sbit LED2=P2^1; |
| sbit LED3=P2^2; |
| sbit LED4=P2^3; |
| sbit LED5=P2^4; |
| sbit LED6=P2^5; |
| sbit LED7=P2^6; |
| sbit LED8=P2^7; |
| unsigned char Key_Handle=0; |
| void Sys_Init(); |
| void Delay_Long(); |
| void Perpherial_Init(); |
| void Key_Scan(); |
| void Key_Service(); |
| |
| void main() |
| { |
| Sys_Init(); |
| Delay_Long(); |
| Perpherial_Init(); |
| while (1) |
| { |
| Key_Service(); |
| } |
| |
| } |
| |
| void Sys_Init() |
| { |
| TMOD=0X01; |
| TL0=T1MS; |
| TH0=T1MS>>8; |
| } |
| |
| void Delay_Long() |
| { |
| unsigned char i,j; |
| for(i=0;i++;i<220) |
| { |
| for(j=0;j<220;j++) |
| ; |
| } |
| } |
| |
| void Perpherial_Init() |
| { |
| ET0=1; |
| TR0=1; |
| EA=1; |
| } |
| |
| void Timer0_ISR() interrupt 1 |
| { |
| TL0=T1MS; |
| TH0=T1MS>>8; |
| Key_Scan(); |
| } |
| |
| void Key_Scan() |
| { |
| static unsigned int Key_CNT; |
| static unsigned char Key_Lock=0; |
| static unsigned char KeyScan_Step; |
| static unsigned char KeyOutput_LineNum=1; |
| switch (KeyScan_Step) |
| { |
| case 0: |
| if (1==KeyOutput_LineNum) |
| { |
| Key_Line1_Output=0; |
| Key_Line2_Output=1; |
| Key_Line3_Output=1; |
| Key_Line4_Output=1; |
| } |
| else if (2==KeyOutput_LineNum) |
| { |
| Key_Line1_Output=1; |
| Key_Line2_Output=0; |
| Key_Line3_Output=1; |
| Key_Line4_Output=1; |
| } |
| else if (3==KeyOutput_LineNum) |
| { |
| Key_Line1_Output=1; |
| Key_Line2_Output=1; |
| Key_Line3_Output=0; |
| Key_Line4_Output=1; |
| } |
| else if (4==KeyOutput_LineNum) |
| { |
| Key_Line1_Output=1; |
| Key_Line2_Output=1; |
| Key_Line3_Output=1; |
| Key_Line4_Output=0; |
| } |
| Key_CNT=0; |
| KeyScan_Step++; |
| break; |
| case 1: |
| Key_CNT++; |
| if (Key_CNT>=2) |
| { |
| Key_CNT=0; |
| KeyScan_Step++; |
| } |
| break; |
| case 2: |
| if ((1==Key_Row1_Input)&&(1==Key_Row2_Input)&&(1==Key_Row3_Input)&&(1==Key_Row4_Input)) |
| { |
| Key_CNT=0; |
| Key_Lock=0; |
| KeyScan_Step=0; |
| KeyOutput_LineNum++; |
| if(KeyOutput_LineNum>=5) |
| { |
| KeyOutput_LineNum=1; |
| } |
| } |
| else if (0==Key_Lock) |
| { |
| if ((0==Key_Row1_Input)&&(1==Key_Row2_Input)&&(1==Key_Row3_Input)&&(1==Key_Row4_Input)) |
| { |
| Key_CNT++; |
| if (Key_CNT>Key_Debounce) |
| { |
| Key_Lock=1; |
| Key_CNT=0; |
| if (1==KeyOutput_LineNum) |
| { |
| Key_Handle=1; |
| } |
| else if (2==KeyOutput_LineNum) |
| { |
| Key_Handle=5; |
| } |
| else if (3==KeyOutput_LineNum) |
| { |
| Key_Handle=9; |
| } |
| else if(4==KeyOutput_LineNum) |
| { |
| Key_Handle=13; |
| } |
| } |
| |
| } |
| else if ((1==Key_Row1_Input)&&(0==Key_Row2_Input)&&(1==Key_Row3_Input)&&(1==Key_Row4_Input)) |
| { |
| Key_CNT++; |
| if (Key_CNT>Key_Debounce) |
| { |
| Key_Lock=1; |
| Key_CNT=0; |
| if (1==KeyOutput_LineNum) |
| { |
| Key_Handle=2; |
| } |
| else if (2==KeyOutput_LineNum) |
| { |
| Key_Handle=6; |
| } |
| else if (3==KeyOutput_LineNum) |
| { |
| Key_Handle=10; |
| } |
| else if (4==KeyOutput_LineNum) |
| { |
| Key_Handle=14; |
| } |
| } |
| } |
| else if ((1==Key_Row1_Input)&&(1==Key_Row2_Input)&&(0==Key_Row3_Input)&&(1==Key_Row4_Input)) |
| { |
| Key_CNT++; |
| if (Key_CNT>Key_Debounce) |
| { |
| Key_Lock=1; |
| Key_CNT=0; |
| if (1==KeyOutput_LineNum) |
| { |
| Key_Handle=3; |
| } |
| else if (2==KeyOutput_LineNum) |
| { |
| Key_Handle=7; |
| } |
| else if (3==KeyOutput_LineNum) |
| { |
| Key_Handle=11; |
| } |
| else if (4==KeyOutput_LineNum) |
| { |
| Key_Handle=15; |
| } |
| } |
| } |
| else if ((1==Key_Row1_Input)&&(1==Key_Row2_Input)&&(1==Key_Row3_Input)&&(0==Key_Row4_Input)) |
| { |
| Key_CNT++; |
| if (Key_CNT>Key_Debounce) |
| { |
| Key_Lock=1; |
| Key_CNT=0; |
| if (1==KeyOutput_LineNum) |
| { |
| Key_Handle=4; |
| } |
| else if (2==KeyOutput_LineNum) |
| { |
| Key_Handle=8; |
| } |
| else if (3==KeyOutput_LineNum) |
| { |
| Key_Handle=12; |
| } |
| else if (4==KeyOutput_LineNum) |
| { |
| Key_Handle=16; |
| } |
| } |
| } |
| } |
| |
| break; |
| } |
| |
| } |
| |
| void Key_Service() |
| { |
| if (0==Key_Handle) |
| { |
| return; |
| } |
| |
| switch (Key_Handle) |
| { |
| case 1: |
| LED1=0; |
| Key_Handle=0; |
| break; |
| case 2: |
| LED2=0; |
| Key_Handle=0; |
| break; |
| case 3: |
| LED3=0; |
| Key_Handle=0; |
| break; |
| case 4: |
| LED4=0; |
| Key_Handle=0; |
| break; |
| case 5: |
| LED5=0; |
| Key_Handle=0; |
| break; |
| case 6: |
| LED6=0; |
| Key_Handle=0; |
| break; |
| case 7: |
| LED7=0; |
| Key_Handle=0; |
| break; |
| case 8: |
| LED8=0; |
| Key_Handle=0; |
| break; |
| case 9: |
| LED1=1; |
| Key_Handle=0; |
| break; |
| case 10: |
| LED2=1; |
| Key_Handle=0; |
| break; |
| case 11: |
| LED3=1; |
| Key_Handle=0; |
| break; |
| case 12: |
| LED4=1; |
| Key_Handle=0; |
| break; |
| case 13: |
| LED5=1; |
| Key_Handle=0; |
| break; |
| case 14: |
| LED6=1; |
| Key_Handle=0; |
| break; |
| case 15: |
| LED7=1; |
| Key_Handle=0; |
| break; |
| case 16: |
| LED8=1; |
| Key_Handle=0; |
| break; |
| } |
| |
| } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验