51 microcontroller learning: DS1302 clock experiment

Publisher:知识的海洋Latest update time:2023-02-03 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Experiment name: DS1302 clock experiment
wiring instructions:
Experimental phenomenon: After downloading the program, the electronic clock hours, minutes and seconds are displayed on the digital tube in the format of "XX-XX-XX"
Notes:
************* *************************************************** ************************/
#include "public.h"
#include "smg.h"
#include "ds1302.h"


/****************************************************** ******************************
* Function name: main
* Function function: main function
* Input: None
* Output: None
* *************************************************** ******************************/
void main()
{
u8 time_buf[8];

ds1302_init();//Initialize DS1302

while(1)
{
ds1302_read_time();
time_buf[0]=gsmg_code[gDS1302_TIME[2]/16];
time_buf[1]=gsmg_code[gDS1302_TIME[2]&0x0f];
time_buf[2]=0x40;
time_buf[3]= time_buf [
7 ] ​=gsmg_code[gDS1302_TIME[0]&0x0f]; smg_display(time_buf,1); } }






#include "ds1302.h"
#include "intrins.h"

//---DS1302 address commands for writing and reading hours, minutes and seconds---//
//---seconds, minutes, hours, days, months, and anniversary lowest read and write bits;-------//
u8 gREAD_RTC_ADDR[ 7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
u8 gWRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};

//---DS1302 clock initialized Thursday, May 20, 2021 at 13:51:47. ---//
//---The storage sequence is seconds, minutes, days, months and anniversaries, and the storage format is BCD code---//
u8 gDS1302_TIME[7] = {0x47, 0x51, 0x13, 0x20, 0x04, 0x05, 0x21};


/****************************************************** ******************************
* Function name: ds1302_write_byte
* Function function: DS1302 writes single byte
* Input: addr: address /command
dat:data
* output: None
****************************************** ******************************************/
void ds1302_write_byte(u8 addr,u8 dat)
{
u8 i=0;

DS1302_RST=0;
_nop_();
DS1302_CLK=0;//CLK low level
_nop_();
DS1302_RST=1;//RST changes from low to high
_nop_();

for(i=0;i<8;i++)//Loop 8 times, write 1 bit each time, write the low bit first and then the high bit
{
DS1302_IO=addr&0x01;
addr>>=1;
DS1302_CLK=1
; _nop_();
DS1302_CLK =0;//CLK generates a rising edge from low to high, thereby writing data_nop_
();
}
for(i=0;i<8;i++)//Loop 8 times, write 1 bit each time, write first Write the low bit then the high bit
{
DS1302_IO=dat&0x01;
dat>>=1;
DS1302_CLK=1;
_nop_();
DS1302_CLK=0;
_nop_();
}
DS1302_RST=0;//RST pull low
_nop_();
}

/****************************************************** ******************************
* Function name: ds1302_read_byte
* Function function: DS1302 reads single byte
* Input: addr: address /Command
* Output: Read data
****************************************** ****************************************/
u8 ds1302_read_byte(u8 addr)
{
u8 i=0 ;
u8 temp=0;
u8 value=0;

DS1302_RST=0;
_nop_();
DS1302_CLK=0;//CLK low level
_nop_();
DS1302_RST=1;//RST changes from low to high
_nop_();

for(i=0;i<8;i++) //Loop 8 times, write 1 bit each time, write the low bit first and then the high bit
{
DS1302_IO=addr&0x01;
addr>>=1;
DS1302_CLK=1;
_nop_();
DS1302_CLK=0; //CLK generates one from low to high Rising edge, thus writing
data_nop_();
}
for(i=0;i<8;i++)//Loop 8 times, read 1 bit each time, read the low bit first and then the high bit
{
temp=DS1302_IO;
value=( temp<<7)|(value>>1);//First shift value to the right by 1 bit, then shift temp to the left by 7 bits, and finally perform the OR operation
DS1302_CLK=1;
_nop_();
DS1302_CLK=0
; _nop_();
}
DS1302_RST=0;//RST pull low_nop_
();
DS1302_CLK=1;//For the real thing, the P3.4 port does not have an external pull-up resistor, so the code needs to be added here to make the data port have a rising edge pulse.
_nop_();
DS1302_IO = 0;
_nop_();
DS1302_IO = 1;
_nop_();
return value;
}

/****************************************************** ****************************
* Function name: ds1302_init
* Function function: DS1302 initialization time
* Input: None
* Output: None
*************************************************** *****************************/
void ds1302_init(void)
{
u8 i=0;
ds1302_write_byte(0x8E,0X00);
for( i=0;i<7;i++)
{
ds1302_write_byte(gWRITE_RTC_ADDR[i],gDS1302_TIME[i]);
}
ds1302_write_byte(0x8E,0X80);
}

/****************************************************** ****************************
* Function name: ds1302_read_time
* Function function: DS1302 read time
* Input: None
* Output: none
************************************************* ******************************/
void ds1302_read_time(void)
{
u8 i=0;
for(i=0;i< 7;i++)
{
gDS1302_TIME[i]=ds1302_read_byte(gREAD_RTC_ADDR[i]);
}
}


#include "smg.h"

//The common cathode digital tube displays the segment code data of 0~F
u8 gsmg_code[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79 ,0x71};

/****************************************************** ****************************
* Function name: smg_display
* Function function: Dynamic digital tube display
* Input: dat: to be displayed Data
pos: starting from the left position, range 1-8
* Output: None
****************************** *************************************************** */
void smg_display(u8 dat[],u8 pos)
{
u8 i=0;
u8 pos_temp=pos-1;

for(i=pos_temp;i<8;i++)
{
switch(i)//bit selection
{
case 0: LSC=1;LSB=1;LSA=1;break;
case 1: LSC=1;LSB=1; LSA=0;break;
case 2: LSC=1;LSB=0;LSA=1;break;
case 3: LSC=1;LSB=0;LSA=0;break;
case 4: LSC=0;LSB=1 ;LSA=1;break;
case 5: LSC=0;LSB=1;LSA=0;break;
case 6: LSC=0;LSB=0;LSA=1;break;
case 7: LSC=0;LSB= 0;LSA=0;break;
}
SMG_A_DP_PORT=dat[i-pos_temp];//Transmit segment selection data
delay_10us(100);//Delay for a period of time, waiting for the display to stabilize
SMG_A_DP_PORT=0x00;//Mute the sound
}
}


#include "public.h"

/****************************************************** ****************************
* Function name: delay_10us
* Function function: Delay function, when ten_us=1, the delay is approximately 10us
* Input: ten_us
* Output: None
**************************************** ***************************************/
void delay_10us(u16 ten_us)
{
while(ten_us --);
}

/****************************************************** ****************************
* Function name: delay_ms
* Function function: ms delay function, when ms=1, approximately Delay 1ms
* Input: ms: ms delay time
* Output: None
********************************** *********************************************/
void delay_ms(u16 ms)
{
u16 i,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}


Reference address:51 microcontroller learning: DS1302 clock experiment

Previous article:51 microcontroller learning: infrared remote control experiment
Next article:51 microcontroller learning: DS18B20 temperature sensor experiment

Recommended ReadingLatest update time:2024-11-16 19:54

Some thoughts on STM32 driving DS1302
I have used 51 to drive DS1302 before, and it output the correct time in a short time. At that time, I thought this chip was simple and simple. But now I use STM32 to do a project, using the same chip, and I think it is not difficult. I just need to copy and paste the program and change the IO settings. But things are
[Microcontroller]
Application of DS1302 clock chip in single chip perpetual calendar
The production of perpetual calendar based on DS1302 clock chip Contains temperature sensor, temperature display Button operation, infrared remote control Perpetual calendar time power failure record 12864 LCD display shows perpetual calendar, etc. ------------------------------------------------- Five y
[Microcontroller]
Application of DS1302 clock chip in single chip perpetual calendar
Timing control system composed of ATMEGA48 and DS1302
Atmel's ATMEGA48 is a high-performance, low-power 8-bit AVR microprocessor that uses an advanced RISC structure. Most instructions are executed in a single clock cycle, so the operation speed is faster. Two 8-bit timers/counters with independent prescaler and comparator functions; one 16-bit timer/counter with presc
[Microcontroller]
Timing control system composed of ATMEGA48 and DS1302
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号