访问电脑版页面

导航:老古开发网手机版其他

(转)XD5101实验二----按键计数(led显示)

导读:
关键字:

程序功能:按键计数。每个键对应数码管一位。按键1位数加1,键2十位数加1,键3百位数加1,键4千位数加1。超过量程,清零。

程序源码:

#include<reg52.h>
#define uchar unsigned char

sbit s1 = P2^3;
sbit s2 = P2^0;
sbit s3 = P2^1;
sbit s4 = P2^2;
sbit en = P2^5;
sbit Key1= P3^2;
sbit Key2= P3^3;
sbit Key3= P3^4;
sbit Key4= P3^5;
//各位计数变量
uchar ct1,ct2,ct3,ct4;
//数码管显示
uchar dis[10]={ 0x84,  // 0
          0xBD,  // 1
          0xE0,  // 2
          0xB0,  // 3
          0x99,  // 4
          0x92,  // 5
          0x82,  // 6
          0xBC,  // 7
          0x80,  // 8
          0x90  // 9
    };

void Delay(int m)
{
 while(--m);
}

void display()
{
 s1=0;
 P0=dis[ct1];
 s1=1;
 Delay(100);
 s2=0;
 P0=dis[ct2];
 s2=1;
 Delay(100);
 s3=0;
 P0=dis[ct3];
 s3=1;
 Delay(100);
 s4=0;
 P0=dis[ct4];
 s4=1;
 Delay(100);
}

void main()
{
 s1=1;
 s2=1;
 s3=1;
 s4=1;
 Key1=1;
 Key2=1;
 Key3=1;
 Key4=1;
 en = 0;
 ct1=0;
 ct2=0;
 ct3=0;
 ct4=0;
 while(1)   //循环执行
  {
  if(!Key1)//如果键1按下,下同
  {  
   while(!Key1);
   ++ct1;
  }
  if(!Key2)
  {
   while(!Key2);
   ++ct2;
  }
  if(!Key3)
  {   
   while(!Key3);
   ++ct3;
  }
  if(!Key4)
  {
   while(!Key4);
   ++ct4;
  }
  if(ct1>9)//个位进位,下同
  {
   ct1=0;
   ++ct2;
  }
  if(ct2>9)
  {
   ct2=0;
   ++ct3;
  }
  if(ct3>9)
  {
   ct3=0;
   ++ct4;
  }
  if(ct4>9)
  {
   ct1=0;
   ct2=0;
   ct3=0;
   ct4=0;
  }
     display();     //只须调用显示函数
    }
}

 
来源:   作者:  2006/9/25 16:50:53
栏目: [ ]

相关阅读

安森美推出新的高功率图腾柱PFC控制器,满足具挑战的能效标准

动态功耗低至60μA/MHz!助力设备超长续航,首选国民技术低功耗MCU!