| |
| |
| |
| |
| #include<REG51.H> |
| #define Main_Fosc 12000000L |
| #define T1MS (65536-Main_Fosc/12/1000) |
| #define Key_Debounce 40 |
| sbit LED=P2^0; |
| sbit Key=P3^1; |
| sbit Key2=P3^0; |
| 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; |
| |
| if ((0!=Key)||(0!=Key2)) |
| { |
| Key_CNT=0; |
| Key_Lock=0; |
| } |
| else if (0==Key_Lock) |
| { |
| Key_CNT++; |
| if (Key_CNT>Key_Debounce) |
| { |
| Key_Lock=1; |
| Key_Handle=1; |
| |
| } |
| |
| } |
| |
| } |
| |
| void Key_Service() |
| { |
| if (0==Key_Handle) |
| { |
| return; |
| } |
| |
| switch (Key_Handle) |
| { |
| case 1: |
| LED=!LED; |
| Key_Handle=0; |
| break; |
| } |
| |
| } |
| |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步