C program for microcontroller to control ds1302 clock

Publisher:JoyfulSunflowerLatest update time:2012-12-05 Source: 51hei Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/**************************************************/
/* ds1302 real-time clock C program*/
/*51hei.com member classic works have been tested successfully/
/**************http://www.51hei.com/ziliao/file/ds1302c.rar Click this link to download the code of this example****/

#include < reg52.h>
#include

#define uchar unsigned char

sbit T_CLK = P3^4; /*Real-time clock clock line pin*/
sbit T_IO = P3^3; /*Real-time clock data line pin*/
sbit T_RST = P3^2; /*Real-time clock reset line pin*/
//sbit OE=P3^6;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;

uchar time[8]="  :  :  ";
uchar min,hou,day,mon,yea;

void Init1302(void);
void v_W1302(uchar ucAddr, uchar ucDa);
uchar uc_R1302(uchar ucAddr)
;
void v_BurstW1302T(uchar *pSecDa); void v_BurstR1302T(uchar *pSecDa);
void v_BurstW1302R(uchar *pReDa);
void v_BurstR130 2R( uchar *pReDa);
void v_Set1302(uchar *pSecDa);
void v_Get1302(uchar ucCurtime[]);
void donetime(void);
void bcd_int(uchar i);
void bcdtoInt(void);
void SetTime(void);
/** ****************************************
*
* Name: v_RTInputByte
* Description:
* Function: To DS1302 writes 1Byte data
* Call:
* Input: Data written by ucDa
* Return value: None
**************************************** *****************/
void v_WTInputByte(uchar ucDa)
{
uchar i;
//OE=0;
ACC= ucDa;
for(i=8; i>0; i- -)
{
T_IO = ACC0; //* Equivalent to RRC in assembly
T_CLK = 1;
T_CLK = 0;
ACC =ACC>> 1;
}
// OE=1;
}
/********** *****************************************
*
* Name: uchar uc_RTOutputByte
* Description:
* Function: Read 1Byte data from DS1302
* Call:
* Input:
* Return value: ACC
****************************** **********************/
uchar uc_RTOutputByte(void)
{
uchar i;
//OE=0;
for(i=8; i>0; i- -)
{
ACC = ACC>>1; //* Equivalent to RRC in assembly
ACC7 = T_IO;
T_CLK = 1;
T_CLK = 0;
}
//OE=1;
return(ACC);
}
/******** ***********************************************
*
* Name: v_W1302
* Description : Write address first, then write command/data
* Function: Write data to DS1302
* Call: v_RTInputByte()
* Input: ucAddr: DS1302 address, ucDa: data to be written
* Return value: None
******** ************************************************/
void v_W1302(uchar ucAddr, uchar ucDa)
{
//OE=0;
T_RST = 0;
T_CLK = 0;
T_RST = 1;
v_WTInputByte(ucAddr); /* Address, command */
v_WTInputByte(ucDa); /* Write 1Byte data*/
T_CLK = 1;
T_RST = 0;
//OE=1;
} [page]
/**************************** **************************
*
* Name: uc_R1302
* Description: Write address first, then read command/data
* Function: Read a certain address of DS1302 Address data
* Call: v_RTInputByte() , uc_RTOutputByte()
* Input: ucAddr: DS1302 address
* Return value: ucDa : the read data
******************************************************/
uchar uc_R1302(uchar ucAddr)
{
uchar ucDa;
//OE=0;
T_RST = 0;
T_CLK = 0;
T_RST = 1;
v_WTInputByte(ucAddr); /* address, command*/
ucDa = uc_RTOutputByte(); /* read 1Byte data*/
T_CLK = 1;
T_RST =0;
// OE=1;
return(ucDa);
}
/***********************************************
*
* Name: v_BurstW1302T
* Description: Write address first, then write data (clock multi-byte mode)
* Function: Write clock data to DS1302 (multi-byte mode)
* Call: v_RTInputByte()
* Input: pSecDa: Clock data address format: Seconds, minutes, hours, days, months, weeks, years control
* 8Byte (BCD code) 1B 1B 1B 1B 1B 1B 1B 1B
* Return value: None
**********************************************************/
/*void v_BurstW1302T(uchar *pSecDa)
{
uchar i;
v_W1302(0x8e,0x00); //* Control command, WP=0, write operation?
T_RST = 0;
T_CLK = 0;
T_RST = 1;
v_WTInputByte(0xbe); //* 0xbe: Clock multi-byte write command
for (i=8;i>0;i--) //*8Byte = 7Byte clock data + 1Byte control
{
v_WTInputByte(*pSecDa);//* Write 1Byte data
pSecDa++;
}
T_CLK = 1;
T_RST =0;
} */
/*******************************************
*
* Name: v_BurstR1302T
* Description: Write address first, then read command/data (clock multi-byte mode)
* Function: Read DS1302 clock data
* Call: v_RTInputByte() , uc_RTOutputByte()
* Input: pSecDa: Clock data address format: Seconds, hours, minutes, days, months, weeks, years
* 7Byte (BCD code) 1B 1B 1B 1B 1B 1B 1B
* Return value: ucDa: read data
*************************************************/
/*void v_BurstR1302T(uchar *pSecDa)
{
uchar i;
T_RST = 0;
T_CLK = 0;
T_RST = 1;
v_WTInputByte(0xbf); //* 0xbf: clock multi-byte read command
for (i=8; i>0; i--)
{
*pSecDa = uc_RTOutputByte(); //* read 1Byte data
pSecDa++;
}
T_CLK = 1;
T_RST =0;
} */
/**********************************************
*
* Name: v_BurstW1302R
* Description: write address first, then write data (register multi-byte mode)
* Function: write data to DS1302 register (multi-byte mode)
* Call: v_RTInputByte()
* Input: pReDa: register data address
* Return value: None
**********************************************/
/*void v_BurstW1302R(uchar *pReDa)
{
uchar i;
v_W1302(0x8e,0x00); //* Control command, WP=0, write operation?
T_RST = 0;
T_CLK = 0;
T_RST = 1;
v_WTInputByte(0xfe); //* 0xbe: Clock multi-byte write command
for (i=31;i>0;i--) //*31Byte register data
{
v_WTInputByte(*pReDa); //* Write 1Byte data
pReDa++;
}
T_CLK = 1;
T_RST =0;
} */ [page]
/****************************************************
*
* Name: uc_BurstR1302R
* Description: Write address first, then read command/data (register multi-byte mode)
* Function: Read DS1302 register data
* Call: v_RTInputByte() , uc_RTOutputByte()
* Input: pReDa: Register data address
* Return value: None
************************************************/
/*void v_BurstR1302R(uchar *pReDa)
{
uchar i;
T_RST = 0;
T_CLK = 0;
T_RST = 1;
v_WTInputByte(0xff); //* 0xbf: clock multi-byte read command
for (i=31; i>0; i--) //*31Byte register data
{
*pReDa = uc_RTOutputByte(); //* Read 1Byte data
pReDa++;
}
T_CLK = 1;
T_RST =0;
} */
/****************************************************
*
* Name: v_Set1302
* Description:
* Function: Set the initial time
* Call: v_W1302()
* Input: pSecDa: initial time address. The initial time format is: Seconds, hours, minutes, days, months, weeks, and years
* 7Byte (BCD code) 1B 1B 1B 1B 1B 1B 1B
* Return value: None
****************************************************/
/*void v_Set1302(uchar *pSecDa)
{
uchar i;
uchar ucAddr = 0x80;
v_W1302(0x8e,0x00); // Control command, WP=0, write operation?
for(i =7;i>0;i--)
{
v_W1302(ucAddr,*pSecDa); //Seconds, hours, minutes, days, months, weeks, and years
pSecDa++;
ucAddr +=2;
}
v_W1302(0x8e,0x80); // Control command, WP=1, write protection?
} */
/**************************************************
*
* Name: v_Get1302
* Description:
* Function: Read the current time of DS1302
* Call: uc_R1302()
* Input: ucCurtime: Save the current time address. The current time format is: Seconds, Hours, Days, Months, Weeks, Years
* 7Byte (BCD code) 1B 1B 1B 1B 1B 1B 1B
* Return value: None
****************************************************/
/*void v_Get1302(uchar ucCurtime[])
{
uchar i;
uchar ucAddr = 0x81;
for (i=0;i<7;i++)
{
ucCurtime[i] = uc_R1302(ucAddr);//*The format is: Seconds, Hours, Days, Months, Weeks, Years
ucAddr += 2;
}
} */
/**********************************************************
* Name: Init1302
* Description:
* Function: Initialize DS1302
* Call:
* Input:
* Return value: None
**********************************************************/
void Init1302(void)
{
v_W1302(0x8e,0x00); //Control write WP=0
v_W1302(0x90,0xa5);
v_W1302(0x80,0x00);
//Secondsv_W1302(0x82,0x59) ; //Minutesv_W1302(
0x84,0x10); //Hoursv_W1302
(0x86,0x01); //
Dayv_W1302(0x88,0x08); //Monthv_W1302
(0x8a,0x03); //Week
v_W1302(0x8c,0x07); //Year */
v_W1302(0x8e,0x80);
}
/******************************************************
* Name: donetime
* Description:
* Function: Time processing
* Call:
* Input:
* Return value: None
**************************************************************/
void donetime(void)
{
uchar d;
d=uc_R1302(0x81);
time[6]=d/16+48;
time[7]=d%16+48;
d=uc_R1302(0x83);
time[3]=d/16+48;
time[4]=d%16+48;
d=uc_R1302(0x85);
time[0]=d/16+48;
time[1]=d%16+48;
//bcd_int(d);
}

//
/*void bcd_int(uchar i)
{
uchar j;
time[0]=(i&0x0f)+48;
j=i>>4;
time[1]=(j&0x0f)+48;
}  */
//
/*void bcdtoInt(void)
{
uchar i;
for(i=0;i<7;i++)
bcd_int(ucCurtm[i]);
}  */
//
void SetTime(void)
{
v_W1302(0x8e,0x00);
v_W1302(0x80,0x80);
v_W1302(0x82,min);
v_W1302(0x84,hou);
v_W1302(0x86,day);
v_W1302(0x88,mon);
v_W1302(0x8c,yea);
v_W1302(0x80,0x00);
v_W1302(0x8e,0x80);
}

Keywords:MCU Reference address:C program for microcontroller to control ds1302 clock

Previous article:Single chip microcomputer drives 16*16 dot matrix LED Chinese character display C51 program
Next article:Single chip one-button multi-function key recognition technology

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

Output SPWM program based on avr microcontroller I/O port
Based on the ATmega16 microcontroller output SPWM program, the PWM frequency is 25KHZ, the output sine wave is 50HZ, the output voltage waveform is smooth, the distortion is small, only hardware and software need to be combined. The circuit is simple, and the function can be adjusted by modifying the program, with low
[Microcontroller]
Temperature alarm design based on 51 microcontroller
Abstract: Single-chip microcomputer technology has been popularized in our lives, work, scientific research, and various fields, and has become a relatively mature technology. This article will introduce a digital thermometer based on single-chip microcomputer control. This thermometer is a multi-functional thermomete
[Microcontroller]
Temperature alarm design based on 51 microcontroller
Bit addressing programming based on 80C51 single chip microcomputer
The 80C51 microcontroller has a bit processing function and can operate on data bits, so there is a corresponding bit addressing mode. The so-called bit addressing is to directly set a bit in the internal RAM or the bit-addressable special function register SFR to 1 or reset it to 0. The scope of bit addressing, tha
[Microcontroller]
Bit addressing programming based on 80C51 single chip microcomputer
Solution to noise when MCU's I2S is slave
g_ak4961_i2s_tx_handle.Instance- SR __IO uint32_t SR; /*! SPI status register,Address offset: 0x08 */ 27.5.3 SPI status register (SPI_SR) CRCERR 8: FRE bit 8 FRE: Frame format error Note: When SPI works in TI slave mode or I2S slave mode, use this flag #define __HAL_I2S_DISABLE(__HANDLE__)  ((__HANDLE__)- Instance-
[Microcontroller]
Design of DS1302 Backup Power Supply
DS1302 is a circuit with trickle current charging capability from DALLAS. Its main features are serial data transmission, programmable charging function for power-off protection power supply, and the charging function can be turned off. It uses a common 32.768kHz crystal oscillator. It can time the year, month, day,
[Microcontroller]
Design of DS1302 Backup Power Supply
Digital thermometer design based on 51 microcontroller design
1. Project introduction A digital thermometer is an electronic measuring instrument widely used in daily life and industry. It is used to detect ambient temperature and convert it into a digital signal for display. With the development of modern technology, digital thermometers have gradually replaced traditional merc
[Microcontroller]
Digital thermometer design based on 51 microcontroller design
Considerations when specifying a Wi-Fi®-enabled MCU
Alex Li, Senior Marketing Engineer, Wireless Solutions Products Division, Microchip Technology The trend in the Industrial Internet of Things is to perform more functions in one SoC rather than multiple discrete devices to streamline the bill of materials, reduce design risk, and reduce space. A typical example is a
[Internet of Things]
Considerations when specifying a Wi-Fi®-enabled MCU
Learning experience of single chip microcomputer beginners
The single-chip microcomputer is a very practical subject. Without practice, everything is just a mirage. It is very necessary to spend some money to buy a development board. ("The development board is like the seeds that farmers plant. As long as they can be used reasonably, they will take root and sprout, and finally
[Microcontroller]
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号