标题: 单片机独立按键的基本操作 [打印本页]

作者: zq0109    时间: 2021-3-12 11:31
标题: 单片机独立按键的基本操作



单片机源程序如下:
#include <STC89C5xRC.H>
#include <intrins.h>

sbit s7 = P3^0;
sbit s6 = P3^1;
sbit s5 = P3^2;
sbit s4 = P3^3;

unsigned char code SMG_NoDot[18] =
    {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
     0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,
     0xbf,0x7f};
        
void InitHC573(unsigned char i);
void show(unsigned char j);
void Delay100ms();
void scanfkey();

void main(){
    scanfkey();
}

void Delay100ms()       //@11.0592MHz
{
    unsigned char i, j;

    i = 180;
    j = 73;
    do
    {
        while (--j);
    } while (--i);
}

void InitHC573(unsigned char i){
    switch (i){
        case 6:
                P2 = ( P2 & 0x1f ) | 0xc0;
        break;
        case 7:
            P2 = ( P2 & 0x1f ) | 0xe0;
        break;
    }
}

void show(unsigned char j){
    InitHC573(6);
    P0 = 0x01;
    InitHC573(7);
    P0 = j;
}

void scanfkey(){
    if(s7 == 0){
        Delay100ms();
        if(s7 == 0){
            while(s7 == 0);
            show(SMG_NoDot[0]);
        }
    }
    if(s6 == 0){
        Delay100ms();
        if(s6 == 0){
            while(s6 == 0);
            show(SMG_NoDot[1]);
        }
    }
    if(s5 == 0){
        Delay100ms();
        if(s5 == 0){
            while(s5 == 0);
            show(SMG_NoDot[2]);
        }
    }
    if(s4 == 0){
        Delay100ms();
        if(s4 == 0){
            while(s4 == 0);
            show(SMG_NoDot[3]);
        }
    }
}

作者: hhh402    时间: 2021-3-12 16:06
这种程序只适合学习按键的原理,用了  Delay100ms();while(s4 == 0);程序基本就废了,不使用这两个语句还能够把程序编写出来才算入门。




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1