Demo entry 6780150

c

   

Submitted by wu on Dec 23, 2018 at 11:29
Language: C. Code size: 2.4 kB.

#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char 
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
#define led P1	   //将P0口定义为led 后面就可以使用led代替P1口

sbit key = P3^2;
sbit BEEP = P3^3 ;          //蜂鸣器

uchar  Count1,Count2;
bit  flag,flag2;

static unsigned char second,minute,hour;
unsigned int tcount;
unsigned char m;
uchar code table[]={0xa0,0xbb,0x62,0x2a,0x39,0x2c,0x24,
                        0xba,0x20,0x28};
void delay(uchar i)
{
  uchar j,k; 
  for(j=i;j>0;j--)
    for(k=125;k>0;k--);
}

void display()
{
  
   P0=table[minute/10];
   P2_0 = 0;
   delay(5);
   P2_0 = 1;

   P0=(table[minute%10]& 0xdf);
   P2_1 = 0;
   delay(5);
   P2_1 = 1;

   P0=table[second/10];
   P2_2 = 0;
   delay(5);
   P2_2 = 1;

   P0=table[second%10];
   P2_3 = 0;
   delay(5);
   P2_3 = 1;
   
}

/*演示主程序*/
void main(void)
{

	led=0x00;
	
	Count1=0x00;
    Count2=0x00;
    BEEP=1;
    flag=0;
	flag2=0;

	TMOD=0x12;

    TH0=0x06;
    TL0=0x06;

    TH1=0xfc; 
    TL1=0x66;      //1ms定时

    TR0=1;
    ET0=1;
	ET1=1; 
    EA=1;
   
 while(1)
 {
   display();

  if(flag2==0)
   {
    //TR1=0;
	BEEP=1;
   }  
  if(flag2==1)
   {
    //TR1=1;
	BEEP=0;
   }

	if(key==0)
    {
		delay(5);	  //消抖动
		if(key==0)	  //确认按键被按下
		{
			while(!key);//松手检测
			tcount=0;
			second=0;
			minute=0;
			hour=0;
			led=0x00;
		}		
    }
   	
 }
}


/*
********************************************************************************
** 函数名称 : t0(void) interrupt 1 using 0
** 函数功能 : 定时器中断
********************************************************************************
*/

 void t0(void) interrupt 1 using 0 
  {	tcount++;	  //250微秒

    if(tcount==4000)
	  {tcount=0;
	    second++;

        led=(led<<1);
		if(led==0x00)
		led=0x01;

		if(second==3)
		   flag2=0;

		 if(second==60)
		  {second=0;
		   minute++;
		   flag2=1;
		    if(minute==60)
			 {minute=0;
			  hour++;

			  if(hour==24)
			   {hour=0;
			   }
			 }
		  }
	  }
		
 }

 /*********************************************************
  Time1中断函数
**********************************************************/
void Time1(void) interrupt 3 using 0
{
   TH1=0xfc;               //1ms定时
   TL1=0x66;
  // BEEP=0;
 //  BEEP=~BEEP; 
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).