Showing posts with label plc and arduino. Show all posts
Showing posts with label plc and arduino. Show all posts

Tuesday, 7 October 2014

PLC Real Time Clock Using Arduino programming at Sofcon


PLC real time clock using arduino and real time clock module. for real time module use DS3231 RTC Board. The way it works is: Arduino reads data from the DS3231, this data is sent directly to the PLC memory. and for setting the date and time via the DS3124 and computer by using visual basic net (VB.Net)

PLC real time clock process:
1. Read date and time from DS3231 RTC Board with Arduino via I2C communication
2. Send data to PLC with Arduino via RS232 communication and PLC Protocol
3. Receive data in PLC data memory
Flow process of the PLC Real TIme Clock:

 Arduino Source Code 

#include <Wire.h>;
const int DS1307 = 0x68;
byte second = 0;
byte minute = 0;
byte hourofday = 0;
byte dayofweek = 0;
byte date = 0;
byte month = 0;
byte year = 0;
byte set[7];
const char delimiter = ',';
String  message;
byte i;
int y;

void setup() {
  Wire.begin();
  Serial.begin(9600,SERIAL_8E1);//9600,8,Even,1
  Serial.setTimeout(800);
}

void loop() {
  sendTime();  
  message = Serial.readString();  //= "year,month,date,week,hour,minute,second";
  incomeTime(); 
}
byte decToBcd(byte val) {
  return ((val/10*16) + (val%10));
}
byte bcdToDec(byte val) {
  return ((val/16*10) + (val%16));
}

void incomeTime() { 
  i=0;
  y=-1;  
  do
  {
      y = message.indexOf(delimiter);
      if(y != -1)
      {
          if(i<=5)set[i]=message.substring(0,y).toInt();
          message = message.substring(y+1, message.length());
          i++;
      }
      else
      {
         if(message.length() > 0 && i==6)
           set[i]=message.toInt();         
      }
   }
   while(y >=0);
   
   if (i==6){
     if(set[0]>=0 && set[0]<=99){
       if(set[1]>=1 && set[1]<=12){
         if(set[2]>=1 && set[2]<=31){
           if(set[3]>=1 && set[3]<=7){
             if(set[4]>=0 && set[4]<=23){
               if(set[5]>=0 && set[5]<=59){
                 if(set[6]>=0 && set[6]<=59){
                   setTime();
                 }
               }
             }
           }
         }
       }
     }
   }

}
void setTime() { 
  year =set[0]; 
  month =set[1]; 
  date =set[2]; 
  dayofweek =set[3]; 
  hourofday =set[4]; 
  minute =set[5]; 
  second = set[6];
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hourofday));
  Wire.write(decToBcd(dayofweek));
  Wire.write(decToBcd(date));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.write(byte(0));
  Wire.endTransmission();
}

void sendTime() {
  readTime();
  int VB0 = second;// VB0=SECOND : 0 to 59
  int VB1 = minute;//VB1=MINUTE : 0 to 59
  int VB2 = hourofday;//VB2=HOUR OF DAY : 0 to 23
  int VB3 = date;//VB3= DATE : 1 to 31
  int VB4 = month;//VB4= MONTH : 1 to 12
  int VB5 = dayofweek;//VB5= DAY OF WEEK : 1=SUNDAY, 2=MONDAY, 3=TUESDAY, 4=WEDNESDAY, 5=THURSDAY, 6=FRIDAY, 7=SATURDAY
  int VB6 = year;//VW6= YEAR 00 to 99

  int VWstart = 0; //Start from VW0
  int VWcount = 4; 

  byte str_write[45];
  long Temp_FCS=0;
  int i;
                    
  str_write[1] = (byte)((VWcount * 2) + 31);
  str_write[2] = (byte)str_write[1];
  str_write[24] = (byte)(VWcount * 2);
  str_write[16] = (byte)(str_write[24] + 4);
  str_write[33] = (byte)((VWcount * 16) / 256);
  str_write[34] = (byte)((VWcount * 16) % 256);

  str_write[29] = (byte)((VWstart * 8) / 256);
  str_write[30] = (byte)((VWstart * 8) % 256);

  str_write[0] = (byte)0x68;//H68
  str_write[3] = (byte)0x68;//H68
  str_write[4] = (byte)0x02;//H2
  str_write[5] = (byte)0x00;//H0
  str_write[6] = (byte)0x7C;//H7C
  str_write[7] = (byte)0x32;//H32
  str_write[8] = (byte)0x01;//H1
  str_write[9] = (byte)0x00;//H0
  str_write[10] = (byte)0x0;//H0
  str_write[11] = (byte)0x43;//H43
  str_write[12] = (byte)0x01;//H1
  str_write[13] = (byte)0x00;//H0
  str_write[14] = (byte)0x0E;//HE
  str_write[15] = (byte)0x00;//H0

  str_write[17] = (byte)0x05;//H5
  str_write[18] = (byte)0x01;//H1
  str_write[19] = (byte)0x12;//H12
  str_write[20] = (byte)0x0A;//HA
  str_write[21] = (byte)0x10;//H10
  str_write[22] = (byte)0x02;//H2
  str_write[23] = (byte)0x00;//H0

  str_write[25] = (byte)0x00;//H0
  str_write[26] = (byte)0x01;//H1  
  str_write[27] = (byte)0x84;//H84  
  str_write[28] = (byte)0x00;//H0

  str_write[31] = (byte)0x00;//H0
  str_write[32] = (byte)0x04;//H4


  str_write[35] = (byte)VB0;
  str_write[36] = (byte)VB1;

  str_write[37] = (byte)VB2;
  str_write[38] = (byte)VB3;

  str_write[39] = (byte)VB4;
  str_write[40] = (byte)VB5;

  str_write[41] = (byte)VB6;
  str_write[42] = (byte)0;

  for (i = 4; i <= 42; i++)
  {
      Temp_FCS = Temp_FCS + str_write[i];
  }
  str_write[43] = (byte)(Temp_FCS % 256);
  str_write[44] = (byte)0x16;//H16

  Serial.write(str_write, sizeof(str_write));

  delay(100);
    byte str_val[6];
    str_val[0] = (byte)0x10;//H10; 
    str_val[1] = (byte)0x02;//H2;
    str_val[2] = (byte)0x00;//H0;
    str_val[3] = (byte)0x5C;//H5C;
    str_val[4] = (byte)0x5E;//H5E;
    str_val[5] = (byte)0x16;//H16;
    Serial.write(str_val, sizeof(str_val));
  delay(100);
}


void readTime() {
  Wire.beginTransmission(DS1307);
  Wire.write(byte(0));
  Wire.endTransmission();
  Wire.requestFrom(DS1307, 7);
  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hourofday = bcdToDec(Wire.read());
  dayofweek = bcdToDec(Wire.read());
  date = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}

}