Created
August 15, 2017 10:01
-
-
Save funvill/a79b309ce11d5bbef31ab2a8bf3521e4 to your computer and use it in GitHub Desktop.
A test using the Teensy 3.1 to Sparkfun RS485 breakout to RS485 converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Wiring | |
* ------------------------------------------ | |
* Teensy Pin 0 RX1 ==> RS485 Breakout TX-0 | |
* Teensy Pin 1 TX1 ==> RS485 Breakout RX-1 | |
* Teensy Pin 2 ==> RS485 Breakout RST | |
* Teensy GND ==> RS485 Breakout GND and G | |
* Teensy VIN ==> RS485 Breakout 3-5V | |
* | |
* RS485 Breakout GND => RS485 Breakout G | |
* RS485 Breakout A ==> RS485 converter RX+ And TX+ | |
* RS485 Breakout B ==> RS485 converter RX- And TX- | |
* | |
* RS485 converter RX- => TX- | |
* RS485 converter RX+ => TX+ | |
*/ | |
int RS485RTS = 2 ; | |
int RS485ActivityPin = 13 ; | |
#define RS485_SEND LOW | |
#define RS485_RECV HIGH | |
// set this to the hardware serial port you wish to use | |
#define HWSERIAL Serial1 | |
void setup() { | |
pinMode(RS485ActivityPin, OUTPUT); | |
Serial.begin(9600); | |
HWSERIAL.begin(9600); | |
HWSERIAL.transmitterEnable(RS485RTS); | |
digitalWrite(RS485ActivityPin, HIGH); | |
Serial.println("hello world v14"); | |
HWSERIAL.println("hello world v14"); | |
digitalWrite(RS485ActivityPin, LOW); | |
} | |
void loop() { | |
int incomingByte; | |
if (Serial.available() > 0) { | |
digitalWrite(RS485ActivityPin, HIGH); | |
incomingByte = Serial.read(); | |
Serial.print("USB-FYI: USB received: " ); | |
Serial.println(incomingByte); | |
HWSERIAL.print("HWS-FYI: USB received:"); | |
HWSERIAL.println(incomingByte); | |
digitalWrite(RS485ActivityPin, LOW); | |
} | |
if (HWSERIAL.available() > 0) { | |
digitalWrite(RS485ActivityPin, HIGH); | |
incomingByte = HWSERIAL.read(); | |
Serial.print("USB-FYI: UART received: "); | |
Serial.println(incomingByte); | |
HWSERIAL.print("HWS-FYI: UART received:"); | |
HWSERIAL.println(incomingByte); | |
digitalWrite(RS485ActivityPin, LOW); | |
} | |
static unsigned long previousMillis = 0; // will store last time LED was updated | |
unsigned long currentMillis = millis(); | |
if (currentMillis - previousMillis >= 1000 ) { | |
// save the last time you blinked the LED | |
previousMillis = currentMillis; | |
static unsigned int count = 0 ; | |
count++; | |
digitalWrite(RS485ActivityPin, HIGH); | |
Serial.print("USB-FYI: count: "); | |
Serial.println(count); | |
HWSERIAL.print("HWS-FYI: Ucount:"); | |
HWSERIAL.println(count); | |
digitalWrite(RS485ActivityPin, LOW); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment