| |
| |
| |
| |
| #include<REG51.H> |
| #define Main_Fosc 12000000L |
| #define T1MS (65536-Main_Fosc/12/1000) |
| #define Key_Debounce 40 |
| #define Key_DK_Debounce 300 |
| sbit LED=P2^0; |
| sbit LED1=P2^1; |
| sbit Key=P3^1; |
| 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; |
| static unsigned int Key_DK_CNT; |
| static unsigned char Key_Press_CNT; |
| |
| if (0!=Key) |
| { |
| Key_CNT=0; |
| Key_Lock=0; |
| if (Key_Press_CNT>=1) |
| { |
| Key_DK_CNT++; |
| if (Key_DK_CNT>Key_DK_Debounce) |
| { |
| Key_Press_CNT=0; |
| Key_DK_CNT=0; |
| Key_Handle=1; |
| } |
| |
| } |
| } |
| else if (0==Key_Lock) |
| { |
| Key_CNT++; |
| if (Key_CNT>Key_Debounce) |
| { |
| Key_Lock=1; |
| Key_DK_CNT=0; |
| Key_Press_CNT++; |
| if (Key_Press_CNT>=2) |
| { |
| Key_Press_CNT=0; |
| Key_Handle=2; |
| } |
| |
| |
| } |
| |
| } |
| |
| } |
| |
| void Key_Service() |
| { |
| if (0==Key_Handle) |
| { |
| return; |
| } |
| |
| switch (Key_Handle) |
| { |
| case 1: |
| LED=!LED; |
| Key_Handle=0; |
| break; |
| case 2: |
| LED1=!LED1; |
| Key_Handle=0; |
| break; |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)