Linduino
1.3.0
Linear Technology Arduino-Compatible Demonstration Board
CN0416_echo_node.ino
Go to the documentation of this file.
1
/*
2
Very simple RS485 node example for CN0416.
3
Reads address switch on power-up, and reports address. Once a network is assembled,
4
reset node boards one-by-one to verify addresses.
5
6
Once running, main loop looks for a single ASCII HEX character representing the address,
7
if it matches, report "Hello, from node X"
8
9
10
Copyright 2018(c) Analog Devices, Inc.
11
12
All rights reserved.
13
14
Redistribution and use in source and binary forms, with or without
15
modification, are permitted provided that the following conditions are met:
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
18
- Redistributions in binary form must reproduce the above copyright
19
notice, this list of conditions and the following disclaimer in
20
the documentation and/or other materials provided with the
21
distribution.
22
- Neither the name of Analog Devices, Inc. nor the names of its
23
contributors may be used to endorse or promote products derived
24
from this software without specific prior written permission.
25
- The use of this software may or may not infringe the patent rights
26
of one or more patent holders. This license does not release you
27
from the requirement that you obtain separate licenses from these
28
patent holders to use this software.
29
- Use of the software either in source or binary form, must be run
30
on or directly connected to an Analog Devices Inc. component.
31
32
THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
33
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
34
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35
IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
36
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37
LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
38
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
40
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
43
*/
44
45
46
#include <stdint.h>
47
#include <Arduino.h>
48
49
#define DE 2 // RS485 Driver Enable pin
50
51
const
uint8_t
addr2ch
[16] = {
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'F'
};
// number to character
52
53
char
chaddr
;
54
55
void
setup
() {
56
uint8_t addr = 0;
57
pinMode(
DE
, OUTPUT);
58
digitalWrite(
DE
, LOW);
59
60
pinMode(A0, INPUT);
61
pinMode(A1, INPUT);
62
pinMode(A2, INPUT);
63
pinMode(A3, INPUT);
64
65
addr += digitalRead(A3);
66
addr << 1;
67
addr += digitalRead(A2);
68
addr << 1;
69
addr += digitalRead(A1);
70
addr << 1;
71
addr += digitalRead(A0);
72
73
if
(addr > 15) {addr = 15;}
// just in case...
74
chaddr
=
addr2ch
[addr];
75
Serial.begin(9600);
76
delay(2);
77
digitalWrite(
DE
, HIGH);
78
Serial.print(
"Hello, I am node "
);
79
Serial.println(
chaddr
);
80
Serial.flush();
81
delay(2);
82
digitalWrite(
DE
, LOW);
83
}
84
85
86
void
loop
() {
87
char
ch;
88
if
(Serial.available()) {
// If anything comes in Serial (USB),
89
ch = Serial.read();
90
if
(ch ==
chaddr
){
91
digitalWrite(
DE
, HIGH);
92
delay(1);
93
Serial.print(
"Hello from node "
);
// read it and send it out Serial1 (pins 0 & 1)
94
Serial.print(ch);
95
Serial.println(
"!"
);
96
Serial.flush();
97
delay(2);
98
digitalWrite(
DE
, LOW);
99
}
100
}
101
}
loop
static void loop()
Definition:
CN0416_echo_node.ino:86
DE
#define DE
Definition:
CN0416_echo_node.ino:49
setup
static void setup()
Definition:
CN0416_echo_node.ino:55
addr2ch
const uint8_t addr2ch[16]
Definition:
CN0416_echo_node.ino:51
chaddr
char chaddr
Definition:
CN0416_echo_node.ino:53
LTSketchbook
Example Designs
CN0416_echo_node
CN0416_echo_node.ino
Generated on Thu Mar 19 2020 10:59:01 for Linduino by
1.8.13