DS1302 实时时钟晶体设计注意事项

DS1302 实时时钟晶体设计注意事项
DS1302 实时时钟晶体设计注意事项

Maxim > App Notes > 1-Wire? DEVICES REAL-TIME CLOCKS TIME AND TEMPERATURE RECORDERS

Mar 29, 2001 Keywords: RTC, crystal, 32kHz, real time clock, oscillator, timekeeping, time keeping, 32768, 32.768, elapsed time counter,

ETC, time error, incorrect time, accuracy, timekeeping ppm

APPLICATION NOTE 58

Crystal Considerations with Dallas Real-Time Clocks (RTCs)

Abstract: This application note describes crystal selection and layout techniques for connecting a 32,768Hz crystal to a real-time clock (RTC). It also provides information about oscillator circuit-design criteria, system design, and manufacturing issues.

Oscillator Basics

The oscillator used in Dallas Semiconductor RTCs is a CMOS inverter variation of a Pierce-type oscillator. Figure 1 shows a general configuration. These RTCs include integrated load capacitors (C L1 and C L2) and bias resistors. The Pierce oscillator utilizes a crystal operating in parallel-resonance mode. Crystals used in parallel-resonance mode will be specified for a certain frequency with a specific load capacitance. For the oscillator to run at the correct frequency, the oscillator circuit must load the crystal with the correct capacitive load.

Figure 1. RTC oscillator with internal load capacitors and bias resistors.

Accuracy

The frequency accuracy of a crystal-based oscillator circuit is mainly dependent upon the accuracy of the crystal and the accuracy of the match between the crystal and the oscillator capacitive load. If the capacitive load is less than the crystal was designed for, the oscillator runs fast. If the capacitive load is greater than what the crystal was designed for, the oscillator runs slow.

In addition to the errors from the crystal and the load match, crystals vary from their base frequency as the ambient temperature changes. Dallas RTCs use "tuning fork" crystals, which exhibit an error over temperature, as shown in Figure 2. An error of 20ppm is equivalent to approximately 1 minute per month.

Figure 2. Crystal frequency vs. temperature.

Note: If better accuracy is required, a TCXO such as the DS32kHz can be used.

Crystal Parameters

Figure 3 shows the equivalent circuit for a crystal. Near the resonate frequency the circuit consists of a series circuit including motional inductance L1, motional resistance R1, and motional capacitance C1. The parallel component C O is the shunt capacitance of the crystal.

Figure 3. Crystal equivalent circuit.

The load capacitance C L is the capacitive load of the oscillating circuit as seen from the pins of the crystal. Figure 4 shows C L as a capacitance in parallel with the crystal. The load capacitors used in an oscillator circuit, C L1 and C L2, plus any stray capacitance in the circuit, combine to create the overall load capacitance. All Dallas RTCs have integrated C L1 and C L2 capacitors. Care should be taken to minimize stray capacitance in the PC board layout. The following formula shows the relationship between C L and load capacitor values:

Figure 4. Crystal load capacitors and equivalent parallel load.

Most crystals allow a maximum drive level of 1μW. All Dallas RTCs run under 1μW. Drive level may be determined using the following formula:

P = 2R1 × [π × 32,768(C O + C L)V RMS]2,

where V RMS is the RMS value of the voltage across the crystal.

Oscillator Startup Time

Oscillator startup times are highly dependent upon crystal characteristics, PC board leakage, and layout. High ESR and excessive capacitive loads are the major contributors to long startup times. A circuit using a crystal with the recommended characteristics and proper layout usually starts within one second.

Table 1. Crystal Specifications

Parameter Symbol Min Typ Max Units

Nominal Frequency F O32.768kHz

Frequency Tolerance delta F/ F O±20ppm

Load Capacitance C L6pF

Temperature Turnover Point T0202530°C

Parabolic Curvature Constant k0.042ppm/°C

Quality Factor Q40,00070,000

Series Resistance ESR45k?

Shunt Capacitance C0 1.1 1.8pF

Capacitance Ratio C0/C1430600

Drive Level D L1μW

Note 1: Some devices allow higher ESR values, check the datasheet for specific requirements.

Power Consumption

Many RTCs are designed to operate from a battery supply. In a typical application, a small lithium battery can be used to run the oscillator and clock circuitry while the main supply is off. To maximize battery life, the oscillator must run using as little power as possible. To accomplish this, some design tradeoffs must be made.

Negative Resistance

For typical high-frequency oscillator circuits, it is normal for the circuit to be designed with a 5 or 10X margin for the ESR. Low-frequency crystals typically have higher ESRs. An RTC oscillator may have less than a 2X margin for negative resistance. An oscillator circuit with a low margin normally consumes less current. As a result, an RTC oscillator often is sensitive to relatively small amounts of stray leakage, noise, or an increase in ESR.

The C L of the oscillator circuit influences the power consumption. An RTC with 12.5pF internal loads consumes more power than one that has 6pF loads. However, the oscillator with 12.5pF load capacitors is usually less susceptible to noise. Crystal Layout Guidelines

Since the crystal inputs of Dallas RTCs have very high impedance (about 109?), the leads to the crystal act like very good antenna, coupling high-frequency signals from the rest of the system. If a signal is coupled onto the crystal pins, it can either cancel out or add pulses. Since most of the signals on a board are at a much higher frequency than the 32.768kHz crystal, it is more likely to add pulses where none are wanted. These noise pulses get counted as extra clock "ticks" and make the clock appear to run fast.

The following steps illustrate how to determine if noise is causing the RTC to run fast:

1. Power the system up and synchronize the RTC to a known accurate clock.

2. Turn the system power off.

3. Wait for a period of time (two hours, 24 hours, etc.). The longer the time period, the easier it is to measure the

accuracy of the clock.

4. Turn the system on again, read clock, and compare to the known accurate clock.

5. Resynchronize the RTC to the known accurate clock.

6. Keep the system powered up and wait for a period of time equal to the period in Step 3.

7. Read the clock after waiting for the above period of time and compare it to the known accurate clock.

By using the above steps, the accuracy of the clock can be determined both when the system is powered up and when the system is powered down. If the clock proves to be inaccurate when the system is powered up, but is accurate when the system is powered down, the problem is most likely due to noise from other signals in the system. However, if the clock is inaccurate both when the system is powered up and when it is powered down, then the problem is not due to noise from the system.

Since it is possible for noise to be coupled onto the crystal pins, care must be taken when placing the external crystal on a PC board layout. It is very important to follow a few basic layout guidelines concerning the placement of the crystal on the PC board layout to ensure the extra clock ticks do not couple onto the crystal pins.

1. It is important to place the crystal as close as possible to the X1 and X2 pins. Keeping the trace lengths between the

crystal and RTC as small as possible reduces the probability of noise coupling by reducing the length of the antenna.

Keeping the trace lengths small also decreases the amount of stray capacitance.

2. Keep the crystal bond pads and trace width to the X1 and X2 pins as small as possible. The larger these bond pads

and traces are, the more likely it is that noise can couple from adjacent signals.

3. If possible, place a guard ring (connected to ground) around the crystal. This helps isolate the crystal from noise

coupled from adjacent signals. See Figure 2 for an illustration of using a guard ring around a crystal.

4. Try to ensure that no signals on other PC board layers run directly below the crystal or below the traces to the X1 and

X2 pins. The more the crystal is isolated from other signals on the board, the less likely it is that noise is coupled into the crystal. There should be a minimum of 0.200 inches between any digital signal and any trace connected to X1 or X2. The RTC should be isolated from any component that generates electromagnetic radiation (EMR). This is true for discrete and module type RTCs.

5. It may also be helpful to place a local ground plane on the PC board layer immediately below the crystal. This helps

Note that care must be taken concerning the use of a local ground plane because of the stray capacitance that it introduces. The capacitance between the traces/pads and ground plane is added to the internal load capacitors (C L1 and C L2). Therefore, some factors must be taken into account when considering adding a local ground plane. For example, the capacitance due to the ground plane can be approximated by the following equation:

C = εA/t, where

ε = dielectric constant of the PC board

A = area of the traces/pads

t = thickness of the PC board layer

Therefore, to determine if a ground plane is appropriate for a given design, the above parameters must be taken into account to ensure that the capacitance from the local ground plane is not sufficiently large enough to slow down the clock.

Figure 5. Recommended layout for crystal.

Checking For Oscillation

The first impulse that a designer has when checking for oscillator operation often is to connect an oscilloscope probe to the oscillator input (X1) or output (X2) pin. Doing so is not recommended when using a Real-Time Clock. Since the oscillator is designed to run at low power (which extends operating time from a battery), loading the oscillator with an oscilloscope probe is likely to stop the oscillator. If the oscillator does not stop, the additional loading will reduce the signal amplitude, and may cause erratic operation, such as varying amplitude. Oscillation should therefore be verified indirectly.

Oscillation can be verified several ways. One method is to read the seconds register multiple times, looking for the data to increment. On RTCs with an OSF (Oscillator Stop Flag), clearing and then monitoring this bit will verify that the oscillator has started and is continuously running. These methods won't work if the designer is troubleshooting a design and cannot communicate with the RTC. An alternate method is to check the square wave output on RTCs that have a square wave output. Check the datasheet to verify if the RTC must be written first to enable the oscillator and square wave output. Note that most RTC square wave outputs are open-drain, and require a pull up resistor for operation. The square wave output can also be used to verify the accuracy of the RTC, however, a frequency counter with sufficient accuracy must be used.

Fast Clocks

The following are the most common scenarios that cause a crystal-based RTC to run fast.

specified load capacitance is used. The severity of the inaccuracy is dependent on the value of the C L. For example, using a crystal with a C L of 12pF on an RTC designed with a 6pF C L causes the RTC to be about 3 to 4 minutes per month fast.

Slow Clocks

The following are the most common scenarios that cause a crystal-based RTC to run slow.

1. Overshoots on RTC input pins. It is possible to cause a RTC to run slow by periodically stopping the oscillator. This

can be inadvertently accomplished by noisy input signals to the RTC. If an input signal rises to a voltage that is

greater than a diode drop (~0.3V) above V DD, the ESD protection diode for the input pin will forward bias, allowing the substrate to be flooded with current. This, in turn, stops the oscillator until the input signal voltage decreases to below a diode drop above V DD.

This mechanism can cause the oscillator to stop frequently if input signals are noisy. Therefore, care should be taken to ensure there is no overshoot on input signals.

Another situation that is common to overshoot problem is having an input to the RTC at 5V when the RTC is in

battery-backup mode. This can be a problem in systems that systematically shut down certain circuits but keep

others powered up. It is very important to ensure there are no input signals to the RTC that are greater than the

battery voltage (unless stated otherwise in the device data sheet) when the device is in battery-backup mode.

2. Wrong crystal. A RTC typically runs slow if a crystal with a specified C L is less than the C L of the RTC. The severity of

the inaccuracy is dependent on the value of the C L.

3. Stray capacitance. Stray capacitance between the crystal pins and/or to ground can slow an RTC down. Therefore,

care must be taken when designing the PC board layout to ensure the stray capacitance is kept to a minimum.

4. Temperature. The further the operating temperature is from the crystal turnover temperature, the slower the crystal

oscillates. See Figures 3 and 4.

Clock Does Not Run

The following are the most common scenarios that cause a RTC to not run.

1. The single most common problem when the clock does not run is that the CH (clock halt) or EOSC (enable oscillator)

bit has not been set or cleared, as required. Many Dallas RTCs include a circuit that keeps the oscillator from running when power is first applied. This allows a system to wait for shipment to the customer, without drawing power from the backup battery. When the system is powered for the first time, the software/firmware must enable the oscillator and prompt the user for the correct time and date.

2. Surface mount crystals may have some N.C. (no connect) pins. Make sure that the correct pins from the crystal are

connected to the X1 and X2 pins.

Crystal Manufacturing Issues

Tuning fork crystals should not be exposed to ultrasonic cleaning. They are susceptible to damage from resonant vibration. Crystals should not be exposed to temperatures above their maximum ratings. Exposure to excessive temperatures may damage the crystal, and usually increase the ESR. Crystal "cans" should not be soldered to a PC board. This is sometimes done to ground the case of the crystal. Soldering directly to the case of the crystal usually subjects the unit to excessive temperatures.

RTCs should generally be used in noncondensing environments. Moisture forming around the oscillator conductors can cause leakage, which can cause the oscillator to stop. Conformal coatings can be used to protect the circuit, however, conformal coating may by itself cause problems.

Some conformal coatings, especially epoxy-based materials, can have unacceptable levels of ionic contamination. In addition, conformal coatings can, if the PC board surface is not sufficiently cleaned prior to conformal coating, cause contaminants to concentrate around leads and traces.

DS1558:QuickView-- Full (PDF) Data Sheet

DS1602:QuickView-- Full (PDF) Data Sheet

DS1670:QuickView-- Full (PDF) Data Sheet-- Free Samples DS1672:QuickView-- Full (PDF) Data Sheet-- Free Samples DS1673:QuickView-- Full (PDF) Data Sheet-- Free Samples DS1677:QuickView-- Full (PDF) Data Sheet-- Free Samples DS1678:QuickView-- Full (PDF) Data Sheet-- Free Samples DS1678:QuickView-- Full (PDF) Data Sheet-- Free Samples DS1685:QuickView-- Full (PDF) Data Sheet-- Free Samples DS1688:QuickView-- Full (PDF) Data Sheet

DS1689:QuickView-- Full (PDF) Data Sheet

DS17285:QuickView-- Full (PDF) Data Sheet-- Free Samples DS17485:QuickView-- Full (PDF) Data Sheet-- Free Samples DS17885:QuickView-- Full (PDF) Data Sheet-- Free Samples

AN58, AN 58, APP58, Appnote58, Appnote 58

Copyright ? by Maxim Integrated Products

Additional legal notices: https://www.360docs.net/doc/df11810686.html,/legal

ds1302时钟程序详解-ds1302程序流程图(C程序)

ds1302时钟程序详解,ds1302程序流程图(C程序) ds1302时钟程序详解 DS1302 的控制字如图2所示。控制字节的最高有效位(位7)必须是逻辑1,如果它为0,则不能把数据写入DS1302中,位6如果为0,则表示存取日历时钟数据,为1表示存取RAM数据;位5至位1指示操作单元的地址;最低有效位(位0)如为0表示要进行写操作,为1表示进行读操作,控制字节总是从最低位开始 输出。 2.3 数据输入输出(I/O) 在控制指令字输入后的下一个SCLK时钟的上升沿时,数据被写入DS1302,数据输入从低位即位0开始。同样,在紧跟8位的控制指令字后的下一个SCLK脉冲的下降沿读出DS1302的数据,读出数据时从 低位0位到高位7。 2.4 DS1302的寄存器 DS1302有12个寄存器,其中有7个寄存器与日历、时钟相关,存放的数据位为BCD码形式,其日 历、时间寄存器及其控制字见表1。 此外,DS1302 还有年份寄存器、控制寄存器、充电寄存器、时钟突发寄存器及与RAM相关的寄存器等。时钟突发寄存器可一次性顺序读写除充电寄存器外的所有寄存器容。 DS1302与RAM相关的寄存器分为两类:一类是单个RAM单元,共31个,每个单元组态为一个8位的字节,其命令控制字为C0H~FDH,其中奇数为读操作,偶数为写操作;另一类为突发方式下的RAM寄存器,此方式下可一次性读写所有的RA M的31个字节,命令控制字为FEH(写)、FFH(读)。

ds1302程序流程图 3.2 DS1302实时时间流程 图4示出DS1302的实时时间流程。根据此流程框图,不难采集实时时间。下面结合流程图对DS1302的基 本操作进行编程:

基于DS1302的数码管显示数字钟

单片机原理课程设计 课题名称:基于DS1302的数码管显示数字钟 专业班级:电子信息工程 学生学号: 学生姓名: 指导教师: 设计时间:2010年6月21日--2010年6月25日

目录 摘要........................................................................................................................................................................ 1 设计任务和要求............................................................................................................................................ 2 方案论证........................................................................................................................................................ 3 系统硬件设计................................................................................................................................................ 3.1 系统总原理图 ................................................................................................................................ 3.2 元器件清单...................................................................................................................................... 3.3 PCB板图....................................................................................................................................... 3.4 Proteus仿真图 ............................................................................................................................... 3.5 分电路图及原理说明................................................................................................................... 3.5.1 主控部分(单片机MCS-51).............................................................................. 3.5.2 计时部分(实时时钟芯片DS1302).................................................................. 3.5.3 显示部分(共阳极数码管)................................................................................ 3.5.4 调时部分(按键)................................................................................................ 4系统软件设计................................................................................................................................................ 4.1 程序流程图..................................................................................................................................... 4.2 程序源代码........................................................................................................................................ 5心得体会........................................................................................................................................................ 6参考文献........................................................................................................................................................ 7结束语............................................................................................................................................................

DS1302时钟芯片读写详解

DS1302时钟芯片读写详解 2008-09-26 13:07 /*DS1302读写程序(C51)*/ sbit DS13CLK =P1^5; /*DS1302的SCLK脚脉冲*/ sbit DS13IO =P1^6; /*DS1302的IO脚数据*/ sbit DS13CS =P1^7; /*DS1302的RST脚片选*/ /*向DS1302写一个字节*/ void _wds13byte(uchar _code) { uchar i; DS13CLK =0; DS13CLK =0; for(i=0;i<8;i++) { if(_code&0x01) DS13IO =1; else DS13IO =0; DS13CLK =1; DS13CLK =1; DS13CLK =0; DS13CLK =0; _code =_code >> 1; } } /*从DS1302读一个字节*/ uchar _rds13byte(void) { uchar i,_code; _code=0; DS13CLK =0; DS13CLK =0; DS13IO =1; for(i=0;i<8;i++) { _code =_code >>1; if(DS13IO) _code =_code|0x80; DS13CLK =1; DS13CLK =1; DS13CLK =0; DS13CLK =0; } return _code; } /*读功能_code读功能命令*/ uchar readds1302(uchar _code)

{ DS13CS =0; /*关闭DS1302*/ DS13CLK =0; DS13CLK =0; DS13CS =1; /*使能DS1302*/ _wds13byte(_code); /*读代码*/ _code=_rds13byte(); /*返回读取数字*/ DS13CLK =1; DS13CS =0; /*关闭DS1302*/ return _code; } /*写功能fp写的地址,_code写的内容*/ void writeds1302(uchar fp,uchar _code) { DS13CS =0; /*关闭DS1302*/ DS13CLK =0; DS13CLK =0; DS13CS =1; /*使能DS1302*/ _wds13byte(fp); /*写控制命令*/ _wds13byte(_code); /*写入数据*/ DS13CLK=1; DS13CS =0; /*关闭DS1302*/ } /*******DS1302设置快速充电***************/ void ds13_charg(void) { writeds1302(0x8e,0x00); /*解除写保护*/ writeds1302(0x90,0xa5); /*单二极管2K电阻充电*/ writeds1302(0x8e,0x80); /*置位写保护*/ } ;;;DS1302读写程序(汇编);;; ;******************************************************************* **/ T_CLK Bit P1.5 ;实时时钟时钟线引脚 T_IO Bit P1.6 ;实时时钟数据线引脚 T_RST Bit P1.7 ;实时时钟复位线引脚 ;********************************************************** ;子程序名:Set1302 ;功能:设置DS1302 初始时间,并启动计时。 ;说明: ;调用:RTInputByte ;入口参数:初始时间在:Second,Minute,Hour,Day,Month,Week.YearL(地址连续) ;出口参数:无 ;影响资源:A B R0 R1 R4 R7

DS1302时钟芯片的原理与应用

DS1302 时钟芯片的原理与应用 1 写保护寄存器操作 当写保护寄存器的最高位为0 时,允许数据写入寄存器,写保护寄存器可以通过命令字节8E 8F 来规定禁止写入/读出。写保护位不能在多字节传送模式下写入Write_Enable: MOV Command,#8Eh ;命令字节为8E MOV ByteCnt,#1 ;单字节传送模式 MOV R0,#XmtDat 数据地址覆给R0 MOV XmtDat,#00h 数据内容为0 写入允许 ACALL Send_Byte 调用写入数据子程序 RET 返回调用本子程序处 当写保护寄存器的最高位为1 时禁止数据写入寄存器 Write_Disable: MOV Command,#8Eh ;命令字节为8E MOV ByteCnt,#1 ;单字节传送模式 MOV R0,#XmtDat 数据地址覆给R0 MOV XmtDat,#80h 数据内容为80h 禁止写入 ACALL Send_Byte 调用写入数据子程序 RET 返回调用本子程序处 以上程序调用了基本数据发送(Send_Byte)模块及一些内存单元定义, 其源程序清单在附录中给出下面 的程序亦使用了这个模块 2 时钟停止位操作 当把秒寄存器的第7 位时钟停止位设置为0 时起动时钟开始 Osc_Enable: MOV Command,#80h ; 命令字节为80 MOV ByteCnt,#1 ; 单字节传送模式 MOV R0,#XmtDat 数据地址覆给R0 MOV XmtDat,#00h 数据内容为0 振荡器工作允许 ACALL Send_Byte 调用写入数据子程序 RET 返回调用本子程序处 当把秒寄存器的第7 位时钟停止位设置为1 时,时钟振荡器停止DS1320 进入低功耗方式 Osc_Disable: MOV Command,#80h ;命令字节为80 MOV ByteCnt,#1 ;单字节传送模式 MOV R0,#XmtDat 数据地址覆给R0 MOV XmtDat,#80h 数据内容为80h 振荡器停止 ACALL Send_Byte 调用写入数据子程序 RET 返回调用本子程序处 3. 多字节传送方式

DS1302时序和C语言程序

1 DS130 2 简介: DS1302是美国DALLAS公司推出的一种高性能、低功耗的实时时钟芯片,附加31字节静态RAM,采用SPI三线接口与CPU进行同步通信,并可采用突发方式一次传送多个字节的时钟信号和RAM数据。实时时钟可提供秒、分、时、日、星期、月和年,一个月小与31天时可以自动调整,且具有闰年补偿功能。工作电压宽达2.5~5.5V。采用双电源供电(主电源和备用电源),可设置备用电源充电方式,提供了对后背电源进行涓细电流充电的能力。DS1302的外部引脚分配如图1所示及内部结构如图2所示。DS1302用于数据记录,特别是对某些具有特殊意义的数据点的记录上,能实现数据与出现该数据的时间同时记录,因此广泛应用于测量系统中。 图1 DS1302的外部引脚分配 图2 DS1302的内部结构 各引脚的功能为: Vcc1:主电源;Vcc2:备份电源。当Vcc2>Vcc1+0.2V时,由Vcc2向DS1302供电,当Vcc2< Vcc1时,由Vcc1向DS1302供电。 SCLK:串行时钟,输入; I/O:三线接口时的双向数据线; CE:输入信号,在读、写数据期间,必须为高。该引脚有两个功能:第一,CE开始控制字访问移位寄存器的控制逻辑;其次,CE提供结束单字节或多字节数据传输的方法。 DS1302有下列几组寄存器: ①DS1302有关日历、时间的寄存器共有12个,其中有7个寄存器(读时81h~8Dh,写时80h~8Ch),存放的数据格式为BCD码形式,如图3所示。

图 3 DS1302有关日历、时间的寄存器 小时寄存器(85h、84h)的位7用于定义DS1302是运行于12小时模式还是24小时模式。当为高时,选择12小时模式。在12小时模式时,位5是,当为1时,表示PM。在24小时模式时,位5是第二个10小时位。 秒寄存器(81h、80h)的位7定义为时钟暂停标志(CH)。当该位置为1时,时钟振荡器停止,DS1302处于低功耗状态;当该位置为0时,时钟开始运行。 控制寄存器(8Fh、8Eh)的位7是写保护位(WP),其它7位均置为0。在任何的对时钟和RAM 的写操作之前,WP位必须为0。当WP位为1时,写保护位防止对任一寄存器的写操作。 ②DS1302有关RAM的地址 DS1302中附加31字节静态RAM的地址如图4所示。 图4 ③DS1302的工作模式寄存器 所谓突发模式是指一次传送多个字节的时钟信号和RAM数据。突发模式寄存器如图5所示。 图5 ④此外,DS1302还有充电寄存器等。 2 读写时序说明 DS1302是SPI总线驱动方式。它不仅要向寄存器写入控制字,还需要读取相应寄存器的数据。 要想与DS1302通信,首先要先了解DS1302的控制字。DS1302的控制字如图6。 图6 控制字(即地址及命令字节) 控制字的最高有效位(位7)必须是逻辑1,如果它为0,则不能把数据写入到DS1302中。

ds1302时钟程序详解经典

dsl302时钟程序详解经典 dsl302时钟程序详解 DS1302的控制字如图2所示。控制字节的最高有效位(位7)必须是逻辑1,如果它为0,则不能把数据写入DS1302中,位6如果为0,则表示存取日历时钟数据,为1表示存取RAM数据;位5至位1指示操作单元的地址;最低有效位(位0)如为0表示要进行写操作,为1表示进行读操作,控制字节总是从最低位开始 2.3数据输入输出(I/O) 在控制指令字输入后的下一个SCLK时钟的上升沿时,数据被写入DS1302,数据输入从低位即位0开始。同样,在紧跟8位的控制指令字后的下一个SCLK脉冲的下降沿读出DS1302的数据,读出数据时从低位0 位到高位7o 2.4 DS1302的寄存器 DS1302有12个寄存器,其中有7个寄存器与日历、时钟相关,存放的数据位 为BCD码形式,其日历、 时间寄存器及其控制字见表1。

? I日历?別间襦存祁及凡担制孑 fir* 野擅"itwtr 収他总cn - T ?fsy网 移 e S』3 2 1 0 林斶 son8!ll00-59 f.H IUSVX SIX X2H S3II oum(1Mh、 MH K4H851101 \2A12 24? 10 IIH HK MhH M7II01 -2S.2V, W-Jl ?o imiAre 8SH WII03 - !2(11) 0 IUM MOYI1I AAII8HH ni(i II ? 0 0h\V 8LH Mill OQ ? 9910YLAH 此外,DS1302还有年份寄存器、控制寄存器、充电寄存器、时钟突发寄存器 及与RAM相关的寄存器等。时钟突发寄存器可一次性顺序读写除充电寄存器外的所有寄存器内容。DS1302与RAM相关的寄存器分为两类:一类是单个RAM单元,共31个,每个单元组态为一个8位的字节,其命令控制字为COH, FDH,其中奇数为读操作,偶数为写操作;另一类为突发方式下的RAM寄存器,此方式下可一次性读写所有的RAM的31个字节,命令控制字为FEH(写)、FFH(读)。 dsl302程序流程图

ds1302时钟程序详解 含电路图 源程序 注释

以下资料摘自电子发烧友网感谢作者,版权归网站所有,资料仅供参考 ds1302时钟程序详解 DS1302 的控制字如图2所示。控制字节的最高有效位(位7)必须是逻辑1,如果它为0,则不能把数据写入DS1302中,位6如果为0,则表示存取日历时钟数据,为1表示存取RAM数据;位5至位1指示操作单元的地址;最低有效位(位0)如为0表示要进行写操作,为1表示进行读操作,控制字节总是从最低位开始输出。 2.3 数据输入输出(I/O) 在控制指令字输入后的下一个SCLK时钟的上升沿时,数据被写入DS1302,数据输入从低位即位0开始。同样,在紧跟8位的控制指令字后的下一个SCLK脉冲的下降沿读出DS1302的数据,读出数据时从低位0位到高位7。 2.4 DS1302的寄存器 DS1302有12个寄存器,其中有7个寄存器与日历、时钟相关,存放的数据位为BCD码形式,其日历、时间寄存器及其控制字见表1。

此外,DS1302 还有年份寄存器、控制寄存器、充电寄存器、时钟突发寄存器及与RAM相关的寄存器等。时钟突发寄存器可一次性顺序读写除充电寄存器外的所有寄存器内容。DS1302与RAM相关的寄存器分为两类:一类是单个RAM单元,共31个,每个单元组态为一个8位的字节,其命令控制字为C0H~FDH,其中奇数为读操作,偶数为写操作;另一类为突发方式下的RAM寄存器,此方式下可一次性读写所有的RAM的31个字节,命令控制字为FEH(写)、FFH(读)。 ds1302程序流程图

3.2 DS1302实时时间流程 图4示出DS1302的实时时间流程。根据此流程框图,不难采集实时时间。下面结合流程图对DS1302的基本操作进行编程:

DS1302实时时钟模块,1602显示

/*******************说明:************************** 将实时时钟数据通过LCD1602显示 -------------------------------------------------- 基于51单片机 **************************************************/ #include #include #define uchar unsigned char #define uint unsignedint uchardis_time_buf[16]={0}; //LCD1602引脚定义 //采用8位并行方式,DB0~DB7连接至LCDDATA0~LCDDATA7 sbit RS=P2^0; sbit RW=P2^1; sbit CS=P2^2; #define LCDDATA P0 //DS1302引脚定义 sbit RST=P1^3; sbit IO=P1^2; sbit SCK=P1^1; //DS1302地址定义 #define ds1302_sec_add 0x80 //秒数据地址 #define ds1302_min_add 0x82 //分数据地址 #define ds1302_hr_add 0x84 //时数据地址 #define ds1302_date_add 0x86 //日数据地址 #define ds1302_month_add 0x88 //月数据地址 #define ds1302_day_add 0x8a //星期数据地址 #define ds1302_year_add 0x8c //年数据地址 #define ds1302_control_add 0x8e //控制数据地址 #define ds1302_charger_add 0x90 #define ds1302_clkburst_add 0xbe //初始时间定义 uchartime_buf[8] = {0x20,0x10,0x06,0x01,0x23,0x59,0x55,0x02};//初始时间2010年6月1号23点59分55秒星期二 //功能:延时1毫秒 //入口参数:x //出口参数:无 //说明:当晶振为12M时,j<112;当晶振为11.0592M时,j<122 voidDelay_xms(uint x) { uinti,j; for(i=0;i

51单片机DS1302日历时钟程序

51 单片机ds1302 时钟芯片 #define uint unsigned int #define uchar unsigned char sbit lcdrs = P1^0; sbit lcdrw = P1^1; sbit lcden = P1^2; sbit key0 = P2^0;//功能键,选择时分秒 sbit key1 = P2^1;//加1键 sbit key2 = P2^2;//减1键 sbit key4 = P2^4; sbit clk_1302 = P1^5; //1302芯片位定义sbit io_1302 = P1^6; sbit rst_1302 = P1^7; uchar bdata dat; sbit dat0 = dat^0; sbit dat7 = dat^7; uchar key0_count;//按键0被按的次数(0~3) uchar flag; char hour,minute,second; uchar table_date[] = "2009-4-12 Mon"; uchar table_time[] = "00:00:00"; /****** 函数申明********/ void write_cmd_1602(uchar cmd); void write_data_1602(uchar dat); void write_add(uchar add,uchar dat); void init1602(); void delay(uint z); uchar reverse(uchar c); void keyscan(); void init(); void RTC_initial (); void wr_1302(uchar wr_data); uchar rd_1302(void); uchar uc_R1302(uchar ucAddr); void v_W1302(uchar ucAddr, uchar ucDa); 主程序 #include

51单片机可调的ds1302时钟程序

#include #include #define uchar unsigned char #define uint unsigned int uchar count_10ms; //定义10ms计数器 sbit K1 = P3^7; //定义K1键 sbit K2 = P3^4; //定义K2键 sbit K3 = P3^5; //定义K3键 sbit K4 = P3^6; //P1^3定义K4键 sbit K5 = P3^3; sbit BEEP=P2^2; //定义蜂鸣器 sbit reset = P1^6; //P2^5; sbit sclk = P1^7; //P2^6; sbit io = P2^6; //P2^7; sbit LCD_RS=P1^0; sbit LCD_RW=P1^1 ; sbit LCD_EN=P1^2; bit K1_FLAG=0; //定义按键标志位,当按下K1键时,该位置1,K1键未按下时,该位为0。 uchar disp_buf[16] ={0x00}; //定义显示缓冲区 uchar time_buf[7] ={0,0,0x12,0,0,0,0}; //DS1302时间缓冲区,存放秒、分、时、日、月、星期、年 uchar temp [2]={0}; //用来存放设置时的小时、分钟的中间值 uint m=0,n=0,r=0; /********以下是函数声明********/ void Delay_ms(uint xms) ; bit lcd_busy(); void lcd_wcmd(uchar cmd); void lcd_wdat(uchar dat) ; void lcd_clr() ; void lcd_init() ; void write_byte(uchar inbyte); //写一字节数据函数声明 uchar read_byte(); //读一字节数据函数声明 void write_ds1302(uchar cmd,uchar indata); //写DS1302函数声明 uchar read_ds1302(uchar addr); //读DS1302函数声明 void set_ds1302(uchar addr,uchar *p,uchar n); //设置DS1302初始时间函数声明 void get_ds1302(uchar addr,uchar *p,uchar n); //读当前时间函数声明 void init_ds1302(); //DS1302初始化函数声明 /********以下是延时函数********/ void Delay_ms(uint xms) { uint i,j;

基于DS1302的实时时钟设计报告

课程设计 设计题目:基于DS1302的实时时钟设计 学生姓名:黄景军指导教师:高峰 二级学院:龙蟠学院专业:电气工程及其自动化 班级:M11电气工程及其自动化学号: 1121109023

摘要 近年来随着计算机在社会领域的渗透和大规模集成电路的发展,单片机的应用正在不断地走向深入,由于它具有功能强,体积小,功耗低,价格便宜,工作可靠,使用方便等特点,因此特别适合于与控制有关的系统,越来越广泛地应用。本设计采用实时时钟芯片DS1302,基于AT89C51单片机的设计制作了具有红外遥控、LED显示、可设定时的电子时钟,达到制作的目的,并用protuse和medwin进行模拟实验。本报告中主要介绍了系统的硬件设计和软件设计,并用八位共阳极LED数码管,采用查询法查键实现。 关键词:单片机DS1302 AT89C51 共阳极LED数码显示器

目录 设计任务及主要技术指标和要求 (3) 1.1 设计任务 (3) 1.2 主要技术指标和要求 (3) 二、引言 (3) 三、核心芯片简介 (4) 3.1 DS1302简介 (5) 3.1.1 DS1302引脚功能与内部结构 (5) 3.1.2 DS1302的寄存器 (5) 3.2 AT89C52简介 (6) 四、方案设计和论证 (6) 五、软硬件设计 (7) 5.1硬件电路设计 (7) 5.2软件实现 (8) 5.2.1主程序 (8) 5.2.2时钟读出程序设计 (12) 5.2.3时钟调整程序设计 (13) 六、系统调试 (13) 七、总结 (14) 八、参考文献 (14)

一、设计任务及主要技术指标和要求 1.1 设计任务: 用DS1302时钟芯片设计实时时钟。 1.2 主要技术指标和要求: (1)通过程序直接对实时时钟时间进行设置,启动时钟运行。用8位数码管显示。 (2)通过按钮对实时时钟时间进行设置,使时钟能正常运行。 (3)其他发挥功能。 二、引言 从古代的滴漏更鼓到近代的机械钟,从电子表到目前的数字时钟,为了准确的测量和记录时间,人们一直在努力改进着计时工具。钟表的数字化,大力推动了计时的精确性和可靠性。在单片机构成的装置中,实时时钟是必不可少的部件。目前常用的实时时钟,很多采用单片机的中断服务来实现,这种方式一方面需要采用计数器,占用硬件资源,另一方面需要设置中断、查询等,同样耗费单片机的资源,而且某些测控系统可能不允许;有的则使用并行接口的时钟芯片,如MC146818、DS12887等,它们虽然能满足单片机系统对实时时钟的要求,但是这些芯片与单片机接口复杂,占用地址、数据总线多,芯片体积大,占用空间多,给其它设计带来诸多不便。本设计选取串行接口时钟芯片DS1302与单片机同步通信构成数字时钟电路。其简单的三线接口能为单片机节省大量资源,DS1302的后背电源及对后背电源进行涓细电流充电的能力保证电路断电后仍能保存时间和数据信 息等。这些优点解决了目前常用的实时时钟所无法解决的问题。该时钟电路强大的功能和优越的性能,在很多领域的应用中,尤其是某些自动化控制、长时间无人看守的测控系统等对时钟精确性和可靠性有较高要求的场合,具有很高的使用价值。

51单片机+带字库液晶12864+DS1302数字时钟C源程序(无按键修改功能)

51单片机+带字库液晶12864+DS1302数字时钟C源程序(无按键修改功能)过两天的搜索与调试,在别人程序的基础上,不断修改,终于调试成功了这个程序。目前还不能修改时间与日期,只是以预定时间以始。 适用于开发板:51单片机(AT89S52)+带字库液晶12864(ST7920)+DS1302(实时时钟) 实现功能:简单,数字时钟+日期(以后会不断完美)。 C语言源程序如下: #include #include #define uchar unsigned char #define uint unsigned int /*DS1302 端口设置 */ sbit SCK=P3^6; //DS1302时钟 sbit SDA=P3^4; //DS1302 IO sbit RST = P3^5; // DS1302复位 bit ReadRTC_Flag; //读DS1302全局变量 /* 12864端口定义*/ #define LCD_data P0 //带字库液晶12864数据口 sbit LCD_RS = P2^4; //寄存器选择输入 sbit LCD_RW = P2^5; //液晶读/写控制 sbit LCD_EN = P2^6; //液晶使能控制 sbit PSB=P2^1; //并口控制 sbit RES=P2^3; uchar code dis1[] = {" 电子设计天地"}; //液晶显示的汉字 uchar code dis2[] = {"有志者,事竟成!"}; uchar code dis4[] = {'0','1','2','3','4','5','6','7','8','9'}; unsigned char temp; #define delayNOP(); {_nop_();_nop_();_nop_();_nop_();}; void lcd_pos(uchar X,uchar Y); //确定显示位置 unsigned char l_tmpdate[7]={0,7,16,19,10,1,9};//秒分时日月周年 09-10-19 16:07:00 code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日

ds1302时钟原理介绍

早就已经不在学校了,可是前几天突然有老童学问我有没有保存这方面的资料,赶紧翻了一下我的电脑,呵呵,还是找到了一些资料,顺便共享出来,有需要的同学们拿走后留个言吧——可以的话。最后感谢无名的原作者。 DS1302的特点 DS1302是美国DALLAS公司推出的一种高性能、低功耗的实时时钟芯片,附加31字节静态RAM,采用SPI三线接口与CPU进行同步通信,并可采用突发方式一次传送多个字节的时钟信号和RAM数据。实时时钟可提供秒、分、时、日、星期、月和年,一个月小与31天时可以自动调整,且具有闰年补偿功能。工作电压宽达2.5~5.5V。采用双电源供电(主电源和备用电源),可设置备用电源充电方式,提供了对后背电源进行涓细电流充电的能力。DS1302的外部引脚分配如图1所示。DS1302用于数据记录,特别是对某些具有特殊意义的数据点的记录上,能实现数据与出现该数据的时间同时记录,因此广泛应用于测量系统中。 各引脚的功能 Vcc1:主电源;Vcc2:备份电源。当Vcc2>Vcc1+0.2V时,由Vcc2向DS1302供电,当Vcc2< Vcc1时,由Vcc1向DS1302供电。SCLK:串行时钟,输入;I/O:三线接口时的双向数据线;CE:输入信号,在读、写数据期间,必须为高。该引脚有两个功能:第一,CE开始控制字访问移位寄存器的控制逻辑;其次,CE提供结束单字节或多字节数据传输的方法。 DS1302的几组寄存器以及有关RAM的地址

(1)DS1302有关日历、时间的寄存器 DS1302有关日历、时间的寄存器共有12个,其中有7个寄存器(读时81h~8Dh,写时80h~8Ch),存放的数据格式为BCD码形式,如图3所示。 (2)小时寄存器(85h、84h)的位7用于定义DS1302是运行于12小时模式还是24小时模式。当为高时,选择12小时模式。在12小时模式时,位5是,当为1时,表示PM。在24小时模式时、位5是第二个10小时位。 (3)秒寄存器(81h、80h)的位7定义为时钟暂停标志(CH)。当该位置为1时,时钟振荡器止,DS1302处于低功耗状态;当该位置为0时,时钟开始运行。(4)控制寄存器(8Fh、8Eh)的位7是写保护位(WP),其它7位均置为0。在任何的对时钟和RAM的写操作之前,WP位必须为0。当WP位为1时,写保护位防止对任一寄存器的写操作。 (5)DS1302中附加31字节静态RAM的地址如图4所示。 (6)DS1302的工作模式寄存器 所谓突发模式是指一次传送多个字节的时钟信号和RAM数据。突发模式寄存器如图5所示

ds1302的12864液晶按键可调显示实时时钟程序

一.绪言 1.在信息显示技术中,人们发现了信息数字化的重要作用和意义。数字化的信息更加准确,同一性,更易传输和识别。很多信息可以直接由数字表示,从而数字化信息显示又成为信息显示的又一个重要内容。又从数字化显示发展到字符显示,它把人类特有的语言文字用于显示,这种显示与数字显示合在一起用途更广用量更大。在这同时,人们还希望用图形和图像进行显示,且显示的内容为五彩缤纷,并且可以实时活动和具有三维立体效果。这些在二十世纪尾声时都已经陆续实现。LCD的计算机器,半导体发光数码管显示(LED)的汽车计价器,商场的大屏幕广告。这零零总总的各类显示正为你做着各种各样的服务,相信在不久的将来显示技术的发展将会为人类做出更大的贡献。 Ds1302时钟芯片现在流行的串行时钟电路很多,如DS1302、 DS1307、PCF8485等。这些电路的接口简单、价格低廉、使用方便,被广泛地采用。本文介绍的实时时钟电路DS1302是DALLAS公司的一种具有涓细电流充电能力的电路,主要特点是采用串行数据传输,可为掉电保护电源提供可编程的充电功能,并且可以关闭充电功能。采用普通3 2.768kHz晶振。(一)设计任务 本课题采用ds1302的时钟芯片为主要的的设计来源,采用显示是1cd1602的图形点阵液晶显示。,

(二)提出方案及方案论证 在设计中,我主要是考虑ds1302的时间显示问题,因为网上也有ds1302的读写和显示程序,但是至于怎样才能显示详细信息,我提出了两个方案,一个是LCD1602来显示,一个是LCD12864,其中1602是个字符型的点阵,而1CD12864是个图形的点阵,相比之下12864能够更好显示数据,而CPU都采用89C52的单片机 (三)原理说明:原理其实很简单,LCD12864的各种指令先进行宏定义和ds1302的读写指令也进行宏定义,从ds1302的读出数据显示在lcd上 (二)程序流程图

电子万年历设计(基于AT89C51单片机和DS1302时钟芯片)1

随着人们生活水平的提高和生活节奏的加快,对时间的要求越来越高,精准数字计时的消费需求也是越来越多。 二十一世纪的今天,最具代表性的计时产品就是电子万年历,它是近代世界钟表业界的第三次革命。第一次是摆和摆轮游丝的发明,相对稳定的机械振荡频率源使钟表的走时差从分级缩小到秒级,代表性的产品就是带有摆或摆轮游丝的机械钟或表。第二次革命是石英晶体振荡器的应用,发明了走时精度更高的石英电子钟表,使钟表的走时月差从分级缩小到秒级。第三次革命就是单片机数码计时技术的应用(电子万年历),使计时产品的走时日差从分级缩小到1/600万秒,从原有传统指针计时的方式发展为人们日常更为熟悉的夜光数字显示方式,直观明了,并增加了全自动日期、星期、温度以及其他日常附属信息的显示功能,它更符合消费者的生活需求!因此,电子万年历的出现带来了钟表计时业界跨跃性的进步…… 我国生产的电子万年历有很多种,总体上来说以研究多功能电子万年历为主,使万年历除了原有的显示时间,日期等基本功能外,还具有闹铃,报警等功能。商家生产的电子万年历更从质量,价格,实用上考虑,不断的改进电子万年历的设计,使其更加的具有市场。 本设计为软件,硬件相结合的一组设计。在软件设计过程中,应对硬件部分有相关了解,这样有助于对设计题目的更深了解,有助于软件设计。基本的要了解一些主要器件的基本功能和作用。 除了采用集成化的时钟芯片外,还有采用MCU的方案,利用AT89系列单片微机制成万年历电路,采用软件和硬件结合的方法,控制LED数码管输出,分别用来显示年、月、日、时、分、秒,其最大特点是:硬件电路简单,安装方便易于实现,软件设计独特,可靠。AT89C51是由ATMEL 公司推出的一种小型单片机。95年出现在中国市场。其主要特点为采用Flash存贮器技术,降低了制造成本,其软件、硬件与MCS-51完全兼容,可以很快被中国广大用户接受。 本文介绍了基于AT89C51单片机设计的电子万年历。 首先我们在绪论中简单介绍了单片机的发展与其在中低端领域中的优

以DS1302实时时钟芯片及液晶显示屏CD1602为基础设计的电子钟

电子系统设计实验报告 实验目的: 1、能够以DS1302实时时钟芯片和液晶显示屏CD1602为基础设计一款电子钟 2、熟悉DS1302芯片的工作过程 3、熟悉CD1602芯片的工作过程 4、可以进行必要的扩展,如用第三DS18B20新品进行温度采集和显示 5、熟悉掌握51的c程序的编写 6、掌握用Proteus进行系统设计仿真验证 实验仪器、仪表目录 1、DS1302实时时钟芯片1片 2、LCD1602液晶显示屏1个, 3、80C52芯片1片 5、DS18B20芯片一片 6、晶振、电容、电阻、开关各若干等 7、proteus仿真软件 8、Keil C51、PC机 实验设计任务 以DS1302实时时钟芯片和液晶显示屏LCD1602为基础设计一个电子钟,要求:时间和日期可调整,按键采用3个按键;至少在Proteus上调试通过。扩展:闹钟和重要日期提醒功能(增加蜂鸣器),闹铃音乐功能 实验步骤 1、打开Keil软件,新建一个工程文件,选择好芯片,并记得在“Options for Target 1”的Output 选项中,将Create HEX Fil选项勾起来。 2、将编写的程序保存成“.C”的形式 3、编译保存好的C文件,并根据提示修改程序中的错误,直到编译成功为止 4、打开proteus软件,画出实验电路图 5、在89C52中,载入原来已生成的HEX文档

6、按下运行键,对Proteus进行软件仿真,观察运行结果 原理、结果及分析 一、设计方案原理与设计特点分析 电子钟总的设计模块: 各个模块电路原理分析: 1、DS1302时钟采集模块: 1.1电路原理图: 1.2 DS1302分析: 首先DS1302是DALLAS公司推出的涓流充电时钟芯片。内含有一个实时时钟/日历和31字节静态RAM通过简单的串行接口与单片机进行通信实时时钟/日历电路提供秒分时日日期月年的信息每月的天数和闰年的天数可自动调整时钟操作。 DS1302芯片广脚介绍:X1、X2为32.768KHz晶振管脚。GND 为地。RST复位脚。I/O 数据输入/输出引脚。SCLK串行时钟。Vcc1,Vcc2电源供电管脚。与单片机连接的信号线为:DS1302_SCLK 接P1^6; 实时时钟时钟线引脚 DS1302_IO 接P1^7; 实时时钟数据线引脚 DS1302_RST 接P1^5; 实时时钟复位线引脚 特别注意DS1302芯片在读取或写入数据时,都是一位一位传送的,并且每传送一位,SCLK信号线要有一个负跳变。即单片机对SCLK咬先送高电平,再送低电平。数据时通过IO进行传送的。 1.3数据处理子程序流程图

相关文档
最新文档