void spi_transfer(byte working) { for(int i = 1; i <= 8; i++) // Set up a loop of 8 iterations (8 bits in a byte) { if (working > 127) { digitalWrite (POT_MOSI,HIGH) ; } // If the MSB is a 1 then set MOSI high else { digitalWrite (POT_MOSI, LOW) ; } // If the MSB is a 0 then set MOSI low digitalWrite (CLKdpot,HIGH) ; // Pulse the CLKdpot high working = working << 1 ; // Bit-shift the working byte digitalWrite(CLKdpot,LOW) ; // Pulse the CLKdpot low } }
void spi_out(int CS, byte cmd_byte, byte data_byte) { digitalWrite (CS, LOW); // Set the passed ChipSelect pin to low to start programming spi_transfer(cmd_byte); // Send the passed COMMAND BYTE delay(2); spi_transfer(data_byte); // Send the passed DATA BYTE delay(2); digitalWrite(CS, HIGH); // Set the passed ChipSelect pin to high to end programming }
param = readString.substring(6,9); value = readString.substring(10,13).toInt(); if (param=="V1L") {V1L=value; spi_out(CS1, cmd_byte, V1L);} if (param=="V1R") {V1R=value; spi_out(CS2, cmd_byte, V1R);} if (param=="MU1") {spi_out(CS1, cmd_byte, V1L/5); spi_out(CS2, cmd_byte, V1R/5);} if (param=="UM1") {spi_out(CS1, cmd_byte, V1L); spi_out(CS2, cmd_byte, V1R);}
#include <UIPEthernet.h> #include <String.h> int CS1 = 19; // Chip Select int CS2 = 18; int CS3 = 17; int CS4 = 16; int CS5 = 15; int CS6 = 14; int CS7 = 8; int CS8 = 7; int CLKdpot = 4; // Clock pin 4 arduino int POT_MOSI = 5; // MOSI pin 5 arduino byte cmd_byte = B00010011 ; // Command byte 'write' data to POT uint8_t POTposition1 = 10; //initialize DPOT set initial position uint8_t POTposition2 = 10; uint8_t POTposition3 = 10; uint8_t POTposition4 = 10; uint8_t POTposition5 = 10; uint8_t POTposition6 = 10; uint8_t POTposition7 = 10; uint8_t POTposition8 = 10; uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; uint8_t ip[4] = {192, 168, 6, 25}; // IP address for the webserver uint16_t port = 80; // Use port 80 - the standard for HTTP EthernetServer server(80); String readString = String(100); String param = String(3); int value = 0; int V1L = 0; int V1R = 0; int V2L = 0; int V2R = 0; int V3L = 0; int V3R = 0; int V4L = 0; int V4R = 0; void spi_transfer(byte working) { for(int i = 1; i <= 8; i++) // Set up a loop of 8 iterations (8 bits in a byte) { if (working > 127) { digitalWrite (POT_MOSI,HIGH) ; } // If the MSB is a 1 then set MOSI high else { digitalWrite (POT_MOSI, LOW) ; } // If the MSB is a 0 then set MOSI low digitalWrite (CLKdpot,HIGH) ; // Pulse the CLKdpot high working = working << 1 ; // Bit-shift the working byte digitalWrite(CLKdpot,LOW) ; // Pulse the CLKdpot low } } void spi_out(int CS, byte cmd_byte, byte data_byte) { digitalWrite (CS, LOW); // Set the passed ChipSelect pin to low to start programming spi_transfer(cmd_byte); // Send the passed COMMAND BYTE delay(2); spi_transfer(data_byte); // Send the passed DATA BYTE delay(2); digitalWrite(CS, HIGH); // Set the passed ChipSelect pin to high to end programming } void setup() { Serial.begin(9600); pinMode (CS1, OUTPUT); pinMode (CS2, OUTPUT); pinMode (CS3, OUTPUT); pinMode (CS4, OUTPUT); pinMode (CS5, OUTPUT); pinMode (CS6, OUTPUT); pinMode (CS7, OUTPUT); pinMode (CS8, OUTPUT); pinMode (CLKdpot, OUTPUT); pinMode (POT_MOSI, OUTPUT); spi_out(CS1, cmd_byte, POTposition1); spi_out(CS2, cmd_byte, POTposition2); spi_out(CS3, cmd_byte, POTposition3); spi_out(CS4, cmd_byte, POTposition4); spi_out(CS5, cmd_byte, POTposition5); spi_out(CS6, cmd_byte, POTposition6); spi_out(CS7, cmd_byte, POTposition7); spi_out(CS8, cmd_byte, POTposition8); // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); } void loop() { // listen for incoming clients readString=""; EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); size_t pos = 0; if (readString.length() < 16) { //store characters to string readString +=c; } // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); client.println("<!DOCTYPE HTML>"); client.println("<html>"); client.println("</html>"); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disconnected"); Serial.println(readString); param = readString.substring(6,9); value = readString.substring(10,13).toInt(); Serial.println(param); Serial.println(value); if (param=="V1L") {V1L=value; spi_out(CS1, cmd_byte, V1L);} if (param=="V1R") {V1R=value; spi_out(CS2, cmd_byte, V1R);} if (param=="V2L") {V2L=value; spi_out(CS3, cmd_byte, V2L);} if (param=="V2R") {V2R=value; spi_out(CS4, cmd_byte, V2R);} if (param=="V3L") {V3L=value; spi_out(CS5, cmd_byte, V3L);} if (param=="V3R") {V3R=value; spi_out(CS6, cmd_byte, V3R);} if (param=="V4L") {V4L=value; spi_out(CS7, cmd_byte, V4L);} if (param=="V4R") {V4R=value; spi_out(CS8, cmd_byte, V4R);} if (param=="MU1") { spi_out(CS1, cmd_byte, V1L/5); spi_out(CS2, cmd_byte, V1R/5); spi_out(CS3, cmd_byte, V2L/5); spi_out(CS4, cmd_byte, V2R/5); spi_out(CS5, cmd_byte, V3L/5); spi_out(CS6, cmd_byte, V3R/5); spi_out(CS7, cmd_byte, V4L/5); spi_out(CS8, cmd_byte, V4R/5); } if (param=="UM1") { spi_out(CS1, cmd_byte, V1L); spi_out(CS2, cmd_byte, V1R); spi_out(CS3, cmd_byte, V2L); spi_out(CS4, cmd_byte, V2R); spi_out(CS5, cmd_byte, V3L); spi_out(CS6, cmd_byte, V3R); spi_out(CS7, cmd_byte, V4L); spi_out(CS8, cmd_byte, V4R); } } }
Source: https://habr.com/ru/post/259579/
All Articles