#include"lcd_disp.h"
volatile char *dirb = (volatile char *)0x24;
volatile char *outb = (volatile char *)0x25;
volatile char *inb = 0x23;
void setup() {
// put your setup code here, to run once:
*dirb = 0x03; // Setting up Port B as output
// *dirb = 0x00; // Setting up Port B as input
start_over:
volatile char l, n, com_bit;
volatile char i=0;
volatile char array1[] = {0xD0, 0x00, 0xA5, 0x30, 0x17, 0x06, 0x24, 0x06, 0x22, 0x03};
l = (sizeof(array1))/ (sizeof(char));
/* START CONDITION */
*dirb = 0x03;
*outb = 0x03;
*outb = 0x02;
*outb = 0x00;
/* Setting up RTC Register Values */
while (l)
{
*dirb = 0x03;
for (n=0; n<8; n++)
{
com_bit = (((array1[i]) & (1 << (7-n))) ? 1:0);
*outb = 0x00;
*outb = *outb | ((com_bit) << 0); // Each bit of array elements is serially communicated
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
*outb = (*outb) & (0xFD); // SCL is set to LOW
delayMicroseconds(100);
}
*outb = 0x00;
*dirb = 0x02;
*outb = (*outb) | (1<<1); // SCL is set to HIGH
delayMicroseconds(100);
if((*inb & 0x01) != 0)
{
goto start_over;
}
*outb = (*outb) & (0xFD); // SCL is set to LOW
delayMicroseconds(100);
i++;
l--;
}
/* STOP CONDITION */
*dirb = 0x03;
*outb = 0x02;
*outb = 0x03;
/* Retrieving DATA from RTC Registers */
start_over1:
volatile char l1, n1, com_bit1;
volatile char i1=0;
volatile char array2[] = {0xD0, 0x00};
l1 = (sizeof(array2))/ (sizeof(char));
/* START CONDITION */
*dirb = 0x03;
*outb = 0x03;
*outb = 0x02;
*outb = 0x00;
while(l1)
{
*dirb = 0x03;
for(n1=0; n1<8; n1++)
{
com_bit1 = (((array2[i1]) & (1 << (7-n1))) ? 1:0);
*outb = 0x00;
*outb = *outb | ((com_bit1) << 0); // Each bit of array elements is serially communicated
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
}
*outb = 0x00;
*dirb = 0x02;
*outb = (*outb) | (1 << 1);
delayMicroseconds(100);
if((*inb & 0x01) != 0)
{
goto start_over1;
}
*outb = 0x00;
delayMicroseconds(100);
i1++;
l1--;
}
start_over2:
volatile char l2, n2, n3, com_bit2, com_bit3;
volatile char rep_start = 0xD1;
/* START CONDITION */
*dirb = 0x03;
*outb = 0x03;
*outb = 0x02;
*outb = 0x00;
for(n2=0; n2<8; n2++)
{
com_bit2 = (((rep_start) & (1 << (7-n2))) ? 1:0);
*outb = 0x00;
*outb = *outb | ((com_bit2) << 0); // Each bit of array elements is serially communicated
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
}
*outb = 0x00;
*dirb = 0x02;
*outb = (*outb) | (1 << 1);
delayMicroseconds(100);
if((*inb & 0x01) != 0)
{
goto start_over2;
}
*outb = (*outb) & (0xFD);
delayMicroseconds(100);
volatile char ret_data[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//memset(ret_data, '0', sizeof(ret_data));
l2 = (sizeof(ret_data))/ (sizeof(char));
while(l2-1)
{
*outb = 0x00;
*dirb = 0x02;
for(n3=0; n3<8; n3++)
{
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
com_bit3 = (*inb) & (0x01);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
ret_data[8-l2] = ret_data[8-l2] | (com_bit3 << (7-n3));
}
*outb = 0x00;
*dirb = 0x03;
*outb = (*outb) | (0<<0); // Sending ACK (which is 0) from Arduino to RTC
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
l2--;
}
*outb = 0x00;
*dirb = 0x02;
for(n3=0; n3<8; n3++)
{
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
com_bit3 = (*inb) & (0x01);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
ret_data[8-l2] = ret_data[8-l2] | (com_bit3 << (7-n3));
}
*outb = 0x00;
*dirb = 0x03;
*outb = (*outb) | (1<<0); // Sending NACK (which is 1) from Arduino to RTC
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
// STOP condition
*outb = (*outb) & (0xFE); // SDA LOW
*outb = (*outb) | (1<<1); // Making SCL HIGH
*outb = 0x03; // Making SDA HIGH
/*
Serial.begin(9600);
for(int i=0; i<7; i++){
Serial.println((unsigned char)ret_data[i], HEX);
}
*/
volatile char new_sec_reg = (ret_data[0]) & (0x7F);
volatile char new_con_reg = (ret_data[7]) ^ (0x93);
/* Writing updated values into RTC registers at address locations 0x00 and 0x07 */
start_over4:
volatile char l4, n4, com_bit4;
volatile char i4=0;
volatile char array4[] = {0xD0, 0x00, new_sec_reg};
l4 = (sizeof(array4))/ (sizeof(char));
/* START CONDITION */
*dirb = 0x03;
*outb = 0x03;
*outb = 0x02;
*outb = 0x00;
/* Writing to RTC register at 0x00 */
while (l4)
{
*dirb = 0x03;
for (n4=0; n4<8; n4++)
{
com_bit4 = (((array4[i4]) & (1 << (7-n4))) ? 1:0);
*outb = 0x00;
*outb = *outb | ((com_bit4) << 0); // Each bit of array elements is serially communicated
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
*outb = (*outb) & (0xFD); // SCL is set to LOW
delayMicroseconds(100);
}
*outb = 0x00;
*dirb = 0x02;
*outb = (*outb) | (1<<1); // SCL is set to HIGH
delayMicroseconds(100);
if((*inb & 0x01) != 0)
{
goto start_over4;
}
*outb = (*outb) & (0xFD); // SCL is set to LOW
delayMicroseconds(100);
i4++;
l4--;
}
/* STOP CONDITION */
*dirb = 0x03;
*outb = 0x02;
*outb = 0x03;
start_over5:
volatile char l5, n5, com_bit5;
volatile char i5=0;
volatile char array5[] = {0xD0, 0x07, new_con_reg};
l5 = (sizeof(array5))/ (sizeof(char));
/* START CONDITION */
*dirb = 0x03;
*outb = 0x03;
*outb = 0x02;
*outb = 0x00;
/* Writing to RTC register at 0x07 */
while (l5)
{
*dirb = 0x03;
for (n5=0; n5<8; n5++)
{
com_bit5 = (((array5[i5]) & (1 << (7-n5))) ? 1:0);
*outb = 0x00;
*outb = *outb | ((com_bit5) << 0); // Each bit of array elements is serially communicated
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
*outb = (*outb) & (0xFD); // SCL is set to LOW
delayMicroseconds(100);
}
*outb = 0x00;
*dirb = 0x02;
*outb = (*outb) | (1<<1); // SCL is set to HIGH
delayMicroseconds(100);
if((*inb & 0x01) != 0)
{
goto start_over5;
}
*outb = (*outb) & (0xFD); // SCL is set to LOW
delayMicroseconds(100);
i5++;
l5--;
}
/* STOP CONDITION */
*dirb = 0x03;
*outb = 0x02;
*outb = 0x03;
}
void loop(){
/* Retrieving data from RTC registers again */
delay(1000);
start_over6:
volatile char l6, n6, com_bit6;
volatile char i6=0;
volatile char array6[] = {0xD0, 0x00};
l6 = (sizeof(array6))/ (sizeof(char));
/* START CONDITION */
*dirb = 0x03;
*outb = 0x03;
*outb = 0x02;
*outb = 0x00;
while(l6)
{
*dirb = 0x03;
for(n6=0; n6<8; n6++)
{
com_bit6 = (((array6[i6]) & (1 << (7-n6))) ? 1:0);
*outb = 0x00;
*outb = *outb | ((com_bit6) << 0); // Each bit of array elements is serially communicated
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
}
*outb = 0x00;
*dirb = 0x02;
*outb = (*outb) | (1 << 1);
delayMicroseconds(100);
if((*inb & 0x01) != 0)
{
goto start_over6;
}
*outb = 0x00;
delayMicroseconds(100);
i6++;
l6--;
}
start_over7:
volatile char l7, n7, n8, com_bit7, com_bit8;
volatile char rep_start1 = 0xD1;
/* START CONDITION */
*dirb = 0x03;
*outb = 0x03;
*outb = 0x02;
*outb = 0x00;
for(n7=0; n7<8; n7++)
{
com_bit7 = (((rep_start1) & (1 << (7-n7))) ? 1:0);
*outb = 0x00;
*outb = *outb | ((com_bit7) << 0); // Each bit of array elements is serially communicated
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
}
*outb = 0x00;
*dirb = 0x02;
*outb = (*outb) | (1 << 1);
delayMicroseconds(100);
if((*inb & 0x01) != 0)
{
goto start_over7;
}
*outb = (*outb) & (0xFD);
delayMicroseconds(100);
volatile char ret_data1[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//memset(ret_data, '0', sizeof(ret_data));
l7 = (sizeof(ret_data1))/ (sizeof(char));
while(l7-1)
{
*outb = 0x00;
*dirb = 0x02;
for(n8=0; n8<8; n8++)
{
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
com_bit8 = (*inb) & (0x01);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
ret_data1[8-l7] = ret_data1[8-l7] | (com_bit8 << (7-n8));
}
*outb = 0x00;
*dirb = 0x03;
*outb = (*outb) | (0<<0); // Sending ACK (which is 0) from Arduino to RTC
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
l7--;
}
*outb = 0x00;
*dirb = 0x02;
for(n8=0; n8<8; n8++)
{
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
com_bit8 = (*inb) & (0x01);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
ret_data1[8-l7] = ret_data1[8-l7] | (com_bit8 << (7-n8));
}
*outb = 0x00;
*dirb = 0x03;
*outb = (*outb) | (1<<0); // Sending NACK (which is 1) from Arduino to RTC
*outb = (*outb) | (1<<1); // SCL is SET to HIGH
delayMicroseconds(100);
*outb = (*outb) & (0xFD); // SCL is SET to LOW
delayMicroseconds(100);
// STOP condition
*outb = (*outb) & (0xFE); // SDA LOW
*outb = (*outb) | (1<<1); // Making SCL HIGH
*outb = 0x03; // Making SDA HIGH
/* Displaying Time, Day, Date, Month, and Year on LCD */
init_port();
init_lcd();
outdata(0x83);
lcd_control_write();
// Displaying Hours (24 HR format)
volatile char data5 = (((unsigned char)ret_data1[2] & 0x70) >> 4) + '0';
write_data(data5);
volatile char data6 = (ret_data1[2] & 0x0F) + '0';
write_data(data6);
write_data(':');
// Displaying Minutes
volatile char data3 = (((unsigned char)ret_data1[1] & 0x70) >> 4) + '0';
write_data(data3);
volatile char data4 = (ret_data1[1] & 0x0F) + '0';
write_data(data4);
write_data(':');
// Displaying Seconds
volatile char data1 = (((unsigned char)ret_data1[0] & 0x70) >> 4) + '0';
//volatile char data1 = (((unsigned char)ret_data[0] & 0x70) >> 4) | (0x03 << 4);
write_data(data1);
//volatile char data2 = (ret_data[0] & 0x0F) | (0x03 << 4);
volatile char data2 = (ret_data1[0] & 0x0F) + '0';
write_data(data2);
outdata(0xC0);
lcd_control_write();
// Displaying Month
volatile char data7 = (((unsigned char)ret_data1[5] & 0x70) >> 4) + '0';
write_data(data7);
volatile char data8 = (ret_data1[5] & 0x0F) + '0';
write_data(data8);
write_data('/');
// Displaying Date
volatile char data9 = (((unsigned char)ret_data1[4] & 0x70) >> 4) + '0';
write_data(data9);
volatile char data10 = (ret_data1[4] & 0x0F) + '0';
write_data(data10);
write_data('/');
// Displaying Year
write_string("20");
volatile char data11 = (((unsigned char)ret_data1[6] & 0x70) >> 4) + '0';
write_data(data11);
volatile char data12 = (ret_data1[6] & 0x0F) + '0';
write_data(data12);
outdata(0xCC);
lcd_control_write();
volatile char data13 = (ret_data1[3] & 0x0F) + '0';
if(data13 == '1')
write_string("SUN");
else if(data13 == '2')
write_string("MON");
else if(data13 == '3')
write_string("TUE");
else if(data13 == '4')
write_string("WED");
else if(data13 == '5')
write_string("THU");
else if(data13 == '6')
write_string("FRI");
else
write_string("SAT");
}