10 changed files with 152 additions and 285 deletions
@ -1,95 +0,0 @@
@@ -1,95 +0,0 @@
|
||||
/* RS 485-Implementierung, die sowohl Schreiben als auch Lesen kann */ |
||||
/* Basis für andere Projekte */ |
||||
|
||||
#define BUTTON 2 |
||||
#define RECEIVE 3 |
||||
#define RECEIVE_ENABLE 4 |
||||
#define SEND 5 |
||||
#define SEND_ENABLE 6 |
||||
|
||||
#define MAX_LEN 128 |
||||
|
||||
void setup() { |
||||
// Buttons connected to analog input lines
|
||||
pinMode(A0,INPUT_PULLUP); |
||||
pinMode(A1,INPUT_PULLUP); |
||||
pinMode(A2,INPUT_PULLUP); |
||||
pinMode(A3,INPUT_PULLUP); |
||||
pinMode(A4,INPUT_PULLUP); |
||||
pinMode(A5,INPUT_PULLUP); |
||||
pinMode(A6,INPUT_PULLUP); |
||||
pinMode(A7,INPUT_PULLUP); |
||||
// Currently, all buttons are also connected via a diode to an interruptable pin, to notify on input changes.
|
||||
// This interruptable pin should be assgined to BUTTON
|
||||
pinMode(BUTTON,INPUT_PULLUP); |
||||
attachInterrupt(digitalPinToInterrupt(BUTTON),button,FALLING); // interrupt for button change
|
||||
|
||||
// Set up pins for RS485 connection
|
||||
pinMode(RECEIVE, INPUT); |
||||
pinMode(RECEIVE_ENABLE, OUTPUT); |
||||
pinMode(SEND, OUTPUT); |
||||
pinMode(SEND_ENABLE, OUTPUT); |
||||
attachInterrupt(digitalPinToInterrupt(RECEIVE),receive,CHANGE); // interrupt for incoming RS485 traffic
|
||||
|
||||
reset485(); |
||||
buttons = 0x0; |
||||
} |
||||
|
||||
void button() { |
||||
buttons = 0xFF ^ (digitalRead(A7)<<7 | digitalRead(A6)<<6 | digitalRead(A5)<<5 | digitalRead(A4)<<4 | digitalRead(A3)<<3 | (!digitalRead(A2))<<2 | digitalRead(A1)<<1 | digitalRead(A0));
|
||||
} |
||||
|
||||
void receive(){ |
||||
bool received_bit = digitalRead(RECEIVE); |
||||
curr = micros(); |
||||
duration = curr - last; |
||||
Serial.print("Input switched to "); |
||||
Serial.print(received_bit?"H":"L");
|
||||
Serial.print(" after "); |
||||
Serial.print(duration); |
||||
Serial.println(" ticks"); |
||||
last = curr; |
||||
if (duration > MAX_TICKS) reset485(); |
||||
received_bit = !received_bit; // received_bit is the current value, the duration of the previous value (=inverted) has been measured
|
||||
duration /= BASE; |
||||
while (duration > 0){ |
||||
byte_idx--; |
||||
recv |= received_bit<<byte_idx; |
||||
if (byte_idx == 0) { |
||||
msg[msg_idx] = recv; |
||||
msg_idx++; |
||||
byte_idx = 8; |
||||
if (recv == 0x00) message_received(); |
||||
} |
||||
duration--; |
||||
} |
||||
} |
||||
|
||||
void reset485(){ |
||||
byte_idx = 8; |
||||
msg_idx = 0; |
||||
recv=0x0; |
||||
} |
||||
|
||||
void message_received(){ |
||||
for (int i=0; i<MAX_LEN; i++) message[i]=msg[i]; |
||||
reset485(); |
||||
} |
||||
|
||||
void button_pressed(int i){ |
||||
Serial.println("not implemented"); |
||||
} |
||||
|
||||
void loop(){ |
||||
if (buttons>0){ |
||||
for (int i=0; i<7; i++){ |
||||
if (buttons & (1<<i)) button_pressed(i);
|
||||
}
|
||||
} |
||||
if (message[0] != 0x0){ |
||||
Serial.print("Received '"); |
||||
Serial.print(message); |
||||
Serial.println("'");
|
||||
message[0] = 0x0; |
||||
} |
||||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,61 +0,0 @@
@@ -1,61 +0,0 @@
|
||||
// Für Controllino könnte man benutzen: sRx = 20, sTx = 21, enable = 53
|
||||
|
||||
int sTx = 2; |
||||
int sRx = 3; |
||||
|
||||
long base = 100; |
||||
long rst = base*10; // reset
|
||||
boolean bt = false; |
||||
long dif = 0; |
||||
long lt = 0; |
||||
long now = 0; |
||||
char rcv = 0; |
||||
int idx = 0; |
||||
String message; |
||||
void setup(){ |
||||
pinMode(sRx,INPUT); |
||||
pinMode(sTx,OUTPUT); |
||||
attachInterrupt(digitalPinToInterrupt(sRx),change485,CHANGE); |
||||
Serial.begin(115200);
|
||||
Serial.println("Receiver ready"); |
||||
} |
||||
|
||||
void reset485(long dif){ |
||||
rcv = 0; |
||||
idx = -1; |
||||
message.reserve(32); |
||||
} |
||||
void change485(){ |
||||
bool bt = digitalRead(sRx); |
||||
now = micros(); |
||||
dif = now - lt; |
||||
lt = now; |
||||
if (dif > rst){ |
||||
reset485(dif); |
||||
} else { |
||||
handle485(!bt,dif); |
||||
} |
||||
} |
||||
|
||||
void handle485(boolean bt, long dif){ |
||||
long count = dif/base; |
||||
for (int i=0; i<count; i++) push485(bt); |
||||
} |
||||
|
||||
void push485(boolean bt){ |
||||
if (idx>=0) rcv |= bt<<idx; |
||||
idx++; |
||||
|
||||
if (idx == 8){ |
||||
if (rcv == 13) { |
||||
Serial.println(message); |
||||
message = ""; |
||||
} else { |
||||
message+=rcv; |
||||
}
|
||||
idx =0; |
||||
rcv =0; |
||||
} |
||||
} |
||||
|
||||
void loop(){} |
@ -1,129 +0,0 @@
@@ -1,129 +0,0 @@
|
||||
// Breadboard
|
||||
//int enable = 2;
|
||||
//int sTx = 4;
|
||||
//int sRx = 5;
|
||||
|
||||
// RS485-Nano Revision 1.0
|
||||
int enable = 9; |
||||
int sTx = 8; |
||||
int sRx = 3; |
||||
|
||||
// Für Controllino könnte man benutzen: sRx = 20, sTx = 21, enable = 53
|
||||
|
||||
int in1 = 4; // o
|
||||
int in2 = 5; // |o
|
||||
int in3 = 6; // ||o
|
||||
int in4 = 7; // |||o
|
||||
// ↓↓↓↓
|
||||
int irq = 2;// ←++++
|
||||
|
||||
long base = 1000; |
||||
|
||||
int id = 0; |
||||
int baud = 115200; |
||||
char c; |
||||
int counter = 0; |
||||
bool reg1=false,reg2=false,reg3=false,reg4=false; |
||||
|
||||
void setup(){ |
||||
Serial.begin(baud);
|
||||
pinMode(A0,INPUT); |
||||
id = analogRead(A0); |
||||
|
||||
pinMode(enable,OUTPUT); |
||||
digitalWrite(enable,LOW); |
||||
|
||||
pinMode(sTx,OUTPUT); |
||||
digitalWrite(sTx,LOW); |
||||
|
||||
pinMode(in1,INPUT); |
||||
pinMode(in2,INPUT); |
||||
pinMode(in3,INPUT); |
||||
pinMode(in4,INPUT); |
||||
|
||||
pinMode(irq,INPUT); |
||||
attachInterrupt(digitalPinToInterrupt(irq),interrupt,RISING); |
||||
|
||||
pinMode(sRx,INPUT); |
||||
Serial.print("Baud rate set to "); |
||||
Serial.println(baud); |
||||
Serial.print("Initialized Arduino #"); |
||||
Serial.println(id); |
||||
} |
||||
|
||||
void interrupt(){ |
||||
Serial.println("Interrupt"); |
||||
reg1 = digitalRead(in1); |
||||
reg2 = digitalRead(in2); |
||||
reg3 = digitalRead(in3); |
||||
reg4 = digitalRead(in4); |
||||
|
||||
} |
||||
bool sendBit(bool bt){ |
||||
digitalWrite(sTx,bt); |
||||
delayMicroseconds(80); |
||||
if (digitalRead(sTx) != bt) return false; |
||||
delayMicroseconds(base-80); |
||||
return true; |
||||
} |
||||
|
||||
bool sendChar(char c){ |
||||
return sendBit(c & 0x01) |
||||
&& sendBit(c & 0x02) |
||||
&& sendBit(c & 0x04) |
||||
&& sendBit(c & 0x08) |
||||
&& sendBit(c & 0x10) |
||||
&& sendBit(c & 0x20) |
||||
&& sendBit(c & 0x40) |
||||
&& sendBit(c & 0x80); |
||||
} |
||||
|
||||
bool sendString(String s){ |
||||
Serial.print("Sending '"+s+"'…"); |
||||
boolean busy = false; |
||||
for (int i = 0; i<11; i++){ |
||||
if (!digitalRead(sRx)) { |
||||
if (!busy){ |
||||
Serial.print("busy…"); |
||||
busy = true; |
||||
} |
||||
i = 0; // wait for free line
|
||||
} |
||||
delayMicroseconds(base); |
||||
} |
||||
digitalWrite(enable,HIGH); |
||||
bool success = sendBit(0); // erste Flanke erzeugen
|
||||
if (success) { |
||||
for (char c : s){ |
||||
if (!sendChar(c)){ |
||||
success = false; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
success = success && sendChar(13); |
||||
success = success && sendBit(1); // letzte Flanke erzeugen
|
||||
digitalWrite(enable,LOW); |
||||
Serial.println(success ? "success" : "failed"); |
||||
return success; |
||||
} |
||||
|
||||
void loop(){ |
||||
if (reg1){ |
||||
reg1 = false; |
||||
sendString("t:02:01"); |
||||
} |
||||
if (reg2){ |
||||
reg2 = false; |
||||
sendString("t:02:02"); |
||||
} |
||||
if (reg3){ |
||||
reg3 = false; |
||||
sendString("t:02:03"); |
||||
} |
||||
if (reg4){ |
||||
reg4 = false; |
||||
sendString("t:02:04"); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Demonstrates how to use LCDisplay in conjunction with SoftRS485: |
||||
* |
||||
* The main _loop_ of this program listens on the RS485 bus. |
||||
* Whenever a message is received from the bus, it is displayed on the LCD. |
||||
*/ |
||||
|
||||
// include the library code:
|
||||
#include <LiquidCrystal.h> |
||||
#include <SoftRS485.h> |
||||
|
||||
#define Max485_RO 3 // read-output of Max485
|
||||
#define Max485_RE 5 // not-read-enable of Max485
|
||||
#define Max485_DE 9 // data enable of Max485
|
||||
#define Max485_DI 8 // data input of Max485
|
||||
|
||||
#define LCD_RS 4 |
||||
#define LCD_EN 6 |
||||
#define LCD_D4 7 |
||||
#define LCD_D5 11 |
||||
#define LCD_D6 12 |
||||
#define LCD_D7 13 |
||||
|
||||
|
||||
|
||||
// initialize the library by associating any needed LCD interface pin with the arduino pin number it is connected to
|
||||
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7); |
||||
|
||||
|
||||
void setup() { |
||||
Serial.begin(115200); |
||||
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
|
||||
invert485polarity(true); |
||||
// set up the LCD's number of columns and rows:
|
||||
lcd.begin(16, 2); |
||||
// Print a message to the LCD.
|
||||
Serial.println("LCDReceive 1.2"); |
||||
lcd.print("LCDReceive 1.2"); |
||||
|
||||
} |
||||
|
||||
void loop() { |
||||
if (available485()) { |
||||
String s = get485message(); |
||||
Serial.println(s); |
||||
lcd.clear(); |
||||
lcd.setCursor(0, 0); |
||||
lcd.print(s); |
||||
if (s.length()>16) { |
||||
lcd.setCursor(0, 1); |
||||
s=s.substring(16); |
||||
s.trim(); |
||||
lcd.print(s); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,96 @@
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Demonstrates how to use LCDisplay in conjunction with SoftRS485: |
||||
* |
||||
* The main _loop_ of this program listens on the RS485 bus. |
||||
* Whenever a message is received from the bus, it is displayed on the LCD. |
||||
*/ |
||||
|
||||
// include the library code:
|
||||
#include <SoftRS485.h> |
||||
|
||||
#define PROGRAM "RS485-Nano 2.0.1" |
||||
|
||||
#define BTN_INT 2 // button interrupt pin
|
||||
#define Max485_RO 3 // read-output of Max485
|
||||
#define Max485_RE 5 // not-read-enable of Max485
|
||||
#define Max485_DE 9 // data enable of Max485
|
||||
#define Max485_DI 8 // data input of Max485
|
||||
|
||||
#define TRESHOLD 100000 // 100ms
|
||||
#define ID 0 |
||||
// 0=Test
|
||||
// 1=Arbeitszimmer
|
||||
// 2=Küche
|
||||
// 3=Bad
|
||||
|
||||
//#define LOG_TO_SERIAL
|
||||
#define SEND_485 |
||||
|
||||
boolean states[8]; |
||||
unsigned long times[8]; |
||||
|
||||
|
||||
|
||||
void setup() { |
||||
Serial.begin(115200); |
||||
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
|
||||
pinMode(BTN_INT,INPUT_PULLUP); |
||||
pinMode(14,INPUT_PULLUP); |
||||
pinMode(15,INPUT_PULLUP); |
||||
pinMode(16,INPUT_PULLUP); |
||||
pinMode(17,INPUT_PULLUP); |
||||
pinMode(18,INPUT_PULLUP); |
||||
pinMode(19,INPUT_PULLUP); |
||||
pinMode(20,INPUT_PULLUP); |
||||
pinMode(21,INPUT_PULLUP); |
||||
|
||||
attachInterrupt(digitalPinToInterrupt(2),isr,CHANGE); |
||||
// Print a message to the LCD.
|
||||
Serial.println(PROGRAM); |
||||
#ifdef LOG_TO_SERIAL |
||||
Serial.println("14 15 16 17 18 19 20 21"); |
||||
#endif |
||||
} |
||||
|
||||
void isr(){ |
||||
states[0] = !digitalRead(14); |
||||
states[1] = !digitalRead(15); |
||||
states[2] = !digitalRead(16); |
||||
states[3] = !digitalRead(17); |
||||
|
||||
states[4] = !digitalRead(18); |
||||
states[5] = !digitalRead(19); |
||||
states[6] = !(analogRead(20)>200); |
||||
states[7] = !(analogRead(21)>200); |
||||
#ifdef LOG_TO_SERIAL |
||||
for (int i=0;i<8;i++){ |
||||
Serial.print(" "); Serial.print(states[i]); Serial.print(" "); |
||||
} |
||||
Serial.println(); |
||||
#endif |
||||
} |
||||
|
||||
#ifdef SEND_485 |
||||
void send(int btn){ |
||||
String s = "{nano:"+String(ID)+",btn:"+String(btn)+"}"; |
||||
for (int i = 0; i<10; i++){
|
||||
if (send485(s.c_str())) break; |
||||
Serial.println("collision detected, trying again:"); |
||||
} |
||||
} |
||||
#endif |
||||
|
||||
void loop(){ |
||||
unsigned long now = micros(); |
||||
for (int i=0;i<8;i++){ |
||||
if (states[i]){ |
||||
if (now - times[i] > TRESHOLD){ |
||||
#ifdef SEND_485 |
||||
send(i+1); |
||||
#endif |
||||
times[i] = now; |
||||
} |
||||
states[i] = 0; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue