diff --git a/FC_InferfaceBoard/FC_InferfaceBoard.ino b/FC_InferfaceBoard/FC_InferfaceBoard.ino index 08c503f..15f6ff5 100644 --- a/FC_InferfaceBoard/FC_InferfaceBoard.ino +++ b/FC_InferfaceBoard/FC_InferfaceBoard.ino @@ -6,23 +6,38 @@ #include "SC_AI.h" // Differential analog input collector #include "SC_MC9.h" // MC9 Tempreture Controller +#define RcvOK "OK\r\n" +#define RcvErr "ER\r\n" +#define MODE_DEBUG false + // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network. // gateway and subnet are optional: -byte mac[] = { - 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x00 }; +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x00 }; IPAddress ip(192, 168, 20, 177); IPAddress gateway(192, 168, 20, 1); IPAddress subnet(255, 255, 255, 0); -// telnet defaults to port 23 -EthernetServer server(23); -boolean alreadyConnected = false; // whether or not the client was connected previously +// SCPI defaults to port 5025 +EthernetServer server(5025); +EthernetClient client; + +// HTTP defaults to port 5025 +EthernetServer web(80); + +unsigned long lastDataReceivedTime; +unsigned long timeoutPeriod = 5000; // 타임아웃 시간 (5초) + +bool busy = false; +bool connection = false; +char buffer[1024]; -int Size_AI = 8; +int Size_AI = 16; int AI_VALUES[] = { - 0, 0, 0, 0, 0, 0, 0, 0 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; +int msCnt = 0; +unsigned long timer = 0; void setup() { // put your setup code here, to run once: @@ -35,14 +50,12 @@ void setup() { SC_AI_setup(); SC_MC9_setup(); - - // - MsTimer2::set(50, AnalogIn); - MsTimer2::set(1000, AnalogIn_Print); + // Timer set + MsTimer2::set(10, periodic_10ms); MsTimer2::start(); // Report address - Serial.print("MAC:"); + Serial.print("MAC>>"); for (int i = 0; i < 6; i++) { if (mac[i] < 16) { Serial.print("0"); @@ -53,37 +66,81 @@ void setup() { } } Serial.println(); - Serial.print("IP:"); + Serial.print("IP >>"); Serial.println(Ethernet.localIP()); - - } void loop() { // wait for a new client: - EthernetClient client = server.available(); - //SC_AI(AI_VALUES, Size_AI); - // check if client is connected + + webReponse(); + client = server.available(); + + if (client) { + // check if client is connected + Serial.println("Client Connected!!!"); + + // Do What Message Command + while(client.connected()){ + + int buffCnt = 0; + + // check for command byte + if (client.available() > 0) { + char c; + + String command; + String cmd; + unsigned int data[32]; + int dataSize; + + busy = true; + // Read message by byte + while ((c = client.read())) { + // Read data until LF or CR + if((c != 10) && (c != 13)){ + command += c; + }else{ + break; + } + } + + cmd = demuxCMD(command, data, &dataSize); + + if(cmd=="AI"){ + client.print(Prcss_AI()); + }else if(cmd=="AO"){ + client.print(Prcss_AO(data, dataSize)); + }else{ + client.print(RcvErr); + } + + + // Debug + if(MODE_DEBUG){ + //Serial.print("Received command: "); + //Serial.println(command); + } + + busy = false; + // 데이터를 수신했으므로 타임아웃 타이머 초기화 + lastDataReceivedTime = millis(); + + }// if end, client.available() + //Serial.println("end client available"); + + // 타임아웃 확인 + if (millis() - lastDataReceivedTime > timeoutPeriod) { + Serial.println("Client Disconnected... (Timeout)"); + client.stop(); + } - // check for command byte - if (client.available() > 0) { - cli();//stop interrupts - Serial.print(" "); - byte command = client.read(); - if (command == 'A'){ - client.print("AI"); - SC_MC9_read(AI_VALUES, Size_AI); - } - if (command == 'B'){ - client.print("AO"); - SC_MC9_write(AI_VALUES, Size_AI); - } - sei();//allow interrupts } - } - //delay(50); + Serial.println("Client Disconnected..."); + + } } @@ -94,17 +151,69 @@ void TCPIP_setup(byte mac[], IPAddress ip, IPAddress gateway, IPAddress subnet){ Ethernet.begin(mac, ip, gateway, subnet); // start listening for clients server.begin(); + web.begin(); } -void AnalogIn(){ - SC_AI(AI_VALUES, Size_AI); +void periodic_10ms(){ + + + msCnt += 10; + if(!busy){ + + SC_AI(AI_VALUES, Size_AI); + + // periodic_1s + if (msCnt > 1000){ + msCnt = 0; + periodic_1s(); + } + } + } -void AnalogIn_Print(){ +void periodic_1s(){ - for (int i = 0; i < 8; i++) { + //AnalogIn_Print(); +} + + +void AnalogIn_Print(){ + for (int i = 0; i < Size_AI; i++) { Serial.print(AI_VALUES[i]); - + Serial.print("\t"); } Serial.println(); +} + + + +void webReponse(){ + EthernetClient webClient = web.available(); + if (webClient) { + Serial.println("Web client connected"); + while (webClient.connected()) { + if (webClient.available()) { + // 웹 브라우저에서 요청이 도착한 경우 + String request = webClient.readStringUntil('\r'); + //Serial.println(request); + webClient.flush(); + + // 웹 브라우저에 출력할 문구 작성 + String response = "HTTP/1.1 200 OK\r\n"; + response += "Content-Type: text/html\r\n\r\n"; + response += ""; + response += Prcss_AI(); + //response += "

Hello, World!

"; + response += ""; + + // 문구를 웹 브라우저로 전송 + webClient.print(response); + delay(1); + + // 클라이언트 연결 종료 + webClient.stop(); + } + } + } + } \ No newline at end of file diff --git a/FC_InferfaceBoard/Processes.ino b/FC_InferfaceBoard/Processes.ino new file mode 100644 index 0000000..a87dde4 --- /dev/null +++ b/FC_InferfaceBoard/Processes.ino @@ -0,0 +1,24 @@ +String Prcss_AI(){ + + String str = ""; + for (int i = 0; i < Size_AI; i++) { + char formattedNumber[5]; // 4자리 숫자 + 널 종료 문자 + sprintf(formattedNumber, "%04X", AI_VALUES[i]); // 4자리로 고정된 형식의 문자열 생성 + str += formattedNumber; // 형식화된 문자열 추가 + //str += String(AI_VALUES[i]); + str += ','; + } + + str += RcvOK; + return str; +} + +String Prcss_AO(unsigned int data[], int dataSize){ + String str = ""; + for(int i=0 ; i= 0) { + String hexValue = rightPart.substring(startIndex, endIndex); + // 16진수 문자열을 16진수 숫자로 변환하여 data 배열에 저장 + data[index] = strtoul(hexValue.c_str(), NULL, 16); + index++; + + startIndex = endIndex + 1; + endIndex = rightPart.indexOf(delimiter, startIndex); + } + + // 남은 마지막 16진수 배열 원소 처리 + String hexValue = rightPart.substring(startIndex); + data[index] = strtoul(hexValue.c_str(), NULL, 16); + index++; + + // dataSize에 배열 크기 저장 + *dataSize = index; + + // 왼쪽 부분인 "ABC"을 반환 + return leftPart; +} diff --git a/libraries/Scitech/SC_AI.cpp b/libraries/Scitech/SC_AI.cpp index 8748ff1..e397b9c 100644 --- a/libraries/Scitech/SC_AI.cpp +++ b/libraries/Scitech/SC_AI.cpp @@ -7,19 +7,21 @@ #include #include - +int aipin[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15}; void SC_AI_setup(){ } void SC_AI(int arr[], int size){ - cli();//stop interrupts + //cli();//stop interrupts int i = 0; for(i = 0 ; i < size ; i++){ - arr[i] = i; + arr[i] = analogRead(aipin[i]); } - sei();//allow interrupts + + + //sei();//allow interrupts } diff --git a/test/mcp4922/mcp4922.ino b/test/mcp4922/mcp4922.ino new file mode 100644 index 0000000..86dee48 --- /dev/null +++ b/test/mcp4922/mcp4922.ino @@ -0,0 +1,47 @@ +// MCP4922 Demo code sinewave at 16 res top to bot. +// For comparison to MCP4725 operation (DAC_RESOLUTION=-5). +#include + + + +SPISettings settingsA(16000000, MSBFIRST, SPI_MODE0); // At 16 = SPI Clock = 8MHz. + +int RCLKPin = 53; // pin 12 on the 74hc595 latch - nSS +int SRCLKPin = 52; // pin 11 on the 74hc595 shift register clock - SCK +int SERPin = 51; // MOSI + +////////////////////////////////////////////////////////////////////////////// +void setup() { + Serial.begin(115200); // Start serial port (debug). + + pinMode(RCLKPin, OUTPUT); // Set SPI control PINs to output. + pinMode(SRCLKPin, OUTPUT); + pinMode(SERPin, OUTPUT); + + SPI.begin(); + + Serial.println("MCP4922 SPI Dual DAC SPI hardware mode"); + noInterrupts(); +} + +////////////////////////////////////////////////////////////////////////////// +// 0 - A, 1 - B +// +void writeMCP4922_AB(byte AB, uint16_t v) { + + v |=0xf000; // B15(A/B)=1 B, B14(BUF)=1 on, B13(GAn) 1=x1 B12(SHDNn) 1=off + if (!AB) v &= ~0x8000; // When zero clear B15 for A. + + SPI.beginTransaction(settingsA); + digitalWrite(RCLKPin, LOW); + SPI.transfer( (0xff00 & v)>>8 ); + SPI.transfer( 0x00ff & v ); + digitalWrite(RCLKPin, HIGH); + SPI.endTransaction; +} + +void loop() { + writeMCP4922_AB( 0, 4092 ); + writeMCP4922_AB( 1, 3000 ); + +}