티스토리 뷰

#include <Wire.h>



#define ADC_ADDRESS 0x68

#define ADC_CONFIG 0XFC



#define PIN_MUXINA 4

#define PIN_MUXINB 5

#define PIN_MUXINC 6



#define PIN_MUX0 7

#define PIN_MUX1 8

#define PIN_MUX2 9



#define PIN_RELAY 12



int CommandStatus = 0;

int MuxNum = -1;

int PortNum = -1;

long ADCValue = -1;

void setup() {



  Wire.begin();

  Wire.beginTransmission(ADC_ADDRESS);

  Wire.write(ADC_CONFIG);

  Wire.endTransmission();

  

  pinMode(PIN_MUXINA, OUTPUT);

  pinMode(PIN_MUXINB, OUTPUT);

  pinMode(PIN_MUXINC, OUTPUT);

  pinMode(PIN_RELAY, OUTPUT);



  pinMode(PIN_MUX0, OUTPUT);

  pinMode(PIN_MUX1, OUTPUT);

  pinMode(PIN_MUX2, OUTPUT);

  digitalWrite(PIN_MUX0, HIGH);

  digitalWrite(PIN_MUX1, HIGH);

  digitalWrite(PIN_MUX2, HIGH); 



  Serial.begin(9600);



//  analogReference(DEFAULT);

}



void loop() {

  if(Serial.available() > 0){

    char SerialIn = Serial.read();

    switch(CommandStatus){

      case 0:

        PortNum = -1;

        MuxNum = -1;

        ADCValue = -1;

        h_ClearMuxes();

      case 80:

      case 51:

        if(SerialIn == '<') CommandStatus = 10;

//        Serial.println(SerialIn+60);

        break;

      case 10:

        if(SerialIn == 'U') CommandStatus = 20;

        else CommandStatus = 0;

        break;

      case 20:

        switch(SerialIn){

          case 'A':

            MuxNum = 1;

            CommandStatus = 30;

            break;

          case 'B':

            MuxNum = 2;

            CommandStatus = 30;

            break;

          case 'C':

            MuxNum = 3;

            CommandStatus = 30;

            break;

          case 'I':

            MuxNum = -1;

            CommandStatus = 30;

            break;

          default:

            CommandStatus = 0;

            break;

        }

        break;

      case 30:

        PortNum = SerialIn - 0x30;

        if((PortNum < 0) || (PortNum > 9)){

          CommandStatus = 0;

          break;

        }

        CommandStatus = 40;

        break;

      case 40:

        if(SerialIn == '/') CommandStatus = 50;

        else CommandStatus = 0;

        break;

      case 50:

        if(SerialIn == '>'){

          if(MuxNum == -1) CommandStatus = 80;

          else CommandStatus = 51;

        } else CommandStatus = 0;

        break;

      default:

        CommandStatus = 0;

        break;

      }

  }



  switch(CommandStatus){

    case 51:

      h_SetMux(PortNum);

      switch(MuxNum){

        case 1:

          digitalWrite(PIN_MUX0, LOW);

          break;

        case 2:

          digitalWrite(PIN_MUX1, LOW);

          break;

        case 3:

          digitalWrite(PIN_MUX2, LOW);

          break;

      }

      delay(500);

      ADCValue = h_ReadADCValue();

      h_RespondResult();

      CommandStatus = 0;

      break;

    case 80:

      if(PortNum == 0)  digitalWrite(PIN_RELAY, LOW);

      if(PortNum == 1)  digitalWrite(PIN_RELAY, HIGH);

      break;

    default:

      break;

  }

}



void h_RespondResult(){

    Serial.print("<V");

    switch(MuxNum){

      case 1:

        Serial.print("V"); // Voltage

        break;

      case 2:

        Serial.print("I"); // Current

        break;

      case 3:

        Serial.print("A"); // AUX

        break;

      default:

        CommandStatus = 0;

        break;

    }

    Serial.print(PortNum);

    Serial.print(ADCValue);

    Serial.println("/>");

}



long h_ReadADCValue(){

  Wire.requestFrom((int)ADC_ADDRESS, (int)4);

  byte Byte1 = Wire.read();

  byte Byte2 = Wire.read();

  byte Byte3 = Wire.read();

  byte Byte4 = Wire.read();



/*  

 Serial.print(" : ");

  Serial.print(Byte1);

  Serial.print(", ");

  Serial.print(Byte2);

  Serial.print(", ");

  Serial.print(Byte3);

  Serial.print(", ");

  Serial.print(Byte4);

*/

  long Rst = ((Byte1*256L)+Byte2)*256L+Byte3;

  return Rst;

}



void h_ClearMuxes(){

  digitalWrite(PIN_MUX0, HIGH);

  digitalWrite(PIN_MUX1, HIGH);

  digitalWrite(PIN_MUX2, HIGH);

}



void h_SetMux(int PortNum){

  byte MuxInA = (byte) PortNum & 0x01;

  byte MuxInB = (byte) PortNum & 0x02;

  byte MuxInC = (byte) PortNum & 0x04;



  if(MuxInA == 0) digitalWrite(PIN_MUXINA, LOW);

  else digitalWrite(PIN_MUXINA, HIGH);

  if(MuxInB == 0) digitalWrite(PIN_MUXINB, LOW);

  else digitalWrite(PIN_MUXINB, HIGH);

  if(MuxInC == 0) digitalWrite(PIN_MUXINC, LOW);

  else digitalWrite(PIN_MUXINC, HIGH);

}
반응형