DS1307_Read_and_Setting (AnalogRead)













code:


#define DS1307_ID_Read 0xD1   //01000001 ; 001=slave address  ; Read=1
#define DS1307_ID_Write 0xD0   //01000001 ; 000=slave address  ; Write = 0
#define PCF8574_ID_Write 0x40
#define SDA   1                      // Serial data
#define SCL   2                        // Serial clock
#define A3    3                                     // analog input pin

int sensorValue = 0;        // value read from the pot
int sensorValue1 = 0;        // value read from the pot
byte buff10;
byte buff;
char outdata;
#define ATcommand 0x20                  //LCD command   EN=1 ; RS=0       0010  0000
#define ATdata 0x30                     //LCD data      EN=1 ; RS=1       0011  0000
//DS1307 Clock Buffer
byte second;                                
byte minute;
byte hour;
byte wkDay;
byte date;
byte month;
byte year;
byte ctrl;
void i2c_start(void)
{
  pinMode(SCL, OUTPUT);
  pinMode(SDA, OUTPUT);
  digitalWrite(SDA, HIGH);
  digitalWrite(SCL, HIGH);

  digitalWrite(SDA, LOW);
  digitalWrite(SCL, LOW);
}
void i2c_acknowledge(void)
{
  digitalWrite(SDA, LOW);
  digitalWrite(SCL, HIGH);
  digitalWrite(SCL, LOW);
}
void i2c_not_acknowledge(void)
{
  digitalWrite(SDA, HIGH);
  digitalWrite(SCL, HIGH);
  digitalWrite(SCL, LOW);
}
void i2c_stop(void)
{
  digitalWrite(SCL, HIGH);
  digitalWrite(SDA, HIGH);
}
void i2c_write(unsigned char output_data)
{
  int iBit=0;
  for(int iloop = 0 ; iloop < 8 ; iloop++)
  {
    iBit = ((output_data & 0x80) >> 7 ) ;
    digitalWrite(SDA,iBit);   // bit output
    digitalWrite(SCL, HIGH);
    digitalWrite(SCL, LOW);
    output_data = output_data << 1 ;
  }
}
byte i2c_read (void)
{
  byte buff_byte_input=0x00;
  for(byte icount=0 ; icount<8 ; icount++)
  {
    pinMode(SDA, INPUT);
    digitalWrite(SCL, HIGH);
    buff_byte_input = buff_byte_input << 1 ;
    buff_byte_input |= digitalRead(SDA);
    digitalWrite(SCL, LOW);
  }
  return (buff_byte_input);
}
void PCF8574_Write(unsigned char data)
{
  i2c_start();              //         |-[S]-|  ;Start Condition
  i2c_write(PCF8574_ID_Write);    //         |---Slave address (PCF8574)--[A2=0,A1=0,A0=0]---|   +    |-R/W-|
  i2c_acknowledge();        //         |-[A]-|  ;Active LOW
  i2c_write(data);          //         |-------------------data to port-----------------------|
  i2c_acknowledge();        //         |-[A]-|  ;Active LOW
  //i2c_stop();               //         |-[P]-|  ;Stop Condition
}
//---------------------LCD library ----------------------------
void Write_LCD_Byte(char command_data ,char value)
{
  int iMSB=0;
  int iLSB=0;
  iMSB = (value & 0xF0)>>4;
  iLSB = (value & 0x0F);
  PCF8574_Write(command_data|iMSB);//0xFF
  PCF8574_Write(0x00|iMSB);
  PCF8574_Write(command_data|iLSB);
  PCF8574_Write(0x00|iLSB);
}
void initial_LCD(void)
{
  delay(50);//ให้รออย่างน้อย 15ms
  PCF8574_Write(0x00);//ให้ขา E = 0 ให้ขา RS= 0 กำหนดเป็นคำสั่ง ให้ขา R/W = 0 เขียนข้อมูล
  delay(50);
  PCF8574_Write(0x23);      //1    0x20 + 0x03
  delayMicroseconds(1);
  PCF8574_Write(0x03);
  delay(5);                //wait 4ms
  PCF8574_Write(0x23);     //2
  delayMicroseconds(1);
  PCF8574_Write(0x03);
  delayMicroseconds(100);  //wait 100us
  PCF8574_Write(0x23);     //3
  delayMicroseconds(1);
  PCF8574_Write(0x03);
  PCF8574_Write(0x22);//Function Set (BIT5=1) [001DNF00 ],Sets buss size 4bit (D=0)ข้อมูลเฉพาะ D4-D7 จะถูกส่งออกไปที่บัส
  delayMicroseconds(1);
  PCF8574_Write(0x02);
  Write_LCD_Byte(ATcommand,0x28);// 00101000 : [001DNF00 ],Sets buss size 4bit (D=0),number of lines=2 (N=1),font size =5x7(F=0)
  Write_LCD_Byte(ATcommand,0x0C);//00001100 : [00001DUB] ,Display On (D=1),Cursor Off (U=0) ,blinking block cursor Off (B=0)
  Write_LCD_Byte(ATcommand,0x06);//00000110 : [000001MS] , Cursor direction right (M=1);no scroll (S=0)
  Write_LCD_Byte(ATcommand,0x01);//0x01 clear screen
}
//--------------------------end LCD library---------------------

void DS1307_Write(byte ibuff_write)
{
  i2c_start();
  i2c_write(DS1307_ID_Write);
  i2c_acknowledge();
  i2c_write(ibuff_write);
  i2c_acknowledge();
  //i2c_stop();
}
byte DS1307_Read(void)
{
  byte ibuff;
  i2c_start();
  i2c_write(DS1307_ID_Read);
  i2c_acknowledge();
  ibuff=i2c_read();
  //i2c_acknowledge();
  //i2c_stop();
  return(ibuff);
}
void GetClock(void)
{
  DS1307_Write(0x00);
  second = DS1307_Read();
  i2c_acknowledge();
  minute = DS1307_Read();
  i2c_acknowledge();
  hour   = DS1307_Read();
  i2c_acknowledge();
  wkDay  = DS1307_Read();
  i2c_acknowledge();
  date   = DS1307_Read();
  i2c_acknowledge();
  month  = DS1307_Read();
  i2c_acknowledge();
  year   = DS1307_Read();
  i2c_acknowledge();
  ctrl   = DS1307_Read();
  i2c_not_acknowledge();
  //i2c_stop();
}
void SetClock()
{
  i2c_start();
  i2c_write(DS1307_ID_Write);
  i2c_acknowledge();
  i2c_write(0x00);//word Address
  i2c_acknowledge();
  i2c_write(second);
  i2c_acknowledge();
  i2c_write(minute);
  i2c_acknowledge();
  i2c_write(hour);
  i2c_acknowledge();
  i2c_write(wkDay);
  i2c_acknowledge();
  i2c_write(date);
  i2c_acknowledge();
  i2c_write(month);
  i2c_acknowledge();
  i2c_write(year);
  i2c_acknowledge();
  i2c_write(ctrl);
  i2c_acknowledge();
  //i2c_stop();
}
void PrintDayName(byte Name)
{
  switch (Name)
  {
    case 1: Write_LCD_Byte(ATdata,'S');
            Write_LCD_Byte(ATdata,'u');
            Write_LCD_Byte(ATdata,'n');
            break;
    case 2: Write_LCD_Byte(ATdata,'M');
            Write_LCD_Byte(ATdata,'o');
            Write_LCD_Byte(ATdata,'n');
            break;
    case 3: Write_LCD_Byte(ATdata,'T');
            Write_LCD_Byte(ATdata,'u');
            Write_LCD_Byte(ATdata,'e');
            break;
    case 4: Write_LCD_Byte(ATdata,'W');
            Write_LCD_Byte(ATdata,'e');
            Write_LCD_Byte(ATdata,'d');
            break;
    case 5: Write_LCD_Byte(ATdata,'T');
            Write_LCD_Byte(ATdata,'h');
            Write_LCD_Byte(ATdata,'u');
            break;
    case 6: Write_LCD_Byte(ATdata,'F');
            Write_LCD_Byte(ATdata,'r');
            Write_LCD_Byte(ATdata,'i');
            break;
   case 7:  Write_LCD_Byte(ATdata,'S');
            Write_LCD_Byte(ATdata,'a');
            Write_LCD_Byte(ATdata,'t');
            break;
   default: Write_LCD_Byte(ATdata,'?');
            Write_LCD_Byte(ATdata,'?');
            Write_LCD_Byte(ATdata,'?');
 }
}
void PrintMonthName(byte Name)
{
  switch (Name)
  {
    case 1:  Write_LCD_Byte(ATdata,'J');
             Write_LCD_Byte(ATdata,'a');
             Write_LCD_Byte(ATdata,'n');
             break;
    case 2:  Write_LCD_Byte(ATdata,'F');
             Write_LCD_Byte(ATdata,'e');
             Write_LCD_Byte(ATdata,'b');
             break;
    case 3:  Write_LCD_Byte(ATdata,'M');
             Write_LCD_Byte(ATdata,'a');
             Write_LCD_Byte(ATdata,'r');
             break;
    case 4:  Write_LCD_Byte(ATdata,'A');
             Write_LCD_Byte(ATdata,'p');
             Write_LCD_Byte(ATdata,'r');
             break;
    case 5:  Write_LCD_Byte(ATdata,'M');
             Write_LCD_Byte(ATdata,'a');
             Write_LCD_Byte(ATdata,'y');
             break;
    case 6:  Write_LCD_Byte(ATdata,'J');
             Write_LCD_Byte(ATdata,'u');
             Write_LCD_Byte(ATdata,'n');
             break;
    case 7:  Write_LCD_Byte(ATdata,'J');
             Write_LCD_Byte(ATdata,'u');
             Write_LCD_Byte(ATdata,'y');
             break;
    case 8:  Write_LCD_Byte(ATdata,'A');
             Write_LCD_Byte(ATdata,'u');
             Write_LCD_Byte(ATdata,'g');
             break;
    case 9:  Write_LCD_Byte(ATdata,'S');
             Write_LCD_Byte(ATdata,'e');
             Write_LCD_Byte(ATdata,'p');
             break;
    case 10: Write_LCD_Byte(ATdata,'O');
             Write_LCD_Byte(ATdata,'c');
             Write_LCD_Byte(ATdata,'t');
             break;
    case 11: Write_LCD_Byte(ATdata,'N');
             Write_LCD_Byte(ATdata,'o');
             Write_LCD_Byte(ATdata,'v');
             break;
    case 12: Write_LCD_Byte(ATdata,'D');
             Write_LCD_Byte(ATdata,'e');
             Write_LCD_Byte(ATdata,'c');
             break;
    default: Write_LCD_Byte(ATdata,'?');
             Write_LCD_Byte(ATdata,'?');
             Write_LCD_Byte(ATdata,'?');
  }
}
byte SubSetTimeDateYear(byte positions , byte Time)
{
  Write_LCD_Byte(ATcommand,0x80|positions);// line 1 เริ่มตำแหน่ง 11
  Write_LCD_Byte(ATdata,' ');
  Write_LCD_Byte(ATdata,' ');
  delay(100);
  Write_LCD_Byte(ATcommand,0x80|positions);// line 1 เริ่มตำแหน่ง 11

  buff10=Time>>4;
  buff=Time&0x0f;
  if(buff==0x0A|buff==0x0B|buff==0x0C|buff==0x0D|buff==0x0E|buff==0x0F)
  {
    buff10++;
    buff=0x00;
    Time=(buff10<<4|buff);
  }
  buff=Time&0x0f;
  buff10=Time>>4;

  Write_LCD_Byte(ATdata,((buff10)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff+0x30);
  delay(100);
  return(Time);
}
void SubSetWkDate(byte positions , byte WkDay)
{
  Write_LCD_Byte(ATcommand,0x80|positions);// line 1 เริ่มตำแหน่ง 11
  Write_LCD_Byte(ATdata,' ');
  Write_LCD_Byte(ATdata,' ');
  Write_LCD_Byte(ATdata,' ');
  delay(100);
  Write_LCD_Byte(ATcommand,0x80|positions);// line 1 เริ่มตำแหน่ง 11
  PrintDayName(WkDay);
  delay(100);
}
void SubSetMonth(byte positions , byte MoNth)
{
  Write_LCD_Byte(ATcommand,0x80|positions);// line 1 เริ่มตำแหน่ง 11
  Write_LCD_Byte(ATdata,' ');
  Write_LCD_Byte(ATdata,' ');
  Write_LCD_Byte(ATdata,' ');
  delay(100);
  Write_LCD_Byte(ATcommand,0x80|positions);// line 1 เริ่มตำแหน่ง 11
  PrintMonthName(MoNth);
  delay(100);
}
void SetTimeDate (void)
{
  Write_LCD_Byte(ATcommand,0x80|0x00);// line 1 เริ่มตำแหน่ง 00
  Write_LCD_Byte(ATdata,'S');
  Write_LCD_Byte(ATdata,'e');
  Write_LCD_Byte(ATdata,'t');
  Write_LCD_Byte(ATdata,'H');
  Write_LCD_Byte(ATdata,'o');
  Write_LCD_Byte(ATdata,'u');
  Write_LCD_Byte(ATdata,'r');
  while(sensorValue>190&sensorValue<210)
  {
    hour=SubSetTimeDateYear(0x08,hour);
    sensorValue1=analogRead(A3);
    if(sensorValue1>300&sensorValue1<500)// press Buttom ROAD1 !!!
    {
      hour--;
      if(hour==0x00)
      {
        hour=0x59;
      }
    }
    if(sensorValue1>700&sensorValue1<900)// press Buttom ROAD2 !!!
    {
      hour++;
      if(hour>0x23)
      {
        hour=0;
      }
    }
    while(sensorValue1>190&sensorValue1<210)//exit function settime ,// press Buttom TIME !!!
    {
      Write_LCD_Byte(ATcommand,0x80|0x00);// line 1 เริ่มตำแหน่ง 00
      Write_LCD_Byte(ATdata,'S');
      Write_LCD_Byte(ATdata,'e');
      Write_LCD_Byte(ATdata,'t');
      Write_LCD_Byte(ATdata,'M');
      Write_LCD_Byte(ATdata,'i');
      Write_LCD_Byte(ATdata,'n');
      Write_LCD_Byte(ATdata,'.');
      while(sensorValue>190&sensorValue<210)
      {
        minute=SubSetTimeDateYear(0x0B,minute);
        sensorValue1=analogRead(A3);
        if(sensorValue1>300&sensorValue1<500)// press Buttom ROAD1 !!!
        {
          minute--;
          if(minute==0x00)
          {
            minute=0x59;
          }
        }
        if(sensorValue1>700&sensorValue1<900)// press Buttom ROAD2 !!!
          {
            minute++;
            if(minute>0x59)
            {
              minute=0;
            }
          }
          while(sensorValue1>190&sensorValue1<210)//exit function settime ,// press Buttom TIME !!!
          {
            Write_LCD_Byte(ATcommand,0x80|0x00);// line 1 เริ่มตำแหน่ง 00
            Write_LCD_Byte(ATdata,'S');
            Write_LCD_Byte(ATdata,'e');
            Write_LCD_Byte(ATdata,'t');
            Write_LCD_Byte(ATdata,'S');
            Write_LCD_Byte(ATdata,'e');
            Write_LCD_Byte(ATdata,'c');
            Write_LCD_Byte(ATdata,'.');
            while(sensorValue>190&sensorValue<210)
            {
              second=SubSetTimeDateYear(0x0E,second);
              sensorValue1=analogRead(A3);
              if(sensorValue1>300&sensorValue1<500)// press Buttom ROAD1 !!!
              {
                second--;
                if(second==0x00)
                {
                  second=0x59;
                }
              }
              if(sensorValue1>700&sensorValue1<900)// press Buttom ROAD2 !!!
              {
                second++;
                if(second>0x59)
                {
                  second=0;
                }
             }
             while(sensorValue1>100&sensorValue1<300)//exit function settime ,// press Buttom TIME !!!
             {
               Write_LCD_Byte(ATcommand,0x80|0x00);// line 1 เริ่มตำแหน่ง 00
               Write_LCD_Byte(ATdata,'S');
               Write_LCD_Byte(ATdata,'e');
               Write_LCD_Byte(ATdata,'t');
               Write_LCD_Byte(ATdata,'D');
               Write_LCD_Byte(ATdata,'a');
               Write_LCD_Byte(ATdata,'y');
               Write_LCD_Byte(ATdata,'.');  
               while(sensorValue>190&sensorValue<210)
               {
                 SubSetWkDate(0x41,wkDay);
                 sensorValue1=analogRead(A3);
                 if(sensorValue1>300&sensorValue1<500)// press Buttom ROAD1 !!!
                 {
                   wkDay--;
                   if(wkDay==0x01)
                   {
                     wkDay=0x07;
                   }
                 }
                 if(sensorValue1>700&sensorValue1<900)// press Buttom ROAD2 !!!
                 {
                   wkDay++;
                   if(wkDay>0x07)
                   {
                     wkDay=0x01;
                   }
                }
                while(sensorValue1>150&sensorValue1<250)//exit function settime ,// press Buttom TIME !!!
                {
                  Write_LCD_Byte(ATcommand,0x80|0x00);// line 1 เริ่มตำแหน่ง 00
                  Write_LCD_Byte(ATdata,'S');
                  Write_LCD_Byte(ATdata,'e');
                  Write_LCD_Byte(ATdata,'t');
                  Write_LCD_Byte(ATdata,'D');
                  Write_LCD_Byte(ATdata,'a');
                  Write_LCD_Byte(ATdata,'t');
                  Write_LCD_Byte(ATdata,'e');
                   while(sensorValue>190&sensorValue<210)
                   {
                     date=SubSetTimeDateYear(0x45,date);
                     sensorValue1=analogRead(A3);
                     if(sensorValue1>300&sensorValue1<500)// press Buttom ROAD1 !!!
                     {
                       date--;
                       if(date==0x01)
                       {
                         date=0x31;
                       }
                     }
                     if(sensorValue1>700&sensorValue1<900)// press Buttom ROAD2 !!!
                     {
                       date++;
                       if(date>0x31)
                       {
                         date=0x01;
                       }
                    }
                    while(sensorValue1>150&sensorValue1<250)//exit function settime ,// press Buttom TIME !!!
                    {
                      Write_LCD_Byte(ATcommand,0x80|0x00);// line 1 เริ่มตำแหน่ง 00
                      Write_LCD_Byte(ATdata,'S');
                      Write_LCD_Byte(ATdata,'e');
                      Write_LCD_Byte(ATdata,'t');
                      Write_LCD_Byte(ATdata,'M');
                      Write_LCD_Byte(ATdata,'o');
                      Write_LCD_Byte(ATdata,'n');
                      Write_LCD_Byte(ATdata,'t');
                       while(sensorValue>190&sensorValue<210)
                       {
                         SubSetMonth(0x48,month);
                         sensorValue1=analogRead(A3);
                         if(sensorValue1>300&sensorValue1<500)// press Buttom ROAD1 !!!
                         {
                           month--;
                           if(month==0x01)
                           {
                             month=0x12;
                           }
                         }
                         if(sensorValue1>700&sensorValue1<900)// press Buttom ROAD2 !!!
                         {
                           month++;
                           if(month>0x12)
                           {
                             month=0x01;
                           }
                        }
                        while(sensorValue1>150&sensorValue1<250)//exit function settime ,// press Buttom TIME !!!
                        {
                          Write_LCD_Byte(ATcommand,0x80|0x00);// line 1 เริ่มตำแหน่ง 00
                          Write_LCD_Byte(ATdata,'S');
                          Write_LCD_Byte(ATdata,'e');
                          Write_LCD_Byte(ATdata,'t');
                          Write_LCD_Byte(ATdata,'Y');
                          Write_LCD_Byte(ATdata,'e');
                          Write_LCD_Byte(ATdata,'a');
                          Write_LCD_Byte(ATdata,'r');
                           while(sensorValue>190&sensorValue<210)
                           {
                             year=SubSetTimeDateYear(0x4E,year);
                             sensorValue1=analogRead(A3);
                             if(sensorValue1>300&sensorValue1<500)// press Buttom ROAD1 !!!
                             {
                               year--;
                               if(year==0x01)
                               {
                                 year=0x99;
                               }
                             }
                             if(sensorValue1>700&sensorValue1<900)// press Buttom ROAD2 !!!
                             {
                               year++;
                               if(year>0x99)
                               {
                                 year=0x01;
                               }
                            }
                            while(sensorValue1>150&sensorValue1<250)//exit function settime ,// press Buttom TIME !!!
                            {
                              sensorValue=0;//get out and pess function,the end function
                              sensorValue1=analogRead(A3);//get out and pess function,the end function
                              SetClock();
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
void setup()
{
  initial_LCD();

}

void loop()
{  
  byte buff10;
  byte buff;
  GetClock();

  sensorValue=analogRead(A3);
  if(sensorValue>190&sensorValue<210)// press Buttom TIME !!!
  {
    SetTimeDate();
  }

  Write_LCD_Byte(ATcommand,0x80|0x00);// line 1 เริ่มตำแหน่ง 00
  Write_LCD_Byte(ATdata,'B'); //+34.5'C
  Write_LCD_Byte(ATdata,'r');
  Write_LCD_Byte(ATdata,'o');
  Write_LCD_Byte(ATdata,'g');
  Write_LCD_Byte(ATdata,'l');
  Write_LCD_Byte(ATdata,'i'); //0xdf='
  Write_LCD_Byte(ATdata,'e');

  Write_LCD_Byte(ATdata,0xFF);

  buff10=hour&0x0f;
  Write_LCD_Byte(ATdata,((hour>>4)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff10+0x30);

  Write_LCD_Byte(ATdata,':');

  buff10=minute&0x0f;
  Write_LCD_Byte(ATdata,((minute>>4)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff10+0x30);

  Write_LCD_Byte(ATdata,':');

  buff10=second&0x0f;
  Write_LCD_Byte(ATdata,((second>>4)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff10+0x30);

  Write_LCD_Byte(ATcommand,0x80|0x40);// line 2 เริ่มตำแหน่ง 00
  Write_LCD_Byte(ATdata,0xff);

  PrintDayName(wkDay);

  Write_LCD_Byte(ATdata,0xFF);

  buff10=date&0x0f;
  Write_LCD_Byte(ATdata,((date>>4)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff10+0x30);

  Write_LCD_Byte(ATdata,0xFF);//0xFF

  buff10=month&0x0f;
  PrintMonthName(((month>>4)*10)+buff10);

  Write_LCD_Byte(ATdata,0xFF);//0xff

  Write_LCD_Byte(ATdata,'2');
  Write_LCD_Byte(ATdata,'0');
  buff10=year&0x0f;
  Write_LCD_Byte(ATdata,((year>>4)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff10+0x30);
}