/*
Controller : Arduino Micro
*/
#include <Wire.h>
//#include <ivafruit_ADS1015.h>
#include <Adafruit_ADS1015.h>
#include <ADS7828.h>
#include<string.h>
#include<stdio.h>
//#include "HardwareSerial1l.h"
Adafruit_ADS1115 ads1115 (0x49);
ADS7828 adc(0x48); // A0 GND , A1 GND
String inputString ="";
String newString="";
String DacinString="";
String DacwriteString="";
unsigned long read_value = 0;
unsigned long read_value1 = 0;
unsigned long read_value2= 0;
unsigned long read_value3 = 0;
unsigned long read_value4 = 0;
unsigned long read_value5 = 0;
unsigned long read_value6 = 0;
unsigned long read_value7 = 0;
unsigned long read_value8 = 0;
unsigned long read_value9 = 0;
unsigned long read_value10 = 0;
unsigned long read_value11 = 0;
unsigned long read_value12 = 0;
unsigned long read_value13 = 0;
unsigned long read_value14 = 0;
unsigned long read_value15 = 0;
unsigned long read_value16 = 0;
unsigned int vref = 4095;
//int32_t adc;
int z;
unsigned int k=0;
long int t=0;
char inChar[5];
#define disk1 0X10//Address of dac chip 1 in LDMD board
#define disk2 0X12//Address of dac chip 2 in comparator
#define relayadd 0X41 //Address of dac chip
static int integerValue=0;
static float integerValue1=0;
static float integerValue11=0;
int integerValue2=0;
unsigned int outValue=0xff; //intial off state for relay
unsigned long req_time;
unsigned long rep_time;
unsigned long res_time;
long int ivolt1;
unsigned int address=0X30; // this for relay address
int32_t adc0;//
int16_t outputValue;
int16_t Dac_data;
int32_t iadc0, iadc1;
unsigned long ivolt0;
double volt0;
long int volt1;
long int offset=35;
//////////
const int buttonPin0 =A0;
const int buttonPin1 =A1; // the number of the pushbutton pin
int buttonState0, buttonState1 =HIGH;
const int EN =6;
const int a0 =4;
const int a1 =5;
const int led1 =7;//relay off
const int led2 =8;//relay on
const int led3 =9;//moniter current less 100
const int led4 =10;//moniter current greater
const int led5 =11;//powerless then 500
const int va=12;//power greater then
unsigned long entrymillis;
unsigned long exitmillis;
unsigned long time_;
unsigned int cont = 0X03;// this relay control select bit
unsigned int lsb_analogvalue,msb_analogvalue;
unsigned int dat_len;
////////////////
void dac(int deviceaddress, unsigned int eeaddress,int data ) // 0x10,0x30,dacvalue
{
Wire.endTransmission();
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress));
Wire.write((int)(data >> 4));
Wire.write((int)(data << 4));
Wire.endTransmission();
}
void relay(int address, unsigned int count,int out )
{
Wire.endTransmission();
Wire.beginTransmission(address);
Wire.write((int)(count));
Wire.write((int)( out ));
Wire.endTransmission();
}
void pdconv(int u0)
{
int f0,f1,f2,f3,f4,f5,f6,f7,f8,f9;
f0=u0/100000;
f1=u0%100000;
f2=f1/10000;
f3=f1%10000;
f4=f3/1000;
f5=f3%1000;
f6=f5/100;
f7=f5%100;
f8=f7/10;
f9=f7%10;
Serial1.print(f0);
Serial1.print(f2);
Serial1.print(f4);
Serial1.print(f6);
Serial1.print(f8);
Serial1.print(f9);
Serial1.print('E');
Serial1.println('6');
exitmillis = micros();//millis();
time_ = exitmillis - entrymillis;
Serial1.println(time_);
Serial1.print("\r"); // 0x0d Serial1l1.println(); the gui expects this
Serial1.print("\n"); //0x0a
}
void setup(void)
{
pinMode(buttonPin0, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(EN, OUTPUT);
pinMode(a0, OUTPUT);
pinMode(a1, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(va,OUTPUT);
Serial1.begin(115200);
Wire.begin();
Wire.setClock(400000L);
String inputString = "";
adc.init(EXT);
ads1115.begin();
while(!Serial1);
//adc.commanding();
digitalWrite(va, HIGH);
dac(0x12,0x70,0x00);// to use external reference DAC
delay(2);
dac(0x12,0x30,4095); // update initially with '0' input.
delay(2);
//dac(0x10,0x30,1000);
}
void loop()
{
Input:
while(Serial1.available()<=0);
inChar[k] = Serial1.read(); // char
if(inChar[0]=='P')
{
entrymillis=micros();// millis();
digitalWrite(va, HIGH);
read_value1 = adc.read(0, SD);
read_value2 = adc.read(0, SD);
read_value3 = adc.read(0, SD);
read_value4 = adc.read(0, SD);
read_value5 = adc.read(0, SD);
read_value6 = adc.read(0, SD);
read_value7= adc.read(0, SD);
read_value8 = adc.read(0, SD);
read_value9 = adc.read(0, SD);
read_value10 = adc.read(0, SD);
read_value11 = adc.read(0, SD);
read_value12 = adc.read(0, SD);
read_value13 = adc.read(0, SD);
read_value14 = adc.read(0, SD);
read_value15 = adc.read(0, SD);
read_value16 = adc.read(0, SD);
read_value=((read_value1+read_value2+read_value3+read_value4+read_value5+read_value6+read_value7+read_value8+read_value9+read_value10+read_value11+read_value12+read_value13+read_value14+read_value15+read_value16)/16);
ivolt0 = read_value;
ivolt1=(float)read_value*1.22;
//Serial1.println(ivolt1);
if(ivolt1 >35)
{
ivolt1 =ivolt1-35;
pdconv(ivolt1);
}
else
{
ivolt1 =2;
pdconv(ivolt1);
}
goto Input;
}
//read_value = adc.read(0, SD);
}