티스토리 뷰
#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);
}
반응형
'HWDesk > HardwareControl' 카테고리의 다른 글
EX9017/F Modebus 통신 테스트 (1) | 2020.04.15 |
---|---|
EX9107/F 데이터 읽기 (0) | 2020.04.13 |
[Arduino활용14] RFID 태그 판독하기 (0) | 2020.04.05 |
[Arduino활용13] 로터리 이동 추적하기 (0) | 2020.04.04 |
아두이노 부트로더 (0) | 2020.04.04 |
반응형
250x250
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 전압전류모니터링
- bilient
- Innovations&Hurdles
- ServantClock
- 절연형
- Innovation&Hurdles
- arduino
- Video
- 티스토리챌린지
- 허들
- 둎
- 치매방지
- 빌리언트
- 혁신
- BSC
- badp
- 아두이노
- 오블완
- 혁신과허들
- 심심풀이
- Decorator
- Innovations
- image
- 배프
- 치매
- DYOV
- Hurdles
- 전압
- 심심풀이치매방지기
- 전류
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함