Application design based on AVR microcontroller and serial clock device DS3231

Publisher:JoyfulHarmonyLatest update time:2013-10-11 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Reference address:Application design based on AVR microcontroller and serial clock device DS3231

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

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号