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

📄 clock.c

📁 用8051单片机和6个数码管加一片DS1302时钟芯片做成实用闹钟
💻 C
字号:
//xxxxxxxxxxxxxxxxxxxxx<<为调试用>>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include<reg51.h> 
//*************************************************************************变量声明
sbit  acc0=ACC^0;
sbit  acc1=ACC^1;
sbit  acc2=ACC^2;
sbit  acc3=ACC^3;
sbit  acc4=ACC^4;
sbit  acc5=ACC^5;
sbit  acc6=ACC^6;
sbit  acc7=ACC^7;
 sbit  point=P0^6;//
 sbit  send=P1^0;//发送使能
 sbit  keyadd=P1^1;//键加1
 sbit  keysub=P1^2;//键减1
 sbit  keyva=P1^3;//加1
 sbit  keyvs=P1^4;//减1
 sbit  keyfun=P1^5;//功能键
 sbit  dog=P1^6;//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 sbit  led=P1^7;//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 sbit  dsrst=P2^0;
 sbit  dsdata=P2^1;
 sbit  dsclk=P2^2;
 sbit datal=P2^3;//显示数据锁存
 sbit databl=P2^4;//显示数据位锁存
 sbit cs5302=P2^5;
 sbit cs7895=P2^6;
 sbit busy7895=P2^7;
 //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 unsigned  char data  i;//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
unsigned  char  data  timedata=0,addres=0;
 unsigned char  data timesuf[7]={0x035,0x036,0x012,0x01,0x018,0x012,0x006};//BCD code sec,min,h,week,day,month,year
 unsigned char  data tab1[9]={0,3,0,3,9,0,5,0x0d,0x0d};//秒/十秒/分/十分/小时/十小时/日/function
unsigned  char data clock[7];//timeout click
 //unsigned int  data  da5302[]={0x0,0x01};
 
 //**********************************************************************************************
 //ds1302 时钟函数
//*************************************************************************

void read(void);
void write(void);
unsigned char   readdata(unsigned char  addres);//read a word from ds1302
void   writedata(unsigned char  addres,timedata);//write a word to ds1302
void wreteds1302 (void);//write sec min h day mon year week
void  trant_d (void);//时间转为可显示
void  trant_ymd(void);
void  trand_t (void);//get  sec min hour from led
void  trand_ymd(void);//get year month day from led
void  displaytime (void);
void displaycalendar (void);

//**************************************************************
//5302 DA
//***************************************************************************
//void write5302(unsigned int *da5302);

//AD7895 ad convertion
//*******************************************************************
void  delay   (void);//延时
void  display (unsigned char a,i ); // 显示
void  keyscanf(void);//键盘扫描程序
void  senddata();//串口通迅
/**************************************************************************/
//interrupt
//****************************************************************************
void  serialdata() interrupt 4 using 1
{static unsigned char data flagt=0,flagr=0;
   if(TI==1)
    {TI=0;flagt+=1;
	 if(flagt<2){SBUF='a';}
	 if((flagt>=2)&&(flagt<0x0a)){SBUF=tab1[flagt-0x02];}
	 if(flagt>=0x0a){flagt=0;TI=0;TR1=0;EA=0;}
	}
   if(RI==1)
     { RI=0;flagr+=1;
	   ACC=SBUF;
	   if(flagr>=5)
	   TR1=0;EA=0;RI=0;flagr=0;
	 
	 
	 }
	 TI=0;RI=0;
}
//*********************************************************************
//5302  da
/*****************************************************
void write5302(unsigned int da5302)
{
  ACC=da5302;
  write();
}
***********************************************************************************************************/
//    set time ;; ds1302;;
//************************************************************************************************************

void read (void)
{
  acc0=dsdata;
  dsclk=1;dsclk=0;
  acc1=dsdata;
  dsclk=1;dsclk=0;
  acc2=dsdata;
  dsclk=1;dsclk=0;
  acc3=dsdata;
  dsclk=1;dsclk=0;
  acc4=dsdata;
  dsclk=1;dsclk=0;
  acc5=dsdata;
  dsclk=1;dsclk=0;
  acc6=dsdata;
  dsclk=1;dsclk=0;
  acc7=dsdata;
  dsclk=1;dsclk=0;
 }
//***********************************************************************
void write (void)
{
    dsdata=acc0;
  dsclk=1;dsclk=0;
  dsdata=acc1;
  dsclk=1;dsclk=0;
  dsdata=acc2;
  dsclk=1;dsclk=0;
  dsdata=acc3;
  dsclk=1;dsclk=0;
  dsdata=acc4;
  dsclk=1;dsclk=0;
  dsdata=acc5;
  dsclk=1;dsclk=0;
  dsdata=acc6;
  dsclk=1;dsclk=0;
  dsdata=1;
  dsclk=1;dsclk=0;
}
//read ds1302 time calendar
unsigned char  readdata(unsigned char data addres)
{
   unsigned char timedata=0;
  dsclk=0;dsrst=0;dsrst=1;
 ACC=addres;
 write();
  read();
  timedata=ACC;
  dsrst=0;
  return timedata;
}
//**************************************************************
void   writedata(unsigned char data  addres,timedata)//write ds1302 time calendar
{
  dsclk=0;dsrst=0;dsrst=1;
  ACC=addres;
  write();
  ACC=timedata;
  write();
  dsrst=0;

}
//*******************************************************************
void writeds1302 (void) //ini ds1302 time calendar

                     {  
								   
                        addres=0x8e;timedata=0x00;		  
                        writedata(addres,timedata);//write enable
				             dog=1;//dogggggggggggggggggggggggg
						addres=0x90;timedata=0x0a5;		   
                        writedata(addres,timedata);//trickle  charge
					
		                    dog=0;//dogggggggggggggggggggggggg
                        addres=0x80;
                        timedata=timesuf[0]; 
                        writedata(addres,timedata);//write sec
                                  
                              dog=1;//doggggggggggggggggggggggggg
                        addres=0x082;
                        timedata=timesuf[1];
                        writedata(addres,timedata);//write min
                               
                               dog=0;//doggggggggggggggggggggggggggg
                        addres=0x084;
                        timedata=timesuf[2];
                        writedata(addres,timedata);//write h
                               dog=1;//dogggggggggggggggggggggggg
                         addres=0x08a;                                
                       timedata=timesuf[3];
                        writedata(addres,timedata);//write week
                               dog=0;//dogggggggggggggggggggggggggggggg
                        addres=0x86;
                        timedata=timesuf[4];
                        writedata(addres,timedata);//write day
                                  
                               dog=1;//dogggggggggggggggggggggggggggggg
                        addres=0x088;
                        timedata=timesuf[5];
                        writedata(addres,timedata);//write month
                               dog=0;//dogggggggggggggggggggggggggggggggg
                      
                        addres=0x08c;
                        timedata=timesuf[6];
                        writedata(addres,timedata);//write year 

                              dog=1;//doggggggggggggggggggggggggggggggggggg
					    addres=0x08e;timedata=0x080;
                        writedata(addres,timedata);    //disenable write
                               dog=0;//dogggggggggggggggggggggggggggggg
                   }
//************************************************************************
void  trant_d(void)//时间转为可显示
{
  tab1[0]=(timesuf[0]&0x0f);            
  tab1[1]=((timesuf[0]>>0x04)&0x0f);
  tab1[2]=(timesuf[1]&0x0f);     
  tab1[3]=((timesuf[1]>>0x04)&0x0f); 
  tab1[4]=(timesuf[2]&0x0f);     
  tab1[5]=((timesuf[2]>>0x04)&0x0f);
  tab1[6]=timesuf[3];
  
}
//************************************************************************
void  trant_ymd(void)//时间转为可显示
{
  tab1[0]=(timesuf[4]&0x0f);            
  tab1[1]=((timesuf[4]>>0x04)&0x0f);
  tab1[2]=(timesuf[5]&0x0f);     
  tab1[3]=((timesuf[5]>>0x04)&0x0f); 
  tab1[4]=(timesuf[6]&0x0f);     
  tab1[5]=((timesuf[6]>>0x04)&0x0f);
  tab1[6]=timesuf[3];
  
}
//********************************************************************
void  trand_t (void)//write sec min hour day
{timesuf[0]=(((tab1[1]&0x0f)<<0x04)|(tab1[0]&0x0f));
 timesuf[1]=(((tab1[3]&0x0f)<<0x04)|(tab1[2]&0x0f));
 timesuf[2]=(((tab1[5]&0x0f)<<0x04)|(tab1[4]&0x0f));
 timesuf[3]=(tab1[6]&0x0f);

}
//*****************************************
void  trand_ymd(void)//write date month year
{
 timesuf[3]=(tab1[6]&0x0f);
 timesuf[4]=(((tab1[1]&0x0f)<<0x04)|(tab1[0]&0x0f));
 timesuf[5]=(((tab1[3]&0x0f)<<0x04)|(tab1[2]&0x0f));
 timesuf[6]=(((tab1[5]&0x0f)<<0x04)|(tab1[4]&0x0f));
}
//***************************************************
void  displaytime(void)
	      {            dog=1;//dogggggggggggggggggggggggggggggg
	         addres=0x081;
             timesuf[0]= readdata(addres);//read sec

		     addres=0x083;
             timesuf[1]=readdata(addres);//read  min

		     addres=0x085;
             timesuf[2]=readdata(addres);//read  h
             addres=0x08b;
             timesuf[3]=readdata(addres);  //read  week

	 	  }
//**************************************************
void displaycalendar (void)
          { 
	              dog=1;//doggggggggggggggggggggggggggggg
	         addres=0x087;
             timesuf[4]= readdata(addres);
                  dog=0;//dogggggggggggggggggggggggggggg
		     addres=0x089;
             timesuf[5]=readdata(addres);
                  dog=1;//doggggggggggggggggggggggggggggggg
		     addres=0x08d;
             timesuf[6]=readdata(addres);
			        dog=0;//dogggggggggggggggggggggggggg
             addres=0x08b;
             timesuf[3]=readdata(addres);
                       
	 	    }

//********************************************************************************
//display
//****************************************************************************************显示子程序
 void display(unsigned char a,i)
      { 
	       
	       unsigned  char j=0;
	       unsigned char  code tab[16]={0x3f,0x21,0x9b,0xb3,0xa5,0xb6,0xbe,0x23,0xbf,0xb7,0x0af,0x0bc,0x01e,0x0b9,0x09e,0x08e};//数组0123456789
	                    dog=1;//doggggggggggggggggggggggggggggggggggggggg
		   P0=0;
              j=1;
		      datal=1;
		      datal=0;
           if(i!=0)
		      
                j=j<<i;		       		      
			P0=j;
			 j=0;
			
			databl=1;
			databl=0;

		   P0=tab[a];
           if ((i==2)||(i==4)||(i==6))
               point=1;
			   j=1;
		      datal=1;
		      datal=0;
			delay();
			dog=0;//doggggggggggggggggggggggggggggggggggggggggggg
	  }
/*************************************************************************************************/
//键盘扫描程序直接操作数组lab1[]
//************************************************************************************************
void  keyscanf(void)
  {             
      static unsigned char data c=0;
	                dog=1;//doggggggggggggggggggggggggggggg
     keyadd=1;keysub=1;keyva=1;keyvs=1;keyfun= 1;
	 //keyfun=0;//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
	 if  ((keyadd!=1)||(keysub!=1)||(keyva!=1)||(keyvs!=1)||(keyfun!=1))
	   {          dog=1;//dogggggggggggggggggggggggggg
	      delay();
		          dog=0;//dogggggggggggggggggggggggggg
        if(keyadd!=1)
		   {c+=1;
		     if ((c<0)||(c>=8))
		       c=0;
               display(c,c);
           }		   		   		   		   		   		   		   		   		   		   
		             dog=1;//doggggggggggggggggggggggggg
		if (keysub!=1)
		       { c=c-1;
				  if ((c<0)||(c>=8))
		            c=7;
                    display(c,c);
			   }
			          dog=0;//doggggggggggggggggggggggggggg
	    if (keyva!=1)
  			 {tab1[c]+=1;
				if ((tab1[c]<0)||(tab1[c]>0x0f))
					tab1[c]=0;
                    display(tab1[c],c);
			 }
			           dog=1;//dogggggggggggggggggggggggggggg
	   if (keyvs!=1)
			{   tab1[c]-=1;
			 if ((tab1[c]<0)||(tab1[c]>0x0f))
				tab1[c]=0x0f;
                display(tab1[c],c);
			}
	                     dog=0;//doggggggggggggggggggggggggg        
	   if (keyfun!=1)
			{            dog=1;//doggggggggggggggggggggggggggggggg
			     led=0;
                 tab1[8]+=1; 
                if (tab1[8]>0x0f)
			    	tab1[8]=0; 
			    	display(tab1[8],c);
                if (tab1[7]==0x01)
                    trand_t();//write sec min hour and day into timesuf
                if (tab1[7]==0x02)
                     trand_ymd();//write date month year into tht timesuf
                if (tab1[7]==0x03)
                   writeds1302 ();//write second minute hour day date month and year into the ds1302
                if(tab1[7]==0x04)
     		      *clock=*timesuf;
					      dog=0;//doggggggggggggggggggggggggggggggg
                 
             }

        while( (keyadd!=1)||(keysub!=1)||(keyva!=1)||(keyvs!=1)||(keyfun!=1))
		       {dog=1;dog=0;//dogggggggggggggggggggggggggggggggg
		       }//waiting  key free
      }
	     dog=0;//doggggggggggggggggggggggggggggggggggggggggg
   }
/*****************************************************************************************/
//******************************************************************************************
void senddata ()
{
  TMOD=0x020;
  TH1=0x0fd;
  TL1=0;
  TR1=1;
  PCON=PCON||0x080;
  SCON=0x040;
  RI=0;
  TI=0;
  send=1;
 SBUF='a';
 


}
/*******************************************************************************/
   //延时
void delay (void)
     {
	   unsigned  int data x=0;
	     
        for (x=0;x<20;x++)  
	   ;
	  }
//********************************************************************************

//***********************************************************  

void  main(void)
  {
   // unsigned char  data i=0,a=0;
    P0=0;
	P1=0xc0;
	P2=0;
    P3=0;
    while(1) 
	{  
	              dog=1;dog=0;//dogggggggggggggggggggggggggggggggggggggggggggggggg
       keyscanf();
	   if ((tab1[8]==0x0d)&&(tab1[7]==0x0c))
	      { displaytime();displaycalendar();
		    if (*clock==*timesuf)
			   led=1;
               trant_d();
		  }
	               dog=1;dog=0;//doggggggggggggggggggggggggggggggggggggggggggggggggg
	   if ((tab1[8]==0x0d)&&(tab1[7]==0x0d)) //tab1[8]=0 then display sec min h else and tab1[5]=0 then write ds1302
	      {displaytime();trant_d();}
		            dog=1;dog=0;//ggggggggggggggggggggggggggggggggggggggggggggggg

      if ((tab1[8]==0x0d)&&(tab1[7]==0x0e))
         { displaycalendar ();trant_ymd();}
		             dog=1;dog=0;//doggggggggggggggggggggggggggggggggggggggggggggggg
       if((tab1[8]==0x0f)&&(tab1[7]==0x0f))
            senddata();
			          dog=1;dog=0;//dogggggggggggggggggggggggggggggg

	 for(i=0;i<8;i++)//display sec min and h
	   display(tab1[i],i);
	                  dog=1;dog=0;//gggggggggggggggggggggggggggg
     }
   }
/*****************************************************************/



	    

⌨️ 快捷键说明

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