2.独立按键

Posted on 2022-11-26 22:41  站地看蓝天哟  阅读(53)  评论(0)    收藏  举报

1.按K1亮一个灯,松即灭

#include<regx52.h>
#include<INTRINS.H>


main()
{
    while(1){
        if(P3_1==0){
            P2_0=0;
        }
        else{
            P2_0=1;
        }
    }
}

2.按键消抖-自保持

#include<regx52.h>
#include<INTRINS.H>

void Delay1ms(unsigned int xms)        //@12.000MHz
{
    unsigned char i, j;
    while(xms--){
        i = 2;
        j = 239;
        do
        {
            while (--j);
        } while (--i);
    }
}


void main()
{
    while(1){
        if(P3_1==0){
            Delay1ms(20);//消抖
            while(P3_1==0);
            Delay1ms(20);//xiao dou
            P2_0=~P2_0;状态自保持
        }

    }
}

3.二进制加法

#include<regx52.h>
#include<INTRINS.H>

void Delay1ms(unsigned int xms)        //@12.000MHz
{
    unsigned char i, j;
    while(xms--){
        i = 2;
        j = 239;
        do
        {
            while (--j);
        } while (--i);
    }
}


void main()
{
    while(1){
        if(P3_1==0){
            Delay1ms(20);//xiao dou
            while(P3_1==0);
            Delay1ms(20);//xiao dou
            P2--;
        }
        

    }
}

4.双键左右移动.

#include<regx52.h>
#include<INTRINS.H>

void Delay1ms(unsigned int xms)        //@12.000MHz
{
    unsigned char i, j;
    while(xms--){
        i = 2;
        j = 239;
        do
        {
            while (--j);
        } while (--i);
    }
}


void main()
{
    unsigned char LEDnum;
    P2=~0x01;
    while(1){
        if(P3_1==0){
            Delay1ms(20);//xiao dou
            while(P3_1==0);
            Delay1ms(20);//xiao dou
            LEDnum++;
            if(LEDnum==8)    LEDnum=0;
            P2=~(0x01<<LEDnum);
        }
        if(P3_0==0){
            Delay1ms(20);//xiao dou
            while(P3_0==0);
            Delay1ms(20);//xiao dou
            if(LEDnum==0)    LEDnum=8;
            LEDnum--;
            P2=~(0x01<<LEDnum);
        }
    }
}