AN1364 Using the Alarm Feature on the MCP79410 RTCC to Implement a Delayed Alarm Author: • On-board 32,768 kHz Crystal Oscillator for the RTCC • On-chip Digital Trimming/Calibration of the Oscillator • Operates Down to 1.8V • Backup Voltage Down to 1.3V • Operating Temperature Range: - Industrial (I): -40C to +85C • Multi-function Pin: - Open-drain configuration - Programmable clock frequency out • Interrupt Capability (based on the 2 sets of alarm registers ALM0 and ALM1) • Timesaver Function • Time-stamp Registers for Holding the Time/Date of Crossing: - from VDD to VBAT - from VBAT to VDD Alexandru Valeanu Microchip Technology Inc. INTRODUCTION An increasing number of applications that involve time measurement are requiring a Real-Time Clock (RTC) device. The MCP79410 is a feature-rich Real-Time Clock and Calendar (RTCC) that incorporates EEPROM, SRAM, unique ID and time-stamp. This application note describes the use of the alarms that are available, in order to build a simple delayed alarm system. FEATURES OF THE RTCC STRUCTURE • I2C™ Bus Interface • RTCC with Time/Date: Year, Month, Date, Day of Week, Hours, Minutes, Seconds • Support for Leap Year • Low-power CMOS Technology • Input for External Battery Backup (maintains RTCC and SRAM contents) FIGURE 1: SCHEMATIC The schematic includes a PIC18 Explorer demo board and the I2C RTCC PICtail™ daughter board as shown in Figure 1. SCHEMATIC DB7 - 0 LCD LUMEX MCP23S17 RS SPI Expander CS SDI E SCK VDD RC5/SD01 RA2 C2 = 0.1 µF C1 RB0 32,768 kHz VDD 1 X1 10 pF VDD 2K 2K PIC18F87J11 MFP 7 RA4/T0 CKI VBAT SCL 6 RC3/SCK1/SCL1 VSS SDA 5 RC4/SDA1 2 X2 3 4 C3 100 pF RTCC MCP 79410 VDD 8 Y R4 S1 VDD MENU KEY S2 RA5 BAT 85 1K C4 BAT RA0/AN0 INCR KEY 2K VDD 1K VDD 2010 Microchip Technology Inc. 10K POT R3 DS01364A-page 1 AN1364 The resources used on the demo board are: • EEPROM: 0xAE for writes, 0xAF for reads • LCD • 2 push buttons • The on-board potentiometer related to RA0 input and internal 10 bits ADC. • AC164140 RTCC PICtail daughter board The chip can support speeds up to: To access the LCD through a minimum of pins, the SPI on the MSSP1 module was used, in conjunction with a 16-bit I/O expander with SPI interface (MCP23S17). The two on-board push buttons are S1 and S2, connected to RB0, RA5 GPIOs. The I2C RTCC is part of the PICtail evaluation board and is directly connected to the MSSP1 module of the MCU. Another necessary connection is between the MFP signal of the RTCC and the RA4 pin. The RTCC is programed to set the MFP at the end of the delay. All connections between the I2C RTCC and the MCU (SDA, SCL, MFP) are open drain and use pull-up resistors. The RTCC PICtail daughter board has two other components: • a 32,768 Hz crystal driving the internal clock of the RTCC • a 3-volt battery sustaining the RTCC when VDD is not present on the demo board. DETAILS ABOUT IMPLEMENTATION The application is designed around the PIC18 Explorer board, running on a PIC18F87J11 MCU. The code is written using the C18 compiler. The firmware implements a delayed alarm system, based on the internal alarm registers for ALARM0. The value of the delay is imposed using the on-board potentiometer (R3) measured through the internal ADC and is enabled by the S1 push button. The delay is then written to the ALARM0 registers. Once this sequence is finished, the firmware displays minutes and seconds (up to 17 minutes and 3 seconds). Once this time has been reached, an alarm message will be shown on the LCD. The application restarts automatically after two seconds. FUNCTIONAL DESCRIPTION MCP79410 is an I2C slave device, working on the related bidirectional 2-wire bus. SDA is a bidirectional pin used to transfer addresses and data in and out of the device. It is an open-drain pin, therefore, the SDA bus requires a pull-up resistor to VCC (typically 10kΩ for 100 kHz, 2kΩ for 400 kHz). For normal data transfers, SDA is allowed to change only during SCL low. Changes during SCL high are reserved for indicating the Start and Stop conditions. SCL input is used to synchronize the data transfer from and to the device. The related internal structures have the following device addresses/control bytes (the RTCC is included in the SRAM bank): • RTCC + SRAM: 0xDE for writes, 0xDF for reads DS01364A-page 2 • 400 kHz 2.5 to 5V • 100 kHz 1.8 to 2.5V APPLICATION DESCRIPTION This application performs a delayed alarm system. The firmware goes through three states: • Setting the value of the delay through the onboard potentiometer. The LCD screen shows: ALM = aa min S1 = ENABLE ALARM bb sec Once the value is enabled, it will be written in the related alarm registers and the RTCC will be initialized. • Reading the current time count (minutes and seconds). The related LCD screen is:. ALM = aa min mm bb sec ss • Reaching the end of the alarm delay. When a match occurs between the time count and the alarm registers, the MFP will be set. The pin is polled through firmware and the code stops displaying the time count. The LCD screen shows: END OF ALARM The application will restart automatically after two seconds. FIRMWARE DESCRIPTION Drivers The drivers are divided into 3 classes: • LCD drivers • RTCC registers access drivers • ADC drivers LCD Drivers The application is implemented on the specific hardware PIC18 Explorer demo board. On this board it was important to reduce the number of GPIO pins used to access the LCD. Accessing the LCD is performed on a SPI bus (included in the MSSP1 module) through an auxiliary chip, the MCP23S17 SPI expander. Since the application handles only ‘minutes’ and ‘seconds’, there are only two high-level LCD drivers: • void sec_to_lcd(void) • void min_to_lcd(void) Both of them are based on the basic LCD function: • void wrdata_lcd(unsigned char data_lcd) 2010 Microchip Technology Inc. AN1364 Drivers to Access RTCC Registers Since MCP79410 is an I2C RTCC, it will use the I2C bus of the MCU (included in the MSSP1 module). Accordingly, the related drivers will be divided into two categories: basic I2C drivers and RTCC drivers. They use as a control method the SPP1IF bit (flag) in the PIR1 register (interrupt flag of the MSSP1 module), read through polling and not through interrupts. The method represents an alternative to the classical “i2c.h” library, included in the C18 compiler. FIGURE 2: FLOWCHART FOR A TYPICAL WRITE OPERATION (FOR A RANDOM BYTE ACCESS): START FIGURE 3: FLOWCHART FOR A TYPICAL READ OPERATION START DEVICE_ADDR_WRITE REGISTER_ADDR RESTART DEVICE_ADDR_READ READ BYTE STOP DEVICE_ADDR_WRITE The two related functions are: void rtcc_wr (unsigned char time_var, unsigned char rtcc_reg); unsigned char rtcc_rd (unsigned char rtcc_reg); REGISTER_ADDR WRITE_BYTE STOP ADC Drivers EXAMPLE 1: ADC FUNCTIONS: void ini_adc (void) void adc_conv (void) ; // ; // ADC initalization (handling ADCON0, ADCON1 registers) ADC conversion (handles the ADCON0bits.GO bit) The internal 10-bit ADC and the on-board potentiometer are used to set the value of the delay, which will be written to the alarm registers of the RTCC. 2010 Microchip Technology Inc. DS01364A-page 3 AN1364 ACCESSING THE RTCC REGISTERS There are two basic functions for accessing the RTCC: one for writes and one for reads. They can be defined as: void rtcc_wr (unsigned char time_var, unsigned char rtcc_reg), unsigned char rtcc_rd (unsigned char rtcc_reg). Each of these two functions include error messages displayed on LEDs, which could signal when an operation is not acknowledged by the slave (RTCC). EXAMPLE 2: FLOWCHART FOR WRITES TO THE RTCC i2c_start() ; // start I2C communication: SDA goes down while SCL remains high i2c_wr(ADDR_RTCC_WRITE); // send the RTCC's address for write = 0xde i2c_wr(rtcc_reg) ; // send the register's address i2c_wr(time_var) ; // send data byte to the RTCC i2c_stop() ; // stop I2C communication: SDA goes high while SCL remains high EXAMPLE 3: FLOWCHART FOR READS FROM THE RTCC i2c_start() ; // start I2C communication: SDA goes down while SCL remains high i2c_wr(ADDR_RTCC_WRITE); // send the RTCC's address for write = 0xde i2c_wr(rtcc_reg) ; // send the register's address i2c_restart() ; // switch to reads i2c_wr(ADDR_RTCC_READ) ; // send the RTCC's address for read = 0xdf i2c_rd() ; // read the byte from the RTCC (register's content) i2c_nack ; // NoACK from MCU to the RTCC (no more bytes to read) i2c_stop() ; // stop I2C communication: SDA goes high while SCL remains high Only two time variables are used in this application: seconds and minutes. As described in the data sheet, the addresses of these registers are shown below: Seconds = 00h Minutes 01h = (START OSCILLATOR BIT is located in Bit 7) In addition, two control registers are used to initialize properly the RTCC for this application (void ini_rtcc (void)): • Control register located at address 07h. The constant written into it will enable ALARM0, initialize OUT (MFP = 0), with no square wave on MFP. • The ALARM0 Control register located at address 0Dh. The constant written into it will set the MFP pin when all alarm variables match. DS01364A-page 4 2010 Microchip Technology Inc. AN1364 CONCLUSION This application note presents how to use the alarm registers of Microchip’s I2C RTCC, MCP79410. It shows how to build a delayed alarm system (a microwave batch automation on which the delay is controlled through an I2C RTCC and a potentiometer related to an ADC). The project is designed around PIC18 Explorer demo board. Many of the on-board hardware resources are used and the code is written using the C18 compiler for portability. 2010 Microchip Technology Inc. DS01364A-page 5 AN1364 NOTES: DS01364A-page 6 2010 Microchip Technology Inc. Note the following details of the code protection feature on Microchip devices: • Microchip products meet the specification contained in their particular Microchip Data Sheet. • Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the intended manner and under normal conditions. • There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data Sheets. Most likely, the person doing so is engaged in theft of intellectual property. • Microchip is willing to work with the customer who is concerned about the integrity of their code. • Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not mean that we are guaranteeing the product as “unbreakable.” Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act. Information contained in this publication regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer’s risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights. Trademarks The Microchip name and logo, the Microchip logo, dsPIC, KEELOQ, KEELOQ logo, MPLAB, PIC, PICmicro, PICSTART, PIC32 logo, rfPIC and UNI/O are registered trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. FilterLab, Hampshire, HI-TECH C, Linear Active Thermistor, MXDEV, MXLAB, SEEVAL and The Embedded Control Solutions Company are registered trademarks of Microchip Technology Incorporated in the U.S.A. Analog-for-the-Digital Age, Application Maestro, CodeGuard, dsPICDEM, dsPICDEM.net, dsPICworks, dsSPEAK, ECAN, ECONOMONITOR, FanSense, HI-TIDE, In-Circuit Serial Programming, ICSP, Mindi, MiWi, MPASM, MPLAB Certified logo, MPLIB, MPLINK, mTouch, Omniscient Code Generation, PICC, PICC-18, PICDEM, PICDEM.net, PICkit, PICtail, REAL ICE, rfLAB, Select Mode, Total Endurance, TSHARC, UniWinDriver, WiperLock and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A. and other countries. SQTP is a service mark of Microchip Technology Incorporated in the U.S.A. All other trademarks mentioned herein are property of their respective companies. © 2010, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved. Printed on recycled paper. ISBN: 978-1-60932-605-0 Microchip received ISO/TS-16949:2002 certification for its worldwide headquarters, design and wafer fabrication facilities in Chandler and Tempe, Arizona; Gresham, Oregon and design centers in California and India. The Company’s quality system processes and procedures are for its PIC® MCUs and dsPIC® DSCs, KEELOQ® code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and analog products. In addition, Microchip’s quality system for the design and manufacture of development systems is ISO 9001:2000 certified. 2010 Microchip Technology Inc. DS01364A-page 7 Worldwide Sales and Service AMERICAS ASIA/PACIFIC ASIA/PACIFIC EUROPE Corporate Office 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 480-792-7200 Fax: 480-792-7277 Technical Support: http://support.microchip.com Web Address: www.microchip.com Asia Pacific Office Suites 3707-14, 37th Floor Tower 6, The Gateway Harbour City, Kowloon Hong Kong Tel: 852-2401-1200 Fax: 852-2401-3431 India - Bangalore Tel: 91-80-3090-4444 Fax: 91-80-3090-4123 India - New Delhi Tel: 91-11-4160-8631 Fax: 91-11-4160-8632 Austria - Wels Tel: 43-7242-2244-39 Fax: 43-7242-2244-393 Denmark - Copenhagen Tel: 45-4450-2828 Fax: 45-4485-2829 India - Pune Tel: 91-20-2566-1512 Fax: 91-20-2566-1513 France - Paris Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 Japan - Yokohama Tel: 81-45-471- 6166 Fax: 81-45-471-6122 Germany - Munich Tel: 49-89-627-144-0 Fax: 49-89-627-144-44 Atlanta Duluth, GA Tel: 678-957-9614 Fax: 678-957-1455 Boston Westborough, MA Tel: 774-760-0087 Fax: 774-760-0088 Chicago Itasca, IL Tel: 630-285-0071 Fax: 630-285-0075 Cleveland Independence, OH Tel: 216-447-0464 Fax: 216-447-0643 Dallas Addison, TX Tel: 972-818-7423 Fax: 972-818-2924 Detroit Farmington Hills, MI Tel: 248-538-2250 Fax: 248-538-2260 Kokomo Kokomo, IN Tel: 765-864-8360 Fax: 765-864-8387 Los Angeles Mission Viejo, CA Tel: 949-462-9523 Fax: 949-462-9608 Santa Clara Santa Clara, CA Tel: 408-961-6444 Fax: 408-961-6445 Toronto Mississauga, Ontario, Canada Tel: 905-673-0699 Fax: 905-673-6509 Australia - Sydney Tel: 61-2-9868-6733 Fax: 61-2-9868-6755 China - Beijing Tel: 86-10-8528-2100 Fax: 86-10-8528-2104 China - Chengdu Tel: 86-28-8665-5511 Fax: 86-28-8665-7889 Korea - Daegu Tel: 82-53-744-4301 Fax: 82-53-744-4302 China - Chongqing Tel: 86-23-8980-9588 Fax: 86-23-8980-9500 Korea - Seoul Tel: 82-2-554-7200 Fax: 82-2-558-5932 or 82-2-558-5934 China - Hong Kong SAR Tel: 852-2401-1200 Fax: 852-2401-3431 Malaysia - Kuala Lumpur Tel: 60-3-6201-9857 Fax: 60-3-6201-9859 China - Nanjing Tel: 86-25-8473-2460 Fax: 86-25-8473-2470 Malaysia - Penang Tel: 60-4-227-8870 Fax: 60-4-227-4068 China - Qingdao Tel: 86-532-8502-7355 Fax: 86-532-8502-7205 Philippines - Manila Tel: 63-2-634-9065 Fax: 63-2-634-9069 China - Shanghai Tel: 86-21-5407-5533 Fax: 86-21-5407-5066 Singapore Tel: 65-6334-8870 Fax: 65-6334-8850 China - Shenyang Tel: 86-24-2334-2829 Fax: 86-24-2334-2393 Taiwan - Hsin Chu Tel: 886-3-6578-300 Fax: 886-3-6578-370 China - Shenzhen Tel: 86-755-8203-2660 Fax: 86-755-8203-1760 Taiwan - Kaohsiung Tel: 886-7-213-7830 Fax: 886-7-330-9305 China - Wuhan Tel: 86-27-5980-5300 Fax: 86-27-5980-5118 Taiwan - Taipei Tel: 886-2-2500-6610 Fax: 886-2-2508-0102 China - Xian Tel: 86-29-8833-7252 Fax: 86-29-8833-7256 Thailand - Bangkok Tel: 66-2-694-1351 Fax: 66-2-694-1350 Italy - Milan Tel: 39-0331-742611 Fax: 39-0331-466781 Netherlands - Drunen Tel: 31-416-690399 Fax: 31-416-690340 Spain - Madrid Tel: 34-91-708-08-90 Fax: 34-91-708-08-91 UK - Wokingham Tel: 44-118-921-5869 Fax: 44-118-921-5820 China - Xiamen Tel: 86-592-2388138 Fax: 86-592-2388130 China - Zhuhai Tel: 86-756-3210040 Fax: 86-756-3210049 08/04/10 DS01364A-page 8 2010 Microchip Technology Inc.