51单片机时钟电路用4个按键切换显示年时间日期的4个共阳极7段码数码管上显示 C语言编程

发布时间:2019-08-27 19:38:51

s1按下显示年

s2按下显示时间

s3按下显示日期


推荐回答

#include<reg52.h>

sbit S1 = P3^0;  //引脚声明sbit S2 = P3^1;sbit S3 = P3^2;

unsigned char code ledchar[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};//共阳极 0 - 9unsigned char Ledbuf[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};//显示缓冲区

unsigned char D1[4];unsigned char D2[4];unsigned char D3[4];

unsigned char hour = 9;unsigned char min = 40;unsigned int sec = 0;

unsigned int year = 2016;unsigned char mon = 6;unsigned char date = 16;

unsigned char index = 0;unsigned int  tt = 0;

unsigned char num = 0;

void Timer0Init(void)  //1毫秒@12.000MHz{ TMOD &= 0xF0;  //设置定时器模式 TMOD |= 0x01;  //设置定时器模式 TL0 = 0x18;  //设置定时初值 TH0 = 0xFC;  //设置定时初值 ET0 = 1;   TR0 = 1;  //定时器0开始计时}void main(){ Timer0Init(); EA = 1;  while(1) {  D1[0] = ledchar[hour/10];   //时分  D1[1] = ledchar[hour%10];  D1[2] = ledchar[min/10];  D1[3] = ledchar[min%10];

  D2[0] = ledchar[year/1000];  //年  D2[1] = ledchar[year/100%10];  D2[2] = ledchar[year/10%10];  D2[3] = ledchar[year%10];

  D3[0] = ledchar[mon/10];   //月 日  D3[1] = ledchar[mon%10];  D3[2] = ledchar[date/10];  D3[3] = ledchar[date%10];

  switch(index)  {   case 0: Ledbuf[0] = D1[3];     Ledbuf[1] = D1[2];     Ledbuf[2] = D1[1];     Ledbuf[3] = D1[0];     break;   case 1: Ledbuf[0] = D2[3];     Ledbuf[1] = D2[2];     Ledbuf[2] = D2[1];     Ledbuf[3] = D2[0];     break;   case 2: Ledbuf[0] = D3[3];     Ledbuf[1] = D3[2];     Ledbuf[2] = D3[1];     Ledbuf[3] = D3[0];     break;   default : break;  }    }   }void S1Scan(){ static unsigned char t = 0; static bit Lock = 0;

 if(S1 == 1) {   t = 0;   Lock = 0; } else if(Lock == 0) {  t++;  if(t >= 10)  {   index = 0;   Lock = 1;  } }}void S2Scan(){ static unsigned char t = 0; static bit Lock = 0;

 if(S2 == 1) {   t = 0;   Lock = 0; } else if(Lock == 0) {  t++;  if(t >= 10)  {   index = 1;   Lock = 1;  } }}void S3Scan(){ static unsigned char t = 0; static bit Lock = 0;

 if(S3 == 1) {   t = 0;   Lock = 0; } else if(Lock == 0) {  t++;  if(t >= 10)  {   index = 2;      Lock = 1;  } }}void KeyScan(){ S3Scan(); S2Scan(); S1Scan(); }void LedScan(){ static unsigned char i = 0;

 P0 = 0xFF;

 if(index == 0) {  if(tt < 500)  {   Ledbuf[2] &= 0x7F;  }  else  {   Ledbuf[2] |= 0x80;  } } else if(index == 2) {  Ledbuf[2] &= 0x7F;  } switch(i) {  case 0: P2 = 0x10; P0 = Ledbuf[3]; i++; break;  case 1: P2 = 0x20; P0 = Ledbuf[2]; i++; break;  case 2: P2 = 0x40; P0 = Ledbuf[1]; i++; break;  case 3: P2 = 0x80; P0 = Ledbuf[0]; i=0; break;  default : break; }}void interruptTimer0() interrupt 1{ 

 TL0 = 0x18;  //设置定时初值 TH0 = 0xFC;  //设置定时重载值

 KeyScan(); LedScan();

 if(mon < 8) {  if((mon%2) != 0)    //1,2,5,7   31天    {   num = 32;  }  else  {   if(mon != 2)    //4,6月   30天   {    num = 31;   }   else   {    if((year%4) == 0) //闰年  2月 29天    {     num = 30;      }    else    //不是闰年  2月 28天    {     num = 29;    }   }  } } else {  if((mon%2) == 0)    //8,10,12 月  31天  {   num = 32;  }  else        //9,11月  30 天  {   num = 31;  } } tt++;  if(tt >= 1000) {  tt = 0;  sec++;  if(sec >= 60)  {   sec = 0;   min++;   if(min >= 60)   {    min = 0;    hour++;    if(hour >= 24)    {     hour = 0;     date++;     if(date >= num)     {      date = 0;      mon++;      if(mon >= 13)      {       mon = 0;       year++;      }     }    }   }  } }}

以上问题属网友观点,不代表本站立场,仅供参考!