Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang berfung sebagai jam digital dengan nterface LCD. alat ini dilengkapi dengan RTC DS3231 modul sehingga akurasi waktu bisa terjaga biarpun mikrokontroller dalam keadaan OFF. alat ini menggunakan Nodemcu sebagai mikrokontrollernya. untuk lebih jelasnya berikut adalah program dan daftar komponennya.
a. Nodemcu ESP8266
b. Module RTC DS3231
c. LCD + I2C
d. Program Arduino IDE
#include "Wire.h"
#include <LiquidCrystal_I2C.h>
#define DS3231_I2C_ADDRESS 0x68
LiquidCrystal_I2C lcd(0x3F, 16, 2);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void setup() {
lcd.begin();
lcd.clear();
lcd.noCursor();
Wire.begin();
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
setDS3231time(0,47,11,6,23,11,18);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
lcd.setCursor(0,0);
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
lcd.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(dayOfMonth, DEC);
lcd.setCursor(0,2);
lcd.print(month, DEC);
lcd.print(" / ");
lcd.print(year, DEC);
lcd.setCursor(0,3);
//lcd.print(" Day of week: ");
switch(dayOfWeek){
case 1:
lcd.print("Sunday");
break;
case 2:
lcd.print("Monday");
break;
case 3:
lcd.print("Tuesday");
break;
case 4:
lcd.print("Wednesday");
break;
case 5:
lcd.print("Thursday");
break;
case 6:
lcd.print("Friday");
break;
case 7:
lcd.print("Saturday");
break;
}
}
void loop(){
displayTime();
delay(1000);
}
#include <LiquidCrystal_I2C.h>
#define DS3231_I2C_ADDRESS 0x68
LiquidCrystal_I2C lcd(0x3F, 16, 2);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
void setup() {
lcd.begin();
lcd.clear();
lcd.noCursor();
Wire.begin();
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
setDS3231time(0,47,11,6,23,11,18);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
lcd.setCursor(0,0);
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
lcd.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(dayOfMonth, DEC);
lcd.setCursor(0,2);
lcd.print(month, DEC);
lcd.print(" / ");
lcd.print(year, DEC);
lcd.setCursor(0,3);
//lcd.print(" Day of week: ");
switch(dayOfWeek){
case 1:
lcd.print("Sunday");
break;
case 2:
lcd.print("Monday");
break;
case 3:
lcd.print("Tuesday");
break;
case 4:
lcd.print("Wednesday");
break;
case 5:
lcd.print("Thursday");
break;
case 6:
lcd.print("Friday");
break;
case 7:
lcd.print("Saturday");
break;
}
}
void loop(){
displayTime();
delay(1000);
}
e. VIDEO HASILNYA
No comments:
Post a Comment