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
//Pin definition void main(void) } ******************************************************************
#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
#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
{
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
}
Previous article:Detailed explanation of USART program of avr microcontroller
Next article:Using AVR to drive MAXIN's DS18B20
- Popular Resources
- Popular amplifiers
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Teach you to learn 51 single chip microcomputer-C language version (Second Edition) (Song Xuefeng)
- ATmega16 MCU C language programming classic example (Chen Zhongping)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- BLE Bluetooth Protocol - BLE connection establishment process summary
- Today at 10:00 AM, live broadcast with prizes: [Introduction to TI's GaN-based applications]
- AD20.0.13 gets stuck when starting Adding View: Explorer under WIN10.
- IIC bus MSP430F149 and 24c16 comprehensive experiment programming example
- Why are the high-precision positioning systems of Beidou and GPS not open to ordinary users?
- Low power solar cell charging
- What is the purpose of connecting multiple capacitors in parallel and using a polarized capacitor among them?
- Why are the wild elephants heading north? Use your imagination and make bold predictions!
- Bose small speaker disassembly
- Guangdong Tonly Semiconductor suffered a confined space suffocation accident: 4 workers from the outsourcing unit died