1 Introduction
The measurement and control system or intelligent display screen composed of a single chip often needs to use a real-time calendar or clock signal. In order to save CPU resources, this article introduces the application of the serial clock device DS3231 and its interface with the AVR microcontroller ATmega8. The system has the characteristics of strong anti-interference ability, accurate timing and no error with seasonal changes.
2 Structure and working principle of DS3231
DS3231 is a low-cost, high-precision I2C real-time clock (RTC) device with an integrated temperature compensated crystal oscillator (TCXO). The device contains a battery input terminal, which can still maintain accurate timing when the main power is disconnected. The integrated crystal oscillator can improve the long-term accuracy of the device. The registers of DS3231 can save information such as seconds, minutes, hours, days of the week, dates, months, years, and alarm settings. For months with less than 31 days, the end of the month can be automatically adjusted, including leap year compensation. The clock works in 24-hour format or 12-hour format with AM/PM indication. DS3231 provides two programmable calendar alarms and one programmable square wave output. The address and data are transmitted through the I2C bidirectional serial bus.
2.1 Composition and working principle of DS3231
As shown in Figure 1, the main components of DS3231 are 8 modules, which are divided into 4 functional groups: TCXO, power control, push button reset and RTC.
2.1.1 32 kHz TCXO
The TCXO consists of a temperature sensor, an oscillator, and control logic. The controller reads the on-chip temperature sensor output and uses a table lookup to determine the required capacitance, plus the aging correction in the AGE register. The capacitor selection register is then set. New values, including changes in the AGE register, are loaded only when the temperature changes or when a user-initiated temperature conversion is completed. The temperature value is read when VCC is first powered up and then every 64 s.
2.1.2 Power Control
The power control function is provided by a temperature-compensated voltage reference (VPF) and a comparator circuit that monitors the VCC level. The DS3231 is powered by VCC when VCC is above VPF, by VCC when VCC is below VPF but above VBAT, and by VBAT when VCC is below VPF and below VBAT. To protect the battery, the oscillator does not start when VBAT is first applied to the device unless VCC is loaded or a valid I2C address is written to the device. Typical oscillator startup time is less than 1 s. Approximately 2 s after VCC is applied or a valid I2C address is written, the device measures the temperature and calibrates the oscillator using the calculated correction value. Once the oscillator is running, it remains operational as long as the power supply (VCC or VBAT) is valid. The device measures the temperature and calibrates the oscillator frequency every 64 s.
2.1.3 Push-Button Reset
The DS3231 has a push-button switch function connected to the RST output pin. If the DS3231 is not in a reset cycle, it continuously monitors the RST signal for a falling edge. If an edge transition is detected, the DS3231 debounces the switch by pulling RST low. After the internal timer expires, the DS3231 continues to monitor the RST signal. If the signal remains low, the DS3231 continuously monitors the signal line for a rising edge. Once the button release is detected, the DS3231 forces RST low for tRST. RST can also be used to indicate a power-fail alarm condition. When VCC falls below VPF, an internal power-fail alarm signal is generated and the RST pin is forced low. When VCC returns to a level above VPF. RST is held low for approximately 250 ms (tREC) to allow the power supply to stabilize. If the oscillator is not operating when VCC is loaded, tREC will be skipped and RST will immediately go high.
2.1.4 Clock and Calendar The RTC
can obtain clock and calendar information by reading the appropriate register bytes. The clock and calendar data are set or initialized by writing the appropriate register bytes. The contents of the clock and calendar registers are in binary-coded decimal (BCD) format. The DS3231 operates in 12-hour or 24-hour mode. Bit 6 of the hour register is defined as the 12- or 24-hour mode select bit. When this bit is high, 12-hour mode is selected. In 12-hour mode, bit 5 is the AM/PM indicator bit, which is PM when logic high.
2.1.5 Alarms
The DS3231 contains two time/date alarms. Alarm 1 can be set by writing to registers 07h to 0Ah. Alarm 2 can be set by writing to registers 0Bh to 0Dh. The alarm can be programmed (via the alarm enable and INTCN bits of the control register) to trigger the INT/SQW output on an alarm match condition. Bit 7 of each time/date alarm register is the mask bit. When the mask bits of each alarm are logic 0, the alarm will only sound when the value in the time register matches the corresponding value stored in the time/date alarm register. The alarm can also be programmed to repeat every second, minute, hour, day of week, or date. When the RTC register value matches the set value of the alarm register, the corresponding alarm flag bit A1F or A2F is set to logic 1. If the corresponding alarm interrupt enable bit A1IE or A2IE is also set to logic 1 and the INTCN bit is set to logic 1, the alarm condition will trigger the INT/SQW signal. The RTC detects a match every second when the time and date registers are updated.
2.2 Internal Registers and Functions of the DS3231
The DS3231 register addresses are 00h to 12h, which are used to store seconds, minutes, hours, day of week, date, and alarm setting information respectively. During a multi-byte access, if the address reaches the end of the RAM space at 12h, a wraparound occurs, positioning to the beginning, i.e., cell 00h. The time and calendar information of the DS3231 is set and initialized by reading the corresponding registers. Auxiliary (user) buffers are used to prevent errors that may occur when the internal registers are updated. When reading the time and calendar registers, the user buffer is synchronized with the internal registers at any START condition or when the register pointer returns to zero. The time information is read from these auxiliary registers while the clock continues to run. This avoids re-reading the registers when the main registers are updated during the read operation. Taking the control register (address 0EH) as an example, the real-time clock, alarm clock, and square wave output can be controlled. The definitions of each bit are shown in Table 1.
BIT7: Enable Oscillator (EOEC). When set to logic 0, the oscillator starts. If set to logic 1, the oscillator stops when the DS3231 power is switched to VBAT. This bit is cleared (logic 0) when power is first applied. When the DS3231 is powered by VCC, the oscillator remains operational regardless of the state of the EOSC bit.
BIT6: Battery-Backed Square-Wave Enable (BBSOW). When set to Logic 1 and the DS3231 is powered from the VBAT pin, this bit enables the square-wave output when VCC is not applied. When BB-SQW is set to Logic 0, the INT/SQW pin becomes high impedance if VCC drops below the power-fail threshold. This bit is cleared (Logic 0) when power is first applied.
BIT5: Convert Temperature (CONV). When this bit is set to 1, it forces the temperature sensor to convert the temperature to a digital value and executes the TCXO algorithm to update the oscillator's capacitor array. Valid only during idle periods. Setting the Convert Temperature bit is disabled when Status Bit BSY = 1. The user should check Status Bit BSY before forcing the controller to start a new TCXO operation. User-initiated temperature conversions do not affect the internal 64 s update cycle. User-initiated temperature conversions do not affect the BSY bit for approximately 2 ms. The CONV bit remains 1 from the time it is written until the conversion is complete, at which point both CONV and BSY become 0. The CONV bit should be used when monitoring the status of user-initiated conversions.
Bits BIT4 and BIT3: Frequency selection (RS2 and RS1). When powered on for the first time, BIT4 and BIT3 are set to logic 1. They are used to control the frequency of the square wave output when the square wave is enabled. The relationship between the logic values of RS1 and RS2 and the square wave output frequency is listed in Table 2. [page]
BIT2: Interrupt Control (INTCN). This bit controls the INT/SQW signal. When INTCN is set to 0, the INT/SQW pin outputs a square wave. When INTCN is set to 1, if the timer register matches any of the alarm registers, the INT/SQW signal is triggered (if the alarm is also enabled). The corresponding alarm flag is always set when a match occurs, regardless of the state of the INTCN bit. When power is first applied, the INTCN bit is set to logic 1.
BIT1: Alarm 2 Interrupt Enable (A2IE). When this bit is set to logic 1, the Alarm 2 Flag bit (A2F) in the Status Register is allowed to trigger the INT/SQW signal (when INTCN = 1). When the A2IE bit is set to 0 or INTCN is set to 0, the A2F bit does not initiate the interrupt signal. When power is first applied, the A2IE bit is cleared (logic 0).
BIT0: Alarm 1 Interrupt Enable (A1IE). When this position is logic 1, the alarm 1 flag (A1F) in the status register is allowed to trigger the INT/SQW signal (when INTCN=1). When the A1IE position is 0 or INTCN is set to 0, the A1F bit does not start the INT/SQW signal. When the power is first turned on, the A1IE bit is cleared (logic 0).
2.3 Data exchange and format of DS3231
DS3231 acts as a slave device on the I2C bus. It can be accessed only after executing the START command and verifying the device address. Then the registers can be accessed until a STOP command is executed.
All address packets transmitted on the I2C bus are 9 bits long, which includes 7 address bits, 1 R/W control bit and 1 acknowledge bit ACK. If R/W is 1, a read operation is performed; if R/W is 0, a write operation is performed. After the slave is addressed, it must respond by pulling SDA low in the 9th SCL (ACK) cycle. If the slave is busy or unable to respond to the master, SDA should be kept high during the ACK cycle. Then the master sends a STOP state or a REP START state to restart transmission. The address packet includes the slave address and the READ or WRITE bit called SLA+R or SLA+W. The MSB of the address byte is sent first. All addresses of 1111xxxx are reserved. For future use.
All data packets transmitted on the I2C bus are 9 bits long, which includes 8 data bits and 1 acknowledge bit. During data transmission, the master generates the clock and the START and STOP states, and the receiver responds to reception. The acknowledgement is achieved by ACK pulling down SDA in the 9th SCL cycle. If the receiver pulls SDA high, a NACK signal is sent. If the receiver cannot receive more data for some reason, it should send a NACK signal after the last data byte to tell the transmitter to stop sending and send the MSB of the data first.
DS3231 exchanges data with the outside world through the bidirectional data line SDA and the clock line SCL. From its timing relationship, it can be seen that DS3231 has two operation modes:
(1) Write operation: write the data on the SDA data line into N bytes of data in sequence according to the first address (Word Address) specified by the RAM. The master device first transmits the address byte of the slave device, followed by a series of data bytes. The slave device returns an ACK bit after receiving each byte. Its format is shown in Figure 2.
(2) Read operation: Read N bytes of data in sequence according to the first address specified by the RAM. The master device first transmits the slave device address. The slave device returns an acknowledge bit. This is followed by a series of data bytes transmitted by the slave device. The master device returns an acknowledge bit after receiving all bytes except the last byte. After receiving the last byte, it returns a "non-acknowledge bit" NACK. Its format is shown in Figure 3.
In the above read and write operation signals: S is the start signal (START), 1101 000 is the port address of DS1307, A is the response signal ACK, A is the non-response signal NACK, and P is the stop signal (STOP). The master device generates all serial clocks and START and STOP conditions, and stops them by transmitting STOP and retransmitting START conditions.
3 Hardware interface
between DS3231 and AVR microcontroller The ATmega series microcontroller integrates a 2-wire serial interface module. Atmel calls it the TWI interface. In fact, TWI is the same as Philips' I2C bus. The TWI interface implemented by AVR hardware is byte-oriented and interrupt-based. It has better real-time performance and code efficiency than the software-simulated I2C bus. The pin input part also has a glitch suppression unit to remove high-frequency interference. Figure 4 is the schematic diagram of the hardware interface circuit between DS3231 and AVR microcontroller ATmega8.
4 Software interface between DS3231 and AVR microcontroller
In software design, the AVR microcontroller ATMega8 and the real-time clock device DS3231 must first be initialized. Give the DS3231 an accurate date and time. Regardless of the master mode or the controlled mode, the TWEN bit of the TWI control register TWCR should be set to 1 to enable the TWI module. After the TWEN bit is set, the I/O pins PC5 and PC4 are converted to SCL and SDA. The operation of the TWI control register TWCR can generate START and STOP signals on the bus. From START to STOP is considered to be the behavior of the master mode. Set the first bit TWGCE of the TWI address register TWAR to valid, and set the TWEA (response enable) bit of the TWI control register TWCR to 1, and the TWI module can respond to its addressing on the bus and set the status word. The operations on the TWI module are all read and write operations on registers. Avr-libc does not provide a special API. The standard I/O based on US-ART can be used to implement the operations of reading and writing the calendar and clock of DS3231.
5 Conclusion
A calendar clock system is designed by using the real-time clock device DS3231 and the AVR microcontroller ATmega8. The system is small in size and highly versatile. The DS3231 has an integrated temperature compensated crystal oscillator (TCXO), which is accurate in timing and does not produce errors with seasonal changes. The system is suitable for smart display screens and real-time measurement and control systems.
Previous article:Circuit diagram and programming example of the interface between AVR microcontroller and CF card
Next article:Heating control system based on AVR microcontroller design
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
- EEWORLD University Hall----Live Replay: ADI MEMS Sensors Open a New Era of Conditional State Monitoring
- Unboxing ESP32-S2 and S3
- Easily solve design challenges with MSP430 MCUs
- TI Logistics Robot CPU Board
- I would like to ask which one has better performance, thank you for your recommendation
- TI supports Bluetooth technology for high-precision body temperature measurement flexible PCB reference design
- Problems of charging and discharging dual capacitors in peak detectors
- EK140P Linux-4.1.15 Test Manual
- EEWORLD University Hall----Live Replay: HARTING- How to quickly and cost-effectively install cables in electrical control cabinets
- Introduction to Machine Vision Technology