PSoC Lab 08: Build a Digital Clock System using DS3231 I2C (RTC)

Objective

  • Learn how to communicate with devices using I2C protocol
  • Learn the blocking and non-blocking I2C APIs to read and write the data 

Required Reading Materials

Background Information

The DS3231 is a low-cost, extremely accurate I2C realtime clock (RTC) with an integrated temperature compensated crystal oscillator (TCXO) and crystal. The device incorporates a battery input, and maintains accurate timekeeping when main power to the device is interrupted.

The RTC maintains seconds, minutes, hours, day, date, month, and year information. The date at the end of the month is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with an AM/PM indicator. Two programmable time-of-day alarms and a programmable square-wave output are provided. Address and data are transferred serially through an I2C bidirectional bus.

 

Project Requirements

Design a digital system, that can display current date and time on the character LCD.

  • Use the DS3231 RTC module (or other RTC module)
  • When the system is turned on, the system will ask for updating the time. If the user press the SW within two seconds after the message shows, the system will update date and time using the default value.
  • System will display the date / time in 12-hour format on the LCD screen, and update it every second.
    2019/09/20 FRI
    08:12:04 AM

 


Required Components List

CharacterLcdDisplay s Character LCD Module x 1
DS3231RTCModule s DS 3231 RTC Module x 1
breadboard s Breadboard x 1
Button Small 64 Switch x 1

 

Circuit Diagram

You can connect the DS3231 to any available pins. The DS3231 Kit board already have pull-up resistors on the I2C data and clock pins.

 

Procedure

Creating a new Project in the EE4450 workspace
  1. Launch the PSoC Creator.
  2. From the File menu, select Open ➤ Project/Workspacee to open the "EE4450" workspace.
  3. Add a new project by clicking File ➤ Add ➤ New Project… in the main menu.
  4. In the New Project window, select the default PSoC 5LP device, or select the spcific device you want to use.
  5. On the "Select project template" page, select Empty schematic.
  6. In Project Name, type the name of the project, for example "Lab08_I2C_DS3231". (Note: The project name cannot exceed 80 characters.)
  7. Click Finish.

PSoC Creator will create a new project in the EE4450 workspace, and set it as active project. Close all the pages in the document window. From the Workspace Explorer, click TopDesign.cysch to open the top-level schematic file.

 

Add/Configure Components

Add Components to the schematic canvas and configure them as appropriate.

 

Placing and Configuring an I2C Master Component

  1. Drag a I2C Master (UDB) component onto your design.
    In  PSoCCreator icon Catalog 1 Cypress Component Catalog, expand the " PSoCCreator icon Catalog 2 Communications ➤  PSoCCreator icon Catalog 2 I2C" folder and select  PSoCCreator icon Catalog 3 I2C Master (UDB).
    CypressComp I2C Master UDB s
  2. Delete the Clock_1 and RST_1 components, which are connected with I2C component.
  3. Rename the digital bidirectional pins: SDA_1 to SDA and SCL_1 to SCL.
  4. Drag-and-drop the Logic Low '0' component and connect it with reset pin on the I2C_1 component.
  5. Double click the I2C_1 component in the schematic to open the configuration window.
  6. Configure the I2C in this manner:
    Conf I2C

 

Placing and Configuring a Character LCD Component

  1. Drag a Character LCD component onto you design (  PSoCCreator icon Catalog 1 Component Catalog ➤  PSoCCreator icon Catalog 2 Display ➤  PSoCCreator icon Catalog 4 Character LCD)
    CypressComp CharLCD s
  2. Double click the LCD_1 component in the schematic to open the configuration window.
  3. Configure the LCD:
    Conf LCD

 

Placing and Configuring a Digital Input Pin Component

  1. Drag a Digital Input Pin component onto you design (  PSoCCreator icon Catalog 1 Component Catalog PSoCCreator icon Catalog 2 Ports and Pins ➤  PSoCCreator icon Catalog 3 Digital Input Pin)
  2. Double click the Pin_1 component in the schematic to open the configuration window.
  3. Configure the Pin:
    Conf InputPin

When complete the schematic looks similar to:

TopDesign DS3231

 Assign Pin
  1. In the Workspace Explorer, expand the Design Wide Resource and double-click the Pin item to open the Pin Editor (or you can double-click the Design Wide Resource to open the I2C_DS3231.cydwr file, the click Pins tab on the bottom to open the Pin Editor.)
  2. Assign the pins:
    Name Port
    \LCD:LCDPort[6:0]\
    nSW
    SCL
    SDA

Generate API Source Code File

From the main menu, click the Build ➤  icon GenerateApplication Generate Application to allow PSoC Creator to generate (or update) the various API files.

 

Reading DS3231 RTC (or DS1307 RTC) Datasheet

Find the following information from the datasheet:

  • Slave Address: 
  • Max I2C Interface Speed: KHz (or Kbps)
  • Supply Voltage: min=  V  max=  V
  • Control register number (address):   (DS3231 RTC only)
  • Status register number (address):   (DS3231 RTC only)
Write C code

In the Workspace Explorer, double-click the main.c file to open it in document window, and add the following C code.

Program the Device
  1. Connect all devices to the appropriate pins on the PSoC kit/board.
  2. Cypress CY8CKIT-059 Kit: Plug PSoC kit into your PC with a USB cable
    EagleSoC Board or other PSoC board: Connect the MiniProg3/4 programmer to SWD/JTAG port on the PSoC board, and plug it into your PC with a USB cable.
  3. Click Program CypressComp CharLCD, or click Debug from menu then select PSoCCreator Btn Program Program.
  4. If the Select Debug Target dialog displays, select your device, then click Connect and OK.
    Dialog SelectDebugTarget s

PSoC Creator will build your design, generate code, and program the device. When programming is complete, your PSoC kit/board will work as your design; press the Reset button if needed.

Expand the Design

Questions

  1. Why does the manufacturer disabled the oscillator before delivering the module to the customers?
  2. What type of drive mode does the both I2C pins (SCL and SDA) use in the TopDesign diagram? Explain it.
  3. In the I2C communication protocol, when we need to send a repeat start signal (Restart)? And Why?
  4. What is the slave address of the DS3231 RTC device?
  5. In I2C protocol, how does Slave inform Master that it is not ready to receive data?

 


Exercises

 

Submit your completed project report including your working code.