PSoC Lab 08: Build a Digital Clock System using DS3231 I2C (RTC)
- 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)
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.
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.
Required Components List
|Character LCD Module||x 1|
|DS 3231 RTC Module||x 1|
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.
Creating a new Project in the EE4450 workspace
- Launch the PSoC Creator.
- From the File menu, select Open ➤ Project/Workspacee 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 spcific device you want to use.
- On the "Select project template" page, select 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 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 Components to the schematic canvas and configure them as appropriate.
Placing and Configuring an I2C Master Component
- Drag a I2C Master (UDB) component onto your design.
In Cypress Component Catalog, expand the " Communications ➤ I2C" folder and select I2C Master (UDB).
- Delete the Clock_1 and RST_1 components, which are connected with 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 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 you 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:
- 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.)
- Assign the pins:
Name Port \LCD:LCDPort[6:0]\ nSW SCL SDA
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 document window, and add the following C code.
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 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.
Expand the Design
- Why does the manufacturer disabled the oscillator before delivering the module to the customers?
- What type of drive mode does the 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?
Submit your completed project report including your working code.