本帖最后由 soma 于 2024-6-10 17:41 编辑
使用开天斧实现一个按键实现多种控制效果
- #include <STC8H.H>
- #define MAIN_Fosc 24000000UL
- #define uint unsigned int
- #define uchar unsigned char
- uchar status = 0;
- sbit key = P3^5; //按键
- void delayms(uint ms)
- {
- uint i;
- do{
- i = MAIN_Fosc / 6000;
- while(--i);
- }while(--ms);
- }
- void main(void)
- {
- P2M0=0x00;P2M1=0x00;
- P3M0=0x00;P3M1=0x00;
- while(1)
- {
- if(!key)
- {
- delayms(10); //软件防抖
- if(!key)
- {
- status++;
- if(status==1)
- {
- P2=0xfe;//亮一灯
- }
- else if(status==2)
- {
- P2=0xf8;//亮3灯
- }
- else if(status==3)
- {
- P2=0xc0;//亮6灯
- }
- else if(status==4)
- {
- status=0;//关灯
- P2=0xff;
- }
- while(!key); //等待按键释放
- }
- }
- }
- }
复制代码
|