四位數碼管,初始值顯示00.00前面兩位爲秒,自動記時

這個是不帶定時器的,需要帶定時器的請看上一個文章

https://blog.csdn.net/title71/article/details/101613668

#include<reg52.h>
unsigned char code SEGMENT_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

sbit BIT_P10=P1^0;
sbit BIT_P11=P1^1;
sbit BIT_P12=P1^2;
sbit BIT_P13=P1^3;
sbit LED=P2^0;
unsigned char micro_second=0;
unsigned int second=0;
unsigned int ccc=0;

 void delay_ms(unsigned int x)
 {
 	unsigned int i=0;
 	unsigned int j=0;

 	for(i=0;i<x;i++)
	 {
  		 for(j=0;j<100;j++)
   		{
   
   		}
	 }
 
 }
  void display()
{
   unsigned char w1=0;
   unsigned char w2=0;
   unsigned char w3=0;
   unsigned char w4=0;

	w4=(micro_second/10);
	w3=micro_second%10;
	w1=(second%100)/10;
	w2=(second%10);

	P0=SEGMENT_CODE[w1];
	BIT_P10=0;
	BIT_P11=1;
	BIT_P12=1;
	BIT_P13=1;
	delay_ms(5);
	BIT_P10=1;

	P0=SEGMENT_CODE[w2]|0x80;
	BIT_P10=1;
	BIT_P11=0;
	BIT_P12=1;
	BIT_P13=1;
	delay_ms(5);
	BIT_P11=1;
	
	P0=SEGMENT_CODE[w3];
	BIT_P10=1;
	BIT_P11=1;
	BIT_P12=0;
	BIT_P13=1;
	delay_ms(5);
	BIT_P12=1;

	P0=SEGMENT_CODE[w4];
	BIT_P10=1;
	BIT_P11=1;
	BIT_P12=1;
	BIT_P13=0;
	delay_ms(5);
	BIT_P13=1;
}

  void main()
 {
 	TMOD=0X01;
	TH0=(65536-10000)/256;
	TL0=(65536-10000)%256;
	EA=1;
	ET0=1;
	TR0=1;
	TMOD|=0X01;
       	TH1=(65536-50000)/256;
	TL1=(65536-50000)%256;
	EA=1;
	ET0=1;
	TR1=1;
	  while(1)
	  {
	  display();  
	  }
 }


 void TIMER() interrupt 1
 {
       	TH0=(65536-10000)/256;
	TL0=(65536-10000)%256;
       	micro_second++;
	   if(micro_second>99)
	   {
	       micro_second=0;
		   second++; 
	   }
}

void TIMER1()  interrupt 3
{
  	TH1=(65536-10000)/256;
	 TL1=(65536-10000)%256;
	  ccc++;   
	  if(ccc>=2)
	  {
	  ccc=0;
	  LED=~LED;  
	  }
}

有問題可以聯繫我們:QQ羣:712573569 羣名:書の庫(booku.cc)

主要偏向全棧,都是大學生,一起努力吧!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章