Lesson 33 – DS3231 clock module


Tutorial

1. Introduction

This lesson will mainly use DS3231 Clock Module. In the lesson, you can see the setting time regarded as the starting time to start accurate timing on the terminal.

2. Hardware Required

  • DS3231 Clock Module*1
  • Keyestudio RPI GPIO-PCF8591 Shield*1
  • USB Cable* 1
  • Raspberry Motherboard * 1
  • Dupont Wires* several

3. Connection Diagram

 

4. Programming

  • a. This lesson will use the bcm2835 library function, and we have provided you with the installation package. First put winSCP compressed files into the system, and then input tar-xzf bcm2835-1.50.tar.gz on the terminal to extract the file.
    b. On the terminal, input cd bcm2835-1.50 to enter inside the bcm2835-1.50 folder, then execute:
    ./configure
    make
    sudo make check
    sudo make install
    Installation finished
    c. Use winSCP to put the lesson32_ DS3231 into the lesson folder of raspberry system.
    d. In the terminal, input cd lesson and cd lesson32_ DS3231 to enter the folder.
    e. In the terminal, input gcc –Wall DS3231.c –o DS3231 –lbcm2835, to generate DS3231 executable file inside the folder.
    f. Wiring as the above diagram, then enter the lesson32_ DS3231 folder, execute sudo ./DS3231 at the terminal. Then, you can see the setting time regarded as the starting time to start accurate timing on the terminal.
    g. Ctrl + c can exit the processing program.Please refer to the figure below:

5. Sample Code

The first three lines below are –

  • #include <bcm2835.h>
  • #include <stdio.h>
  • #include <unistd.h>
 

#include 
#include 
#include 
//regaddr,seconds,minutes,hours,weekdays,days,months,yeas
char  buf[]={0x00,0x00,0x00,0x18,0x04,0x12,0x08,0x15};
char  *str[]  ={"SUN","Mon","Tues","Wed","Thur","Fri","Sat"};
void pcf8563SetTime()
{
    bcm2835_i2c_write(buf,8);
}

void pcf8563ReadTime()
{  
    buf[0] = 0x00; 
    bcm2835_i2c_write_read_rs(buf ,1, buf,7); 
}

int main(int argc, char **argv) 
{ 
    if (!bcm2835_init())return 1; 
    bcm2835_i2c_begin(); 
    bcm2835_i2c_setSlaveAddress(0x68); 
    bcm2835_i2c_set_baudrate(10000); 
    printf("start..........\n");

    pcf8563SetTime();
    while(1) 
    {  
        pcf8563ReadTime();
        buf[0] = buf[0]&0x7F; //sec
        buf[1] = buf[1]&0x7F; //min
        buf[2] = buf[2]&0x3F; //hour
        buf[3] = buf[3]&0x07; //week
        buf[4] = buf[4]&0x3F; //day
        buf[5] = buf[5]&0x1F; //mouth
        //year/month/day
        printf("20%02x/%02x/%02x  ",buf[6],buf[5],buf[4]);
        //hour:minute/second
        printf("%02x:%02x:%02x  ",buf[2],buf[1],buf[0]);
        //weekday
        printf("%s\n",str[(unsigned char)buf[3]-1]);
        bcm2835_delay(1000);
    }
 
    bcm2835_i2c_end();
    bcm2835_close();

    return 0;
}

 

Prerequisites

  1. This development track is based on the Rasbperry Pi and the Ultimate Starter Kit for the Raspberry Pi.
  2. You will need access to both the Raspberry Pi SBC and the electronics components part of the Ultimate Starter Kit for the Raspberry Pi kit to be able to work on these tutorials.
  3. If you haven’t purchased the Raspberry Pi 4 B yet please head over to our store and purchase one now. You can pick up the Ultimate Starter Kit for the Raspberry Pi from OztoyLib.
  4. Depending on where you live you might also be able to pick up the Raspberry Pi and Ultimate Starter Kit for the Raspberry Pi at your local electronics hobby store.

About the Ultimate Starter Kit for the Raspberry Pi

The Ultimate Starter Kit for the Raspberry Pi comes packed with ~37 different electronic bits (Sensors, LEDs, switches, LCD, servo, etc.) including tutorials to get started. You will learn how to connect up the ~37 different electronic bits (Sensors, LEDs, switches, LCD, servo, etc.), create circuits using the Raspberry Pi including learning to write code in C. As you progress through the different tutorials you will explore the different capabilities of the Raspberry Pi including the intricacies of integrating the Raspberry Pi with the different electronic bits i.e. sensors, LED’s, switches, servos, etc. included in the starter kit.

The Ultimate Starter Kit for the Raspberry Pi comes along with detailed tutorials including code samples. Ultimate Starter Kit for the Raspberry Pi walks you through the basics of using the Raspberry Pi in a hands-on way. You’ll learn the fundamentals of electronics, programming in C and gain hands on experience through working on the Raspberry Pi building creative projects. The kit includes a selection of the most common and useful electronic components with an ebook of 37 projects. Starting the basics of electronics, to more complex projects, the kit will get you interacting with the physical world using sensor and actuators. Along with the kit you get access to detailed tutorials and wiring diagrams.


About the Raspberry Pi

The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote the teaching of basic computer science in schools and in developing countries. It is a capable little computer which can be used in electronics projects, and for many of the things that your desktop PC does, like spreadsheets, word processing, browsing the internet, and playing games. The original model became far more popular than anticipated, selling outside its target market for uses such as robotics.

The Raspberry Pi does not include peripherals (such as keyboards, mice and cases). However, some accessories have been included in several official and unofficial bundles. According to the Raspberry Pi Foundation, over 5 million Raspberry Pis were sold by February 2015, making it the best-selling British computer. By November 2016 they had sold 11 million units, and 12.5m by March 2017, making it the third best-selling “general purpose computer”. In July 2017, sales reached nearly 15 million.In March 2018, sales reached 19 million. Most Pis are made in a Sony factory in Pencoed, Wales; some are made in China or Japan.

You can read more about the Raspberry Pi here – RaspberryPi.org.

Questions