
#include "8051.h"
#include "serial.h"
#define SCLK P2_0
#define DATA_IO P2_1
#define RESET_ P2_2
#define YELLOW_LED P3_6
#define RED_LED P3_7
unsigned char serial_storage = 0x00;
unsigned char display;

void usec_wait(unsigned char);
void msec_wait(unsigned char);
void sec_wait(unsigned char);
void convert_and_send(unsigned char);
unsigned char receive_convert(void);


//************************************************************** 
//DELAY FUNCTIONS - USEC_WAIT ; MSEC_WAIT ; SEC_WAIT 
//************************************************************** 
void usec_wait(unsigned char k)
{
    while (k != 0) {
        k--;
    }
}

////////////////////////////////////////////////////// 
void msec_wait(unsigned char j)
{
    while (j != 0) {
        usec_wait(165);
        j--;
    }
}

////////////////////////////////////////////////////// 
void sec_wait(unsigned char sec_count)
{
    while (sec_count != 0) {
        msec_wait(250);
        msec_wait(250);
        msec_wait(250);
        msec_wait(250);
        sec_count--;
    }
}

////////////////////////////////////////////////////// 

//************************************************************** 
//SERIAL INTERRUPT FUNCTION 
//************************************************************** 

void serial_interrupt() interrupt 4
{
    if (TI)
        TI = 0;

    if (RI) {
        serial_storage = SBUF;
        usec_wait(1);
        RI = 0;
        RED_LED = 1;
    }
}


//******************************************************************* 
//FUNCTION THAT SENDS ADDRESS OR DATA_IO AFTER CONVERTING IT TO BITS 
//******************************************************************* 
void convert_and_send(unsigned char hex_data1)
{
    bit sending_bit;
    unsigned char comparing_byte = 0x01;
    unsigned char i;
    unsigned char storing_byte = 0x00;
    SBUF = hex_data1;
    usec_wait(5);

    for (i = 0; i < 8; i++) {
        storing_byte = (hex_data1 & comparing_byte);
        if (storing_byte == 0x00)
            sending_bit = 0;
        else
            sending_bit = 1;
        SCLK = 0;
        SCLK = 0;
        DATA_IO = sending_bit;  //Clock to DATA_IO delay is 250ns (max) 
        comparing_byte <<= 1;
        SCLK = 1;
    }
    serial_storage = 0x00;
    usec_wait(2);
}


//************************************************************************************** 
//FUNCTION THAT RECEIVES DATA AND THEN CONVERTS IT TO CHARACTER AND PASSES TO ITS CALLER 
//************************************************************************************** 
unsigned char receive_convert(void)
{

    bit receiving_bit;
    unsigned char comparing_byte = 0x00;
    unsigned char i = 0;
    unsigned char received_data = 0x00;

    for (i = 0; i < 8; i++) {
        SCLK = 1;
        SCLK = 1;               //For giving a DEALY 
        SCLK = 0;
        receiving_bit = DATA_IO;
        if (receiving_bit == 0)
            comparing_byte = 0x00;
        else
            comparing_byte = 0x80;      //comparing byte is 10000000 as the bit is to be placesd in the MSB position 
        received_data = received_data >> 1;
        received_data = received_data | comparing_byte;
    }
    return (received_data);
}


void main()
{

    initialize_serial();        //INITIALIZES THE SERIAL PORT 

    //FUNCTION THAT INITIALIZES THE TIMMER IC BY SETTING CH AND WP BIT 
    RED_LED = 0;                //BOTH LED's GLOW WHEN THE RTC IS BEING SET 
    YELLOW_LED = 0;
    RESET_ = 1;
    msec_wait(1);
    convert_and_send(0x8E);     //SETS THE WP BIT AS ZERO 
    msec_wait(1);
    convert_and_send(0x00);
    RESET_ = 0;
    msec_wait(1);
    RESET_ = 1;
    msec_wait(1);
    convert_and_send(0x80);     //SETS THE CH BIT AS ZERO 
    msec_wait(1);
    convert_and_send(0x00);
    RESET_ = 0;
    msec_wait(1);

    sec_wait(4);

    RESET_ = 1;                 //INITIALIZING the HT1380 
    msec_wait(1);
    convert_and_send(0xBE);     //Burst Mode Command 
    msec_wait(1);
    convert_and_send(0x00);     //Sec register, Including the CH=0 
    msec_wait(1);
    convert_and_send(0x00);     //min 
    msec_wait(1);
    convert_and_send(0x10);     //hour 
    msec_wait(1);
    convert_and_send(0x10);     //date 
    msec_wait(1);
    convert_and_send(0x05);     //month 
    msec_wait(1);
    convert_and_send(0x07);     //day 
    msec_wait(1);
    convert_and_send(0x08);     //year 
    msec_wait(1);
    RESET_ = 0;
    msec_wait(10);

    RESET_ = 1;
    msec_wait(1);
    convert_and_send(0x8E);     //SETS THE WP BIT AS ONE 
    msec_wait(1);
    convert_and_send(0x80);
    RESET_ = 0;

    RED_LED = 1;                //BOTH LED's TURN OFF WHEN THE RTC TIME HAS BEEN SET 
    YELLOW_LED = 1;

    while (1) {
        RED_LED = 0;            //This shows that only the while loop is running and system is waiting for command(REGISTER ADDRESS) 
        msec_wait(500);
        if (serial_storage != 0x00) {
            RED_LED = 1;        //This shows that the serial_storage has the character 
            RESET_ = 1;
            convert_and_send(serial_storage);   //sending the address of the register that we want to read from 
            msec_wait(1);
            display = receive_convert();        //putting the read DATA_IO into display 
            RESET_ = 0;
            SBUF = display;     //sending the read DATA_IO serially to the computer 
            msec_wait(2);
            display = 0x00;
        } else {
            RED_LED = 1;
            msec_wait(500);
        }
    }

}
