Detailed explanation of ds1302 clock program for avr microcontroller

Publisher:csZhouLatest update time:2019-11-28 Source: 51heiKeywords:ds1302 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

ds1302 is a clock chip and has nothing to do with avr. Here, mega32 is used to operate the chip. There is a lot of information about this chip on the Internet. Let me briefly talk about it. The communication method is SPI mode. There are several registers inside the chip to store year, month, day, hour, minute, and second, and there is write protection, etc.


*****************************************************************
//CPU:mega32
//Compiler:iar
//DS1302
//Call LCD display function in main function


#include
#include "lcd.h"
void Write1302 ( unsigned char addr,unsigned char dat );
unsigned char Read1302 ( unsigned char addr );
void ds1302_init(void);


//Register macro definitions
#define SECOND 0x80
#define MINUTE 0x82
#define HOUR 0x84
#define DAY 0x86
#define MONTH 0x88
#define YEAR 0x8C
#define PROTECT 0x8E

//Pin definition
#define CE PORTD_Bit5 //Read and write enable
#define SCLK PORTD_Bit6 //Clock
#define TIO PORTD_Bit7 //Data output
#define RIO PIND_Bit7 //Data input
#define CE_D DDRD_Bit5 //CE direction
#define SCLK_D DDRD_Bit6 //SCLK direction
#define IO_D DDRD_Bit7 //IO direction

void main(void)
{
  ds1302_init();
  lcd_init();
  while(1)
  {
    delay(10000);
    clear();
    display_string(num2str(Read1302(YEAR)),0,0);display_string("YEAR",0,16);display_string(num2str(Read1302(MONTH)),0,64);display_string("MONTH",0,80);
    display_string(num2str(Read1302(DAY)),2,0);display_string("DAY",2,16);display_string(num2str(Read1302(HOUR)),2,64);display_string("HOUR",2,80);
    display_string(num2str(Read1302(MINUTE)),4,0);display_string("MIN",4,16);display_string(num2str(Read1302(SECOND)),4,64);display_string("SECOND",4,80);
  }
}


//Address, data sending subroutine
void Write1302 ( unsigned char addr,unsigned char data )
{
    unsigned char i;
    IO_D = 1;
    CE = 0; //CE pin is low, data transmission is terminated
    SCLK = 0; //Clear clock bus
    CE = 1; //CE pin is high, logic control is valid
    //Send address
    for ( i=8; i>0; i-- ) //Loop 8 times shift
    {
        SCLK = 0;
        TIO = (addr&0x01); //Transmit low byte each time
        addr >>= 1; //Shift right one bit
        SCLK = 1;
    }
    //Send data
    for ( i=8; i>0; i-- )
    {
        SCLK = 0;
        TIO = (data&0x01);
        data >>= 1;
        SCLK = 1;
    }
    
    CE = 0;

}
//Data reading subroutine
unsigned char Read1302 ( unsigned char addr )
{
    unsigned char i,data=0,temp;
    addr = addr | 1;//Change to read address
    IO_D = 1;
    CE=0;
    SCLK=0;
    CE = 1;
    //Send address
    for ( i=8; i>0; i-- ) //Loop 8 times shift
    {
        SCLK = 0;
        TIO = (addr&0x01); //Transmit low byte each time
        addr >>= 1; //Shift right one bit
        SCLK = 1;
    }
    IO_D = 0;
    //Read data
    for ( i=0; i<8; i++ )
    {
        SCLK = 1;
        SCLK = 0;
        data |= RIO<    }
    
    CE=0;
    
  // data = data/16*10 + data&0x0f ; Why not
    temp = data>>4;
    data &= 0x0f;
    data = temp*10 + data;
    
    return (data);
}
//Initialize DS1302
void ds1302_init(void)
{
    CE_D = 1 ; //CE output
    SCLK_D = 1 ; //SCLK output
    Write1302 (PROTECT,0X00); //Disable write protection
    Write1302 (SECOND ,0x30); //Initialize seconds
    Write1302 (MINUTE ,0x05); //Initialize minutes
    Write1302 (HOUR ,0x6); //Initialize hours
    Write1302 (DAY ,0x14); //Initialize days
    Write1302 (MONTH ,0x12); //Initialize months
    Write1302 (YEAR ,0x88); //Initialize years
    Write1302 (PROTECT,0x80); //Enable write protection
}


******************************************************************

Keywords:ds1302 Reference address:Detailed explanation of ds1302 clock program for avr microcontroller

Previous article:Detailed explanation of USART program of avr microcontroller
Next article:Using AVR to drive MAXIN's DS18B20

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号