Use MSP430 to connect RTC module (DS2321) to make a digital clock
[Copy link]
This post was last edited by Hot Ximixiu on 2020-9-8 20:11
Use MSP430 to connect RTC module DS3231 to make a digital clock, and then display the time and date on the 1602 LCD. MSP-EXP430G2 is a development tool provided by Texas Instruments, also known as LaunchPad, for learning and practicing how to use its microcontrollers. This board belongs to the MSP430 product line, and we can program all MSP430 series microcontrollers.
Required Materials
● MSP430 development board
● DS3231 RTC module
● Potentiometer 10k
● LCD module 1602
● Connecting wire
● Breadboard
What is RTC?
RTC is a Real Time Clock module. It is used to maintain the date and time in most electronic projects. This module has its own coin cell battery power supply, which is used to maintain the date and time even if the main power is removed or the MCU is hardware reset. So once we set the date and time in this module, it will always remain the same. There are many types of RTC, such as DS1307, DS3231, etc.
Connecting DS3231 RTC with MSP430
The circuit diagram of the digital clock based on the MSP430 microcontroller is as follows. As mentioned earlier, the DS3231 communication method uses I2C, which contains a serial clock (SCL) and a serial data (SDA) pin, which must be connected to the I2C pin on pin 9 (pin 2.1, SCL) and pin 10 (pin 2.2, SDA) of our MSP430.
MSP430 is able to provide 3.3V Vcc but we need 5V to connect LCD and RTC modules. So, we will have a jumper at USB cable connector named TP1. You can get 5V from there.
The circuit diagram of connecting the RTC module using MSP430 is as follows:
Programming MSP430 for RTC module
Here we use Energia IDE for programming. It is same as Arduino IDE and easy to use. If you are not familiar with MSP430 and energia IDE, then refer to the link: Getting Started Guide for MSP430G2 Microcontroller Development using Energia IDE - Blinking LED. In order to connect the RTC module, we need the library for this board. Download the RTC library from this link and then install it.
We also need the Wire (for I2C communication) and liquidcrystal libraries, both of which come pre-installed in the Energia IDE.
The complete code of this MSP430 digital clock is given at the end of this article. The code is simple and easy to understand. Here we briefly introduce several parts of it.
First, we have to include the necessary libraries.
The following library is used for I2C communication between RTC module and MSP430. The SDA and SCK pins are already defined in this library, so we don't have to declare these pins separately.
#include <Wire.h>
Copy code
Then, we include the RTClib.h library for the RTC clock and LiquidCrystal.h for the LCD functionality.
#include "RTClib.h"
#include <LiquidCrystal.h>
Copy code
After that, we have to create an instance to initialize our RTC module.
RTC_DS3231 rtc;
Copy code
Then make an array of size 7 and store the names of all 7 days.
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Copy code
Here is the pin declaration of the MSP430 used for the LCD display: (RS (P2.0), EN (P1.4), D4 (P1.5), D5 (P2.3), D6 (P2.4), D7 (P2.5))
LiquidCrystal lcd(8,6,7,11,12,13);
Copy code
In void setup(), we initialize the interface between the LCD screen and the RTC and specify the size (width and height) of the display. begin() needs to be called before any other library commands.
void setup () {
lcd.begin(16, 2);
lcd.setCursor(3,0);
lcd.print("RTC Clock");
delay(3000);
lcd.clear();
rtc.begin();
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
复制代码
Note: In the above function, the commented line code is important. If the time and date are not set, uncomment the line and upload the program. This function stores the computer's time at compile time, so make sure your computer's time is correct.
Now, the time shown on the display is correct but there is a problem, every time you restart/reset the microcontroller, the LCD will show the time you uploaded the code. This is because the rtc.adjust() function has stored the computer's time, so when you reset it starts from that time.
To fix this, first upload the program with the rtc.adjust() function and then uncomment it. Then immediately comment the same line and upload the program again. Now your date and time are set and will not be affected by resetting the microcontroller.
In the loop function, we get the date and time from the RTC module and store it in the predefined now variable and display it on the LCD using the lcd.print() function.
void loop () {
DateTime now = rtc.now();
lcd.clear();
lcd.setCursor(3,0);
lcd.print(now.day(), DEC);
lcd.print("/");….
…….
复制代码
Download the code to the Arduino development board and the running effect is as follows:
Code
The complete code used in this article is as follows:
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
LiquidCrystal lcd(8 ,6, 7,11,12,13);
void setup () {
lcd.begin(16, 2);
lcd.setCursor(3,0);
lcd.print("RTC Clock");
delay(3000);
lcd.clear();
rtc.begin();
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop () {
DateTime now = rtc.now();
lcd.clear();
lcd.setCursor(3,0);
Serial.print(now.year(), DEC);
Serial.print('/');
lcd.print(now.day(), DEC);
lcd.print("/");
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
lcd.setCursor(1,5);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(",");
lcd.print(now.hour(), DEC);
lcd.print(":");
lcd.print(now.minute(), DEC);
lcd.print(":");
lcd.print(now.second(), DEC);
delay(1000);
}
复制代码
|