Driver for the real-time clock chip DS1302 based on single-chip microcomputer

Publisher:Joyful222LifeLatest update time:2014-12-31 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This program realizes the rotation display of time and calendar.
 
Since it was debugged on January 5, 2011, the initial time is set to 11:59:58, and the calendar is 11-01-05                   
 
// Title: Experiment with digital tube display clock                                                  
//                     
// Through this routine, you can understand the basic principles and usage of the DS1302 clock chip, understand and master the DS1302 clock chip   
// Write the driver and implement the display of digital characters in the digital tube.                                                            
// Please digest this example carefully and understand the operation of DS1302 in C language                              
 
#include //Include the header file. Generally, no modification is required. The header file contains the definition of special function registers.
#include
 
sbit SCK=P3^6; //clock 
sbit SDA=P3^4; //data 
sbit RST = P3^5; // DS1302 reset
 
sbit LS138A=P2^2; //74LS138 and MCU interface
sbit LS138B=P2^3;
sbit LS138C=P2^4;
 
bit ReadRTC_Flag; //define read DS1302 flag
 
unsigned char l_tmpdate[7]={88,89,17,5,1,3,17}; //seconds, minutes, days, months, and years 11-01-05 Wed 11:59:58
//unsigned char l_tmpdate[7]={88,89,9,5,1,3,17}; //seconds, minutes, days, months, and years 11-01-05 Wed 9:59:58
unsigned char l_tmpdisplay[8]; //Display intermediate variables
 
code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
//Seconds, minutes, hours, days, months, and years lowest digit read and write register 
 
code unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
//Common cathode digital tube 0-9 '-' 'off' table
 
// Function declaration //
            
void Write_Ds1302_byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );
void Read_RTC(void);//read RTC
void Set_RTC(void); //set RTC
void InitTIMER0(void);//inital timer0
 
// Main function //
delay(char m)
{
 int i,j,k;
 for(i=m;i>0;i--)
 for(j=255;j>0;j--)
 for(k=255;k>0;k--);
}
void main(void)   
{
 int i,j;
    InitTIMER0(); //Initialize timer 0
  Set_RTC();            
 //Write the clock value. If you use a backup battery, you don't need to write it every time you power on. This program should be blocked.
 while(1)
 {
   if(ReadRTC_Flag)
   {
     ReadRTC_Flag=0;
   //The hour, minute, second, year, month, and day registers store binary BCD codes. The processing method is as follows:
 //Data conversion, because we use digital tube 0~9 to display, separate the data
    //Read time
  for(i=0;i<2000;i++)
  { l_tmpdisplay[0]=l_tmpdate[2]/16;   
   l_tmpdisplay[1]=l_tmpdate[2]&0x0f;
   l_tmpdisplay[2]=10;          //加入"-"
   l_tmpdisplay[3]=l_tmpdate[1]/16;
   l_tmpdisplay[4]=l_tmpdate[1]&0x0f;
   l_tmpdisplay[5]=10;
   l_tmpdisplay[6]=l_tmpdate[0]/16;
   l_tmpdisplay[7]=l_tmpdate[0]&0x0f;
    Read_RTC();
   }
  for(j=0;j<1000;j++) //Read date
   {
   l_tmpdisplay[0]=l_tmpdate[6]/16;   
   l_tmpdisplay[1]=l_tmpdate[6]&0x0f;
   l_tmpdisplay[2]=10;          //加入"-"
   l_tmpdisplay[3]=l_tmpdate[4]/16;
   l_tmpdisplay[4]=l_tmpdate[4]&0x0f;
   l_tmpdisplay[5]=10;
   l_tmpdisplay[6]=l_tmpdate[3]/16;
   l_tmpdisplay[7]=l_tmpdate[3]&0x0f;
   Read_RTC();
   }
  }
 }
}
 
// Timer 0 initialization //
 
void InitTIMER0(void)
{
 TMOD|=0x01; //Timer setting 16 bits
 TH0=0xef; //initialization value
 TL0=0xf0;
 ET0=1;
 TR0=1;
 EA=1;
}
 
 
 
// Write a byte //
 
void Write_Ds1302_Byte(unsigned  char temp)
{
 unsigned char i;
 for (i=0;i<8;i++) //Loop 8 times to write data
  {
   SCK=0;
     SDA=temp&0x01; //Transmit low byte each time
     temp>>=1; //Move right one bit
     SCK=1;
   }
}  
// Write to DS1302 //
void Write_Ds1302( unsigned char address,unsigned char dat )    
{
  RST=0;
 _nop_();
  SCK=0;
 _nop_();
  RST=1; 
    _nop_(); //Start
  Write_Ds1302_Byte(address); //Send address
  Write_Ds1302_Byte(dat); //Send data
  RST=0; //Resume
}
 
// Read DS1302 data //
 
unsigned char Read_Ds1302 ( unsigned char address )
{
  unsigned char i,temp=0x00;
  RST=0;
 _nop_();
  SCK=0;
 _nop_();
  RST=1;
 _nop_();
  Write_Ds1302_Byte(address);
  for (i=0;i<8;i++) //Loop 8 times to read data
  {  
   if(SDA)
   temp|=0x80; //transmit low byte each time
  SCK=0;
  temp>>=1; //Move right one bit
  _nop_();
   SCK=1;
 }
  RST=0;
 
 return (temp); //Return
}
 
// Read clock data //
 
void Read_RTC(void) //Read calendar
{
 unsigned char i,*p;
 p=read_rtc_address; //address transfer
 for(i=0;i<7;i++) //Read the second, minute, hour, day, month, and year in 7 times
 {
  l_tmpdate[i]=Read_Ds1302(*p);
  p++;
 }
}
// Set clock data //
void Set_RTC(void) //Set the calendar
{
 unsigned char i,*p,tmp;
 
  Write_Ds1302(0x8E,0X00); //Disable write protection
 
  p=write_rtc_address; //Transfer address 
  for(i=0;i<7;i++) //Write the second, minute, hour, day, month, and year for 7 times
  {
    Write_Ds1302(*p,l_tmpdate[i]);
    p++; 
  }
  Write_Ds1302(0x8E,0x80); //Open write protection
}
 
//Timer interrupt function//
 
void tim(void) interrupt 1 using 1
 //Interrupt, used for digital tube scanning
{
    static unsigned char i,num;
    TH0=0xf5;
    TL0=0xe0;
  P0=table[l_tmpdisplay[i]];  
 //Look up the table to get the digital segment of the number to be displayed
    switch(i)      
      {    
   case 0:LS138A=0; LS138B=0; LS138C=0; break;        
         case 1:LS138A=1; LS138B=0; LS138C=0; break;              
         case 2:LS138A=0; LS138B=1; LS138C=0; break;
         case 3:LS138A=1; LS138B=1; LS138C=0; break;
   case 4:LS138A=0; LS138B=0; LS138C=1; break;
   case 5:LS138A=1; LS138B=0; LS138C=1; break;
   case 6:LS138A=0; LS138B=1; LS138C=1; break;
   case 7:LS138A=1; LS138B=1; LS138C=1; break;
   
      }
 i++;
 if(i==8)
   {
    i=0;
    num++;
    if(10==num) //Read the data of 1302 at intervals. The time interval can be adjusted
      {
   ReadRTC_Flag=1; //Use flag bit to judge
   num=0;
   }
   }
 }
Keywords:MCU Reference address:Driver for the real-time clock chip DS1302 based on single-chip microcomputer

Previous article:Detailed explanation of DS18B20 temperature sensor driver based on 51 single chip microcomputer
Next article:74HC138 experimental routine based on 51 single chip microcomputer

Recommended ReadingLatest update time:2024-11-16 20:46

Implementation and programming of 51 single-chip microcomputer serial port multi-machine communication
        1. The master-slave mode of 51 single-chip microcomputer, first set the working mode 3: (master-slave mode + variable baud rate) SCON serial port function register: SM0=1;SM1=1 (working mode 3) Note: Both the master and slave must be in working mode 3. 2. When sending the "address" of the host, set TB8
[Microcontroller]
Implementation and programming of 51 single-chip microcomputer serial port multi-machine communication
Anti-interference design of small heating furnace control system using AVR single chip microcomputer
1. Introduction : The application of AVR microcontrollers in the field of industrial control is different from that in the civil and commercial fields. The environment in which industrial control is located is relatively harsh, with many interference sources. Its common interference sources come from elec
[Microcontroller]
Hidden Errors in Using ACC in the for Loop on MCU
I recently wrote a few programs, one was to use a 51 microcontroller to read the voltage value of an analog-to-digital sensor adc0832, and the other was to read the time value of a ds1302. As a result, the readings were always 0. I debugged for nearly a week and modified a sentence that I thought could not be wrong. Th
[Microcontroller]
Hidden Errors in Using ACC in the for Loop on MCU
MCS-51 MCU Assembly Language Study Notes
The difference between ACC and A The popular explanation is: ACC is the register with address E0H, and A is the standard accumulator without address When it is to be expressed as a bit, ACC must be used, for example, it should be written as ACC.7, not A.7; but when it is used as an 8-bit binary number, both ACC and A
[Microcontroller]
Voice electronic door lock system based on 16-bit microcontroller
Abstract: This article introduces a voice electronic door lock identity authentication system implemented on Sungyang SPCE061A microcontroller using voiceprint recognition technology. The experimental results show that the system performance is stable, the recognition effect is good, and it can be promoted and used. Ke
[Microcontroller]
Unveiling the secrets of automotive electronics technology: car dashboard MCU
    With the rapid development of automotive electronic technology, traditional mechanical instrument panels have shown a trend of transitioning to digital instruments, and all corresponding functional displays will be replaced by rendered high-definition images. The root cause of this huge change is an integrated elec
[Automotive Electronics]
Unveiling the secrets of automotive electronics technology: car dashboard MCU
The importance of variable initialization in multi-machine communication of single-chip microcomputers...
Importance of variable initialization in multi-chip communication The reason why I added the importance of variable initialization to the title of this blog post is that when I was debugging the microcontroller multi-machine communication program, which is the program given below, it took me a whole night to send it
[Microcontroller]
Design of intelligent temperature control system based on AVR microcontroller and temperature sensor
introduction In industrial and agricultural production and daily life, the measurement and control of temperature are becoming more and more important. Traditional temperature control systems use thermistors or thermocouples to measure temperature. However, since the analog temperature sensor outputs an analog signal,
[Microcontroller]
Design of intelligent temperature control system based on AVR microcontroller and temperature sensor
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号