Code: Select all
#include <SPI.h>
const int cs=10; //chip select
#include "ST7565.h"
// pin 9 - Serial data out (SID)
// pin 8 - Serial clock out (SCLK)
// pin 7 - Data/Command select (RS or A0)
// pin 6 - LCD reset (RST)
// pin 5 - LCD chip select (CS)
ST7565 glcd(A0, A1, A2, A3, A4);
//#define LOGO16_GLCD_HEIGHT 16
//#define LOGO16_GLCD_WIDTH 16
//declare array to be used to set time and date
int TimeDateArray[6]={0,0,0,0,0,0};
void setup() {
Serial.begin(9600);
RTC_init();
//day(1-31), month(1-12), year(0-99), hour(0-23), minute(0-59), second(0-59)
SetTimeDate(8,6,11,20,41,00);
//remove above after keypad successfully sets clock
// initialize lcd and set the contrast to 0x18
glcd.begin(0x18);
}
void loop() {
//read time and date and convert to printable character array
String timedate_string = ReadTimeDate(); //read in string of time and date
char timedate_char[15]; //note: exact size is not important, it will fill as necessary
timedate_string.toCharArray(timedate_char,15); //this converts string to character array
//below is display shown every cycle
glcd.clear();
glcd.drawstring(0, 0, timedate_char);
//glcd.fillrect(0, 0, 127, 7, 1);
glcd.display();
glcd.clear();
delay(2000);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//above ends the void loop function
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//=====================================
//function to initialize real time clock chip:
int RTC_init(){
pinMode(cs,OUTPUT); // chip select
// start the SPI library:
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3); //mode 1 did not work, mode 3 did
//set control register
digitalWrite(cs, LOW);
SPI.transfer(0x8E);
SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
digitalWrite(cs, HIGH);
delay(10);
}
//=====================================
//function used to set time and date of RTC:
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
int TimeDate [7]={s,mi,h,0,d,mo,y};
for(int i=0; i<=6;i++){
if(i==3)
i++;
int b= TimeDate[i]/10;
int a= TimeDate[i]-b*10;
if(i==2){
if (b==2)
b=B00000010;
else if (b==1)
b=B00000001;
}
TimeDate[i]= a+(b<<4);
digitalWrite(cs, LOW);
SPI.transfer(i+0x80);
SPI.transfer(TimeDate[i]);
digitalWrite(cs, HIGH);
}
}
//=====================================
//function used to read time and date from RTC:
String ReadTimeDate(){
String temp;
int TimeDate [7]; //second,minute,hour,null,day,month,year
for(int i=0; i<=6;i++){
if(i==3)
i++;
digitalWrite(cs, LOW);
SPI.transfer(i+0x00);
unsigned int n = SPI.transfer(0x00);
digitalWrite(cs, HIGH);
int a=n & B00001111;
if(i==2){
int b=(n & B00110000)>>4; //24 hour mode
if(b==B00000010)
b=20;
else if(b==B00000001)
b=10;
TimeDate[i]=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate[i]=a+b*10;
}
else{
int b=(n & B01110000)>>4;
TimeDate[i]=a+b*10;
}
}
temp.concat(TimeDate[5]);
temp.concat("/") ;
temp.concat(TimeDate[4]);
temp.concat("/") ;
temp.concat(TimeDate[6]);
temp.concat(" ") ;
if(TimeDate[2]>12){
TimeDate[2]=TimeDate[2]-12;
}//removes military time
temp.concat(TimeDate[2]);
temp.concat(":") ;
if(TimeDate[1] < 10){
temp.concat("0");
} //loop adds zero if minutes is single digit
temp.concat(TimeDate[1]);
//temp.concat(":") ;
//temp.concat(TimeDate[0]);
//above eliminates seconds
return(temp);
}