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
- DS3231 RTC Datasheet: Download or
DS1307 RTC Datasheet: Download - Lesson 07: Interfacing Character LCD Display Module with PSoC
- Inter-Integrated Circuit Bus (I2C Bus)
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 the 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. Addresses and data are transferred serially through an I2C bidirectional bus.
Project Requirements
Design a digital system, that can display the 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 presses the SW within two seconds after the message shows, the system will update the date and time using the default value.
- The system will display the date/time in a 12-hour format on the LCD screen, and update it every second.
2019/09/20 FRI
08:12:04 AM
Required Components List
![]() |
Character LCD Module | x 1 |
![]() |
DS 3231 RTC Module | x 1 |
![]() |
Breadboard | x 1 |
![]() |
Switch | x 1 |
Circuit/Schematic 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.
Figure 1: The Breadboard Connection for Character LCD and DS3231 RTC Modules
Table 1: Pin Configurations:
Device | Port.Pin | Signal Type | Drive Mode |
---|---|---|---|
Procedure
Creating a new Project in the EE4450 workspace
- Launch the PSoC Creator.
- From the File menu, select Open ➤ Project/Workspace to open the "EE4450" workspace.
- Add a new project by clicking File ➤ Add ➤ New Project… in the main menu.
- In the New Project window, select the default PSoC 5LP device, or select the specific device you want to use.
- On the "Select project template" page, select the Empty schematic.
- In Project Name, type the name of the project, for example, "Lab08_I2C_DS3231". (Note: The project name cannot exceed 80 characters.)
- Click Finish.
PSoC Creator will create a new project in the EE4450 workspace, and set it as an 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
- Drag an I2C Master (UDB) component onto your design.
InCypress Component Catalog, expand the "
Communications ➤
I2C" folder and select
I2C Master (UDB).
- Delete the Clock_1 and RST_1 components, which are connected with the I2C component.
- Rename the digital bidirectional pins: SDA_1 to SDA and SCL_1 to SCL.
- Drag-and-drop the Logic Low '0' component and connect it with the reset pin on the I2C_1 component.
- Double click the I2C_1 component in the schematic to open the configuration window.
- Configure the I2C in this manner:
Placing and Configuring a Character LCD Component
- Drag a Character LCD component onto your design (
Component Catalog ➤
Display ➤
Character LCD)
- Double-click the LCD_1 component in the schematic to open the configuration window.
- Configure the LCD:
Placing and Configuring a Digital Input Pin Component
- Drag a Digital Input Pin component onto you design (
Component Catalog ➤
Ports and Pins ➤
Digital Input Pin)
- Double click the Pin_1 component in the schematic to open the configuration window.
- Configure the Pin:
When complete the schematic looks similar to:
Assign Pin
- 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.)
- Follows Table 1 to assign I/O Pins for each component.
Generate API Source Code File
From the main menu, click the Build ➤ 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 the document window, and implement the I2C code here.
Program the Device
- Connect all devices to the appropriate pins on the PSoC kit/board.
- 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. - Click Program
, or click Debug from the menu then select
Program.
- If the Select Debug Target dialog displays, select your device, then click Connect and OK.
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.
Lab Experiment
Exp #1: Display Current Date/Time
Now, implement the code to display the date/time on the character LCD as the following figure.
Figure 2: Display Current Date/Time on the LCD Screen
Questions
- Why does the manufacturer disable the oscillator before delivering the module to the customers?
- What type of drive mode does both I2C pins (SCL and SDA) use in the TopDesign diagram? Explain it.
- In the I2C communication protocol, when we need to send a repeat start signal (Restart)? And Why?
- What is the slave address of the DS3231 RTC device?
- In I2C protocol, how does Slave inform Master that it is not ready to receive data?
Exercises