Interfacing DS1307 I2C RTC to PSoC®1 Project Name: Example_RTC_DS1307 Programming Language: C Associated Part Families: CY8C29/28/27/24/22/21xxx, CY8C23x33 CY7C603xx, CY7C64215, CYWUSB6953, CY8CLED02/04/08/16 CY8CLED04D01/02/03/04, CY8CTxx110, CY8CNP102 Software Version:PD5.2 Related Hardware: CY3210 PSoC Eval1 Board Author: Pushek Madaan Project Objective ® 2 This project demonstrates how to interface the PSoC 1 to an I C Real Time Clock (DS1307) and display the date and time on the LCD. Overview 2 This project uses the I2CHW User Module as I C Master to retrieve date and time from the DS1307 RTC and display it on the ® LCD. Clock output of DS1307 is configured as 1Hz which is used as an interrupt to PSoC 1 to read data from DS1307. The following block diagram shows the block level implementation of the project. User Module List and Placement The following table lists user modules used in this project and the hardware resources occupied by each user module. I2CHW User Module Placement System resource LCD Software implementation User Module Parameter Settings The following tables show the user module parameter settings for each of the user modules used in the project . I2CHW User Module Parameter Read_Buffer_Types Value RAM ONLY Comments Only RAM data buffer is used. CPU_Clk_speed_(CY8C27xA) Not CY8C27xA See the Notes section at the end of this table. I2C Clock 100KHz Standard Sets the I2C clock as 100 kHz. I2C Pin P1[5]-P1[7] Selects P1[5] and P1[7] for I2C communication. P1[5] is SDA and P1[7] is SCL. Notes 1 2 When the Read_Buffer_Types is set to RAM ONLY, only RAM buffers are transmitted over I C. To read and transmit data from the Flash buffer, set the read buffer type to RAM or FLASH. The parameter CPU_Clk_speed is provided as a workaround for a silicon issue that was present in CY8C27x43A (Silicon Rev. A) family of devices. In this family, read or write to the I2C_CFG and I2C_SCR registers occurred with CPU speed 2 less than 6 MHz. If the CPU speed is greater than 6 MHz, it is throttled down to 6 MHz when accessing the I C registers and restored after the access. This workaround is not required for families other than the CY8C27x43A family. 2 2 The I C Clock parameter is dependent on the SysClk. The I C clock setting in the user module is based on a SysClk of 24 2 2 MHz. In devices which support slower Sysclk, the I C clock is reduced by the same proportion. For example, if I C clock is 2 set to 400 kHz and SysClk is set to 6 MHz, the actual I C clock is only 100 kHz. LCD User Module Parameter Value Comments LCDPort Port 2 Use Port 2 to connect LCD. Bargraph Disable Disable the Bargraph feature. Note For more details regarding User Module parameters, please refer to the UM datasheet which can be located from Start All Programs Cypress PSoC Designer 5.1 Documentation User Module Datasheet STDUM. Global Resources Parameter Power Setting [Vcc/SysClk freq] Important Global Resources Value Comments 5.0 V/24 MHz Selects 5 V operation and 24 MHz SysClk. CPU_Clock SysClk/2 Selects 12 MHz as the clock input for the CPU. VC3 Source SysClk/1 Set SysClk as the clock source for VC3. VC3 Divider 240 Divides SysClk by 240 and generates a clock of 100 kHz. Pinout Pin P1[6] Select StdCPU Drive High Z Important Pin Configuration Interrupt Comments ChangeFromRead Configures the interrupt at both rising and falling edges. Notes As the GPIO interrupt triggers on both rising and falling edge, the interrupt occurs every 0.5 seconds. It is enough to have the GPIO trigger on rising edge and read the RTC every one second. However, the 2 Hz interrupt is provided so that code can be added to blink a dot on the LCD to indicate seconds. Other parameters are left at their default value. 2 Hardware Connections The schematic diagram for the project follows. VCC 10K 1 2 3 4 5 6 7 8 9 10 11 12 13 14 VCC VCC 24 4 25 3 26 2 27 1 9 19 14 P0[0] P0[1] P0[2] P0[3] P0[4] P0[5] P0[6] P0[7] P2[0] P2[1] P2[2] P2[3] Ext AGND/P2[4] P2[5] Ext VREF/P2[6] P2[7] SMP XRES XtalOut/SDATA/P1[0] XtalIn/SCLK/P1[1] P1[2] P1[3] EXTCLK/P1[4] SDA/P1[5] P1[6] SCL/P1[7] VSS 28 20 8 21 7 22 6 23 5 VCC VCC Vss Vdd Vee RS R/W E D0 D1 D2 D3 D4 D5 D6 D7 LCD 15 13 16 12 17 11 18 10 VCC R1 5.6K R2 5.6K U2 6 5 1 2 SCL SDA X1 X2 1 2 Y1 32.768KHz 4 VCC VBAT SQW/OUT 8 3 R3 5.6K 7 GND CY8C29466-24PXI 3V DS1307 Y1 is a 32.768 kHz crystal connected to the crystal pins of the RTC. SQW pin is configured in firmware to generate a 1 Hz clock. This is an open drain pin and R3 is the pull up resistor for this pin. The SQW pin is connected to P1[6], configured to generate an interrupt on both the edges of the 1 Hz clock. The RTC is read on these interrupts. R1 and R2 are pull up 2 resistors for the I C bus. The LCD is a 2x16 alphanumeric LCD based on the Hitachi HD44780 controller and is connected to Port2 of the PSoC. This schematic can be wired using the bread board area of the CY3210 PSoC Eval1 board. This kit is available on our website at the following link: CY3210 – PsoCEval1. Operation On reset, all hardware settings from the device configuration are loaded into the device and main.c is executed. The following operations are performed by the firmware. Global interrupt is enabled. The I2CHW module is configured as Master and its interrupt is enabled. The RTC configuration register is written to configure SQW pin to produce a 1 Hz output. GPIO interrupt is enabled. The SQW output from the RTC is connected to P1[6] which is configured for a ChangeFromRead interrupt. The RTC is configured to generate a 1 Hz clock on the SQW output. As the GPIO interrupt triggers on both rising and falling edge, the interrupt occurs every 0.5 seconds. The time to be set to the RTC is loaded to the SetTime structure and the RTC_SetTime function is called. This is a onetime operation. After the time in the RTC is set, there is no need to set it every time the PSoC is reset. The call to RTC_SetTime may be commented out. An infinite loop is entered where the following operations are performed. 3 Check if the fReadRTC is set. This flag is set after every 0.5 seconds inside the RTC_Read_ISR interrupt service routine. If fReadRTC is set: 1. Clear the fReadRTC flag. 2. Call RTC_ReadTime to read the time into the structure Time. 3. Display the time on LCD. GPIO ISR The ISR for GPIO is written in C. The ISR function is named as RTC_Read_ISR and is declared as an ISR using the following code in the beginning of main.c. #pragma interrupt_handler RTC_Read_ISR An ljmp _RTC_Read_ISR statement is placed in the PSoCGPIOINT.asm. When the interrupt occurs, the control is transferred to the RTC_Read_ISR. This interrupt is triggered after every 0.5 seconds because it is configured as ChangeFromRead. For more details about writing an ISR in C, refer to the following Knowledge Base article on the Cypress website: Implementing an Interrupt Service Routine in C on the PSoC. SETUP & OUTPUT PICTURE 4 Upgrade Information The ljmp _RTC_Read_ISR code is placed within the user code marker area in the PSoCGPIOINT.asm file. Therefore, any change to the device editor and subsequent application generation does not affect this code. However, if PSoC Designer™ is updated to a later version, which has an upgrade to the PSoCGPIOINT.asm file, then the code inside the ISR may be lost. For any PSoC Designer version upgrade, make sure that the ljmp _RTC_Read_ISR code is present inside the PSoCGPIOINT.asm file. Otherwise, manually place this code inside the user code marker area. 5 PSoC is a registered trademark and PSoC Designer is a trademark of Cypress Semiconductor Corp. All other trademarks or registered trademarks referenced herein are the property of their respective owners. Cypress Semiconductor 198 Champion Court San Jose, CA 95134-1709 Phone: 408-943-2600 Fax: 408-943-4730 http://www.cypress.com/ © Cypress Semiconductor Corporation, 2009-2011. The information contained herein is subject to change without notice. Cypress Semiconductor Corporation assumes no responsibility for the use of any circuitry other than circuitry embodied in a Cypress product. Nor does it convey or imply any license under patent or other rights. Cypress products are not warranted nor intended to be used for medical, life support, life saving, critical control or safety applications, unless pursuant to an express written agreement with Cypress. Furthermore, Cypress does not authorize its products for use as critical components in life-support systems where a malfunction or failure may reasonably be expected to result in significant injury to the user. The inclusion of Cypress products in life-support systems application implies that the manufacturer assumes all risk of such use and in doing so indemnifies Cypress against all charges. This Source Code (software and/or firmware) is owned by Cypress Semiconductor Corporation (Cypress) and is protected by and subject to worldwide patent protection (United States and foreign), United States copyright laws and international treaty provisions. Cypress hereby grants to licensee a personal, non-exclusive, non-transferable license to copy, use, modify, create derivative works of, and compile the Cypress Source Code and derivative works for the sole purpose of creating custom software and or firmware in support of licensee product to be used only in conjunction with a Cypress integrated circuit as specified in the applicable agreement. Any reproduction, modification, translation, compilation, or representation of this Source Code except as specified above is prohibited without the express written permission of Cypress. Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress reserves the right to make changes without further notice to the materials described herein. Cypress does not assume any liability arising out of the application or use of any product or circuit described herein. Cypress does not authorize its products for use as critical components in life-support systems where a malfunction or failure may reasonably be expected to result in significant injury to the user. The inclusion of Cypress’ product in a life-support systems application implies that the manufacturer assumes all risk of such use and in doing so indemnifies Cypress against all charges. Use may be limited by and subject to the applicable Cypress software license agreement. 6