51 single chip microcomputer displays time and date

Publisher:jiaohe1Latest update time:2018-08-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include

#include

#include<DS1302.h>

 

sbit p10=P1^0;

 

bit flag=0; //Adjust control flag

bit flagh=1; //Time and date switch mark

uchar temp[7]; //Time before adjustment

uchar curtime1[8],curtime2[8]; //Adjusted time display mode

uchar tab1[]={30,40,11,31,8,2,14}; //DS1302 initialization time

uchar xingqi; //Read the day of the week

uchar code tab2[]={0xff,0xf9,0xA4,0xb0,0x99,0x92,0x82,0xf8}; //Common cathode LED to display the day of the week

 

 uchar tab4[11]={0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b,0x01}; //LED digital tube display code for displaying time and date

 

 

  void delay2(void)

{

int i,j;

for(i=0; i<=10; i++);

for(j=0; j<=2; j++);

}

void  main(void)

   {   

    fly i,temp0[8];

    P3=0xff;

Init_7219(); //Initialize MAX7219

set_rtc(tab1); //Initialize DS1302

  //  init_DS1302();

    temp0[2]=10; //Display the "-" symbol at a fixed position

temp0[5]=10;

 while(1)

 {  

     get_1302(); //Read the time and save it in temp

if(p10==0) //Judge whether the button is released or pressed

 delay2();

     if( p10==0 )

       {

   flagh=0;  

}

   if((flagh==1)) //Time and date display switch flag

   {

    

    temp0[0]=temp[0]/16;

    temp0[1]=temp[0]%16;

    temp0[3]=temp[1]/16;

    temp0[4]=temp[1]%16;

    temp0[6]=temp[2]/16;

    temp0[7]=temp[2]%16;

    

   }

   else

   { flagh=1; //Normal display of time, press the p10 key to display the date

temp0[0]=temp[3]/16;

     temp0[1]=temp[3]%16;

     temp0[3]=temp[4]/16;

     temp0[4]=temp[4]%16;

     temp0[6]=temp[6]/16;

     temp0[7]=temp[6]%16;    

   }

   

  for(i=0;i<8;i++)

  {  

   Write_7219(i+1,tab4[temp0[i]]); 

  }

 

  }

 

 }

 


#include

 #include

 

 

/*-------------------------------------------------

This function is to write data to MAX7219 (operation word or display data)

address is the address of the internal register;

dat is the operation word or displayed data

-------------------------------------------------*/

void Write_7219(uchar address,uchar dat)

{ fly i;

ACC=address;

    LOAD=0; //Pull down the chip select line to select the device

    //sending address

    for (i=0;i<8;i++) // shift loop 8 times              

    {   

      CLK=0; //Clear clock bus

        DIN=A7; //Each time, take the highest bit and send it to max7219  

    CLK=1; //Clock rising edge, send address   

        address<<=1; //Shift left one bit      

    }

//send data

ACC=that;

    for (i=0;i<8;i++)               

    {   

        CLK=0;

        DIN=A7;  

        CLK=1; //Clock rising edge, send data

  dat<<=1; 

    }

    LOAD=1; //Sending is completed, rising edge latches data                       

}

/*-------------------------------------------------

This is the initialization program of MAX7219

DECODE_MODE 0x09 //Decoding control register

INTENSITY 0x0A //Brightness control register

SCAN_LIMIT 0x0B //Scan limit register

SHUT_DOWN 0x0C //Shutdown mode register

DISPLAY_TEST 0x0F //Test control register

-------------------------------------------------*/

void Init_7219(void)                 

    Write_7219(SHUT_DOWN,0x01); //Start normal working mode (0xX1)

    Write_7219(DISPLAY_TEST,0x00); //Select working mode (0xX0)

    Write_7219(DECODE_MODE,0x00); //Select non-decoding mode

    Write_7219(SCAN_LIMIT,0x07); //Use all 8 LEDs

    Write_7219(INTENSITY,0x04); //Set the initial brightness      

}

 /*

void mul_write_7219( uchar *point) //write multiple data

{

 flying i;

 

 for (i=0;i<8;i++)

 {

  Write_7219(i+1,*point);

  point++;   

  }

  

  }

*/


void set_rtc(uchar tab[])

{ uchar ucaddr=0x80; //Write the initial address of the initial time to the time register

flying a,i;

write_1302(0x8e,0x00); //, write protection is removed during initialization

for(i=0;i<7;i++)

{ a=tab[i]/10; //Convert decimal number to BCD code

 tab[i]=tab[i]%10;

 tab[i]=tab[i]+a*16;

 write_1302(ucaddr,tab[i]);

 ucaddr+=2;

}

// write_1302(0x8e,0x80); //Add write protection

}

/*-------------------------------------------------

This function reads out the data in the 1302 time register in multiple bytes and puts them into the curtime array.

{0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};   

The initial address to read data from the time register

  Put the read data into the array curtime

-------------------------------------------------*/

void get_1302() //Read multiple bytes of data from the 1302 time register and put them into the curtime array

{ uchar ucaddr=0x81; //The initial address to read data from the time register

fly i=0;

//for(i=0;i<7;i++)

while(i<7)

{

temp[i]=read_1302(ucaddr);

ucaddr+=2;

i++;

}

}

void init_DS1302(void) // Initialize separately, no loop statement

{

write_1302(0x80,0x30);  

write_1302(0x82,0x25);  

write_1302(0x84,0x01);

write_1302(0x86,0x25); 

write_1302(0x88,0x02); 

write_1302(0x8A,0x04); 

write_1302(0x8c,0x14);

write_1302(0x8e,0x0); //Remove write protection

}


Reference address:51 single chip microcomputer displays time and date

Previous article:51 MCU timestamp related functions
Next article:51 MCU Function Module Study Notes - Timer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号