Category:Interface

From ElectroDragon
Jump to: navigation, search

Reference Guide

Type Distance Speed Receiver Mode ICs More Features
USB 5M -
UART - -
RS232 10M (50ft, 15.24M) 160 kb/s, up to 1Mb/s 10 Receiver Single end MAX232 full duplex
RS422 1000M (4000ft, 1220M) @ 100Kbps 10 Mb/s @ 12M 10 Receiver Differential MAX490 4-Wires, full duplex
RS485 1000M (4000ft, 1220M) @ 100Kbps 10 Mb/s @ 12M 32 Receiver Differential MAX485 2-Wires, half duplex (full duplex in certain mode)

Pinout

Schematic

Demo Code

Demo Code RS485

  • RS485 server
int enablePin = 2;  // use pin 2 for enable pin 
 
void setup() {
  // initialize serial:
  Serial.begin(9600);
  // setup enable pin for server (always high)
  pinMode(enablePin, OUTPUT);
  delay(10);
  digitalWrite(enablePin, HIGH);
}
 
void loop() {
  // send message to slave ("A") with different value
  for (int i = 0; i < 5; i++) {
    switch (i) {
      case 0 : Serial.println("A1000"); break;
      case 1 : Serial.println("A0100"); break;
      case 2 : Serial.println("A0010"); break;
      case 3 : Serial.println("A0001"); break;
    }
    delay(2000);
  }
}
  • RS485 Slave
int enablePin = 2; // ebanble pin for slave
 
String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
 
void setup() {
  // initialize serial:
  Serial.begin(9600);
  // setup enable pin for slave (always low)
  pinMode(enablePin, OUTPUT);
  delay(10);
  digitalWrite(enablePin, LOW);
  // set digital pin 8 - 11 for output
  for (int i = 8; i < 12; i++) {
    pinMode(i, OUTPUT);
  }
}
 
void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    // make sure this message for slave "A"
    if (inputString.charAt(0) == 'A') {  
      for (int i = 1; i < 5 ; i++) {
        if (inputString.charAt(i) == '1')
          digitalWrite(7 + i, HIGH);
        else
          digitalWrite(7 + i, LOW);
      }
    } 
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}
 
/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read(); 
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    } 
  }
}

Documents

Pages in category "Interface"

The following 3 pages are in this category, out of 3 total.