单片机按键控制数字加减

软件使用:keil,proteus

程序:

#include
#define u8 unsigned char
#define u16  unsigned int
#define    SMG_XS    P2 
#define    SMG_XM    P0
unsigned char code smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共阳极

%使用的数码管为共阳的
unsigned int shuzi;

sbit s1=P1^0;
sbit s2=P1^3;

void delay(u16 i)
{
    while(i–);    
}

void main()
    {
        shuzi=99;
        while(1)
        {
            SMG_XS=smg[shuzi%10]; //取余
            SMG_XM=smg[shuzi/10];
        if(s1==0){          //加
        delay(500);
            if(s1==0){
            shuzi++;
            if(shuzi>99){
            shuzi=0;
            }    
            }
        while(!s1);
        } 
        if(s2==0){    //减
            delay(500);
            if(s2==0){
            shuzi–;
                    
            }
            while(!s2);
        }
        }
        
    }
proteus作图:

 

文章知识点与官方知识档案匹配,可进一步学习相关知识C技能树首页概览113594 人正在系统学习中

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年9月12日
下一篇 2021年9月12日

相关推荐