⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.c

📁 单片机多功能时间程序
💻 C
字号:
#include<reg52.h>

sbit P3_5=P3^3;
sbit a1=P2^0;
sbit b1=P2^1;
sbit c1=P2^2;
sbit d1=P2^3;

char flag1=0,flag2=0,count1=0,count2=0,flag3=0,flag4=0,flag5=0;
int count5=0;
char time=0,a=0,b=0,c=0,d=0,run=0,we=0,we1=0;

void init(void);
void keyscan(void);
void addrun(char);
void led4(void);
void yuwe(void);
void clock(void);

void main()
{
 init();
 while(1)
 {
  if(flag1==1) {keyscan();flag1=0;}
  if(flag2==1) {led4();flag2=0;}
  if(flag5==1&&flag4==1) {clock();flag5=0;}
 }
}

void led4(void)
{ int table[]={0x03,0xf3,0x25,0x0D,0x99,0x49,0xC1,0x1F,0x01,0x19}; 
 switch(we)
 {
  case 0:a1=1;b1=0;c1=0;d1=0;P1=table[a];we=1;break;
  case 1:a1=0;b1=1;c1=0;d1=0;P1=table[b];we=2;break;
  case 2:a1=0;b1=0;c1=1;d1=0;P1=table[c];we=3;break;
  case 3:a1=0;b1=0;c1=0;d1=1;P1=table[d];we=0;break;
 }
}


void init(void)
{
    TMOD=0x01;EA=1;ET0=1;TR0=1;
    TH0=(65536-10000)/256;TL0=(65536-10000)%256;
}

void yuwe(void)
{
 switch(we1)
 {
  case 0:we1=1;break;
  case 1:we1=2;break;
  case 2:we1=3;break;
  case 3:we1=4;count5=0;flag4=1;break;
  case 4:we1=0;flag4=0;break;
 }
}

void keyscan(void)
{

    switch(time)
    {
        case 0:if(P3_5==0)  {time=1;}break;
        case 1:time=2;break;
        case 2:if(P3_5==0)  {time=3;} else time=0; break;
        case 3:if(P3_5==1)  {time=4;}addrun(we1);break;
        case 4:time=5;break;
        case 5:if(P3_5==1)  {time=0;run=0;if(flag3==0)yuwe();else flag3=0;} else time=3; break;
    }
}

void addrun(char s)
{
 run++;
 if(run==20)
 {run=4;flag3=1;
  switch(s)
  {
  case 0:a++;if(a>9) a=0;break;
  case 1:b++;if(b>5) b=0;break;
  case 2:c++;if(c>9) c=0;break;
  case 3:d++;if(d>5) d=0;break;
  }
 }
}

void clock(void)
{
 a++;
 if(a>9)
 {
  a=0;b++;
  if(b>5)
  {
   b=0;c++;
   if(c>9)
   {
    c=0;d++;
    if(d>5)
    {
     d=0;
    }
   }
  }
 }
}


void time0(void) interrupt 1
{
    TH0=(65536-1000)/256;TL0=(65536-1000)%256;
    count1++;count2++;count5++;
    if(count1>=20)   {flag1=1;count1=0;}//keyscan
    if(count2>=5)   {flag2=1;count2=0;}//led4
 if(count5>=1000) {flag5=1;count5=0;}//clock
}

 

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -