| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

DC2364A.ino
Go to the documentation of this file.
1 /*!
2 Linear Technology DC2364A Demonstration Board with SPI interface.
3 LTC2873: Two-Wire RS485/RS232 Transceiver with Switchable Termination.
4
5 @verbatim
6
7 NOTES
8 Setup:
9 Set the terminal baud rate to 115200 and select the newline terminator.
10 Ensure all jumpers on the demo board are installed in their default positions
11 from the factory. Refer to Demo Manual DC2364A.
12
13 How to test:
14
15
16 USER INPUT DATA FORMAT:
17 decimal : 1024
18 hex : 0x400
19 octal : 02000 (leading 0 "zero")
20 binary : B10000000000
21 float : 1024.0
22
23 @endverbatim
24 http://www.linear.com/product/LTC2873
25
26
27 Copyright 2018(c) Analog Devices, Inc.
28
29 All rights reserved.
30
31 Redistribution and use in source and binary forms, with or without
32 modification, are permitted provided that the following conditions are met:
33 - Redistributions of source code must retain the above copyright
34 notice, this list of conditions and the following disclaimer.
35 - Redistributions in binary form must reproduce the above copyright
36 notice, this list of conditions and the following disclaimer in
37 the documentation and/or other materials provided with the
38 distribution.
39 - Neither the name of Analog Devices, Inc. nor the names of its
40 contributors may be used to endorse or promote products derived
41 from this software without specific prior written permission.
42 - The use of this software may or may not infringe the patent rights
43 of one or more patent holders. This license does not release you
44 from the requirement that you obtain separate licenses from these
45 patent holders to use this software.
46 - Use of the software either in source or binary form, must be run
47 on or directly connected to an Analog Devices Inc. component.
48
49 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
50 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
51 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
53 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
55 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
57 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 //! @ingroup Transceivers
62 //! @{
63 //! @defgroup LTC2873 LTC2873: Two-Wire RS485/RS232 Transceiver with Switchable Termination.
64 //! @}
65
66 /*! @file
67 @ingroup LTC2873
68 Library for LTC2873: Two-Wire RS485/RS232 Transceiver with Switchable Termination.
69 */
70
71 // Headerfiles
72 #include <UserInterface.h> // LTC defined
73 #include <SoftwareSerial.h>
74
75 //! @name Assigned Pin Numbers
76 //! @{
77 #define RO 10 // soft UART with interrupt pin. (SSerial RX)
78 #define DI 11 // soft UART with interrupt pin (SSerial TX)
79 #define DE485_F232 3 // RE485B jumpered to here. (SSerialTxControl)
80 #define RE485b 4 // beware of jumper tying to DE485
81 #define MODE485 5 // "select" RS485 if high, 232 if low.
82 #define TE485b 6 // RS485 termination enable BAR
83 #define LB 7 // Loopback 232 or 485
84 #define SHDNb 2 // Shutdown BAR
85 //!@}
86
87 // Global Variables
88 int byteReceived;
89 int byteSend;
90 int di_toggle = 0;
91 int SDNb_val, MODE485_val, REb_val, DE485_F232_val, TEb_val, LB_val, DI_val;
92
93 //! Initialize Linduino
94 void setup()
95 {
96 // Set pin directions
97 pinMode(RO, INPUT_PULLUP);
98 pinMode(DI, OUTPUT);
99 pinMode(DE485_F232, OUTPUT);
100 pinMode(RE485b, OUTPUT);
101 pinMode(MODE485,OUTPUT);
102 pinMode(TE485b,OUTPUT);
103 pinMode(LB,OUTPUT);
104 pinMode(SHDNb,OUTPUT);
105
106 // Initialize pins
107 digitalWrite(DI,LOW);
108 digitalWrite(DE485_F232,LOW);
109 digitalWrite(RE485b,HIGH);
110 digitalWrite(MODE485,HIGH);
111 digitalWrite(TE485b,HIGH);
112 digitalWrite(LB,LOW);
113 digitalWrite(SHDNb,LOW);
114
115 TCCR2B = TCCR2B & 0b11111000 | 0x01; // set PWM freq to 31372.55Hz (and scales timers!)
116 //TCCR2B = TCCR2B & 0b11111000 | 0x04; // set PWM freq on pins 2, 11 to 490.20Hz (default)
117
118 // Open communications to serial monitor
119 Serial.begin(115200); // DON'T FORGET TO SET SERIAL MONITOR TO THIS SPEED!!!
120 // RS485Serial.begin(4800); // Start the software serial port, to another device
121 print_title(); // Menu on serial monitor
122 print_status();
123 }
124
125 //! Repeats Linduino loop
126 void loop()
127 {
128 uint8_t user_command = read_int();
129 Serial.print(F(" "));
130
131 if (user_command > 0 && user_command < 27)
132 {
133 update(user_command);
134 Serial.print(user_command);
135 print_status(); // Print status after operation
136 }
137 else if (user_command == 'm')
138 {
139 update(user_command);
140 Serial.print(F("m"));
141 print_status(); // Print status after operation
142 }
143 else
144 {
145 Serial.println(F("Wrong choice. TRY AGAIN!"));
146 }
147 }
148
149 //! Prints the title block when program first starts.
150 void print_title(void)
151 {
152 Serial.println(F(" "));
153 Serial.println(F("**************************************************************************"));
154 Serial.println(F("DC2364A Demonstration Board for the LTC2873 "));
155 Serial.println(F("**************************************************************************"));
156 Serial.println(F("PIN CONTROL: MODES:"));
157 Serial.println(F("1. Flip SHDNb 10. Shutdown 19. 485 Term"));
158 Serial.println(F("2. Flip 485/232b 11. 232 RX+TX Slow 20. 485 RX+Term"));
159 Serial.println(F("3. Flip RE485b 12. 232 RX+TX Fast 21. 485 TX+Term"));
160 Serial.println(F("4. Flip DE485/F232 13. 232 Loopbk+TX Slow 22. 485 RX+TX+Term"));
161 Serial.println(F("5. Flip TE485b 14. 232 Loopbk+TX Fast 23. 485 Loopbk"));
162 Serial.println(F("6. Flip LB 15. 485 Ready 24. 485 Loopbk+TX"));
163 Serial.println(F("7. Flip DI 16. 485 RX 25. 485 Loopbk+Term"));
164 Serial.println(F("8. 31.4kHz 50% on DI* 17. 485 TX 26. 485 Loopbk+TX+Term"));
165 Serial.println(F("9. 31.4kHz 10% on DI* 18. 485 RX+TX "));
166 Serial.println(F("**************************************************************************"));
167 Serial.println(F("Press m to stop 31.4 kHz signal "));
168 Serial.println(F(" "));
169 Serial.println(F("CURRENT STATE:"));
170 Serial.println(F("User Entry SHDNb 485 RE485b DE485 TE485b LB DI MODE"));
171 Serial.println(F("-----------------------------------------------------------------------------"));
172 Serial.print(F(" "));
173 }
174
175 //! Prints the status of each pin based on user selection.
176 void print_status(void)
177 {
178 SDNb_val = digitalRead(SHDNb);
179 MODE485_val = digitalRead(MODE485);
180 REb_val = digitalRead(RE485b);
181 DE485_F232_val = digitalRead(DE485_F232);
182 TEb_val = digitalRead(TE485b);
183 LB_val = digitalRead(LB);
184
185 Serial.print(F("\t\t"));
186 Serial.print(SDNb_val, DEC);
187 Serial.print(F("\t"));
188 Serial.print(MODE485_val, DEC);
189 Serial.print(F("\t"));
190 Serial.print(REb_val, DEC);
191 Serial.print(F("\t"));
192 Serial.print(DE485_F232_val, DEC);
193 Serial.print(F("\t"));
194 Serial.print(TEb_val, DEC);
195 Serial.print(F("\t"));
196 Serial.print(LB_val, DEC);
197 Serial.print(F("\t"));
198
199 if (di_toggle) // must check if pwm mode to preserve, otherwise, digitalRead will kill it.
200 {
201 Serial.print("pwm");
202 }
203 else // not pwm'ing it.
204 {
205 DI_val = digitalRead(DI);
206 Serial.print(DI_val,DEC);
207 }
208 Serial.print(F("\t"));
209
210 String status_val = status();
211 // String status_val = find_status();
212 Serial.print(status_val);
213 Serial.print(F("\n"));
214 }
215
216 //! Returns a descriptive string based on status of pins.
217 String status(void)
218 {
219 if (!SDNb_val)
220 return "SHUTDOWN";
221 if (!MODE485_val && !DE485_F232_val && !LB_val)
222 return "232 RX+TX SLOW";
223 if (!MODE485_val && !DE485_F232_val && LB_val)
224 return "232 LB+TX SLOW";
225 if (!MODE485_val && DE485_F232_val && !LB_val)
226 return "232 RX+TX FAST";
227 if (!MODE485_val && DE485_F232_val && LB_val)
228 return "232 LB+TX FAST";
229 if (MODE485_val && !DE485_F232_val && REb_val && TEb_val && !LB_val)
230 return "485 READY ";
231 if (MODE485_val && !DE485_F232_val && REb_val && !TEb_val && !LB_val)
232 return "485 TERM ";
233 if (MODE485_val && DE485_F232_val && REb_val && TEb_val && !LB_val)
234 return "485 TX ";
235 if (MODE485_val && DE485_F232_val && REb_val && !TEb_val && !LB_val)
236 return "485 TX+TERM";
237 if (MODE485_val && !DE485_F232_val && !REb_val && TEb_val && !LB_val)
238 return "485 RX ";
239 if (MODE485_val && !DE485_F232_val && !REb_val && !TEb_val && !LB_val)
240 return "485 RX+TERM";
241 if (MODE485_val && DE485_F232_val && !REb_val && TEb_val && !LB_val)
242 return "485 RX+TX";
243 if (MODE485_val && DE485_F232_val && !REb_val && !TEb_val && !LB_val)
244 return "485 RX+TX+TERM";
245 if (MODE485_val && !DE485_F232_val && !REb_val && TEb_val && LB_val)
246 return "485 LB ";
247 if (MODE485_val && !DE485_F232_val && !REb_val && !TEb_val && LB_val)
248 return "485 LB+TERM";
249 if (MODE485_val && DE485_F232_val && !REb_val && TEb_val && LB_val)
250 return "485 LB+TX";
251 if (MODE485_val && DE485_F232_val && !REb_val && !TEb_val && LB_val)
252 return "485 LB+TX+TERM";
253 }
254
255 //! Returns a descriptive string based on status of pins.
256 String find_status(void)
257 {
258 String status = "";
259 if (!SDNb_val)
260 status = status + "SHUTDOWN";
261 else
262 {
263 if (MODE485_val)
264 {
265 status = status + "485 ";
266 if (DE485_F232_val)
267 status = status + "ENABLE ";
268 else
269 status = status + "DISABLE ";
270 }
271 else
272 {
273 status = status + "232 ";
274 if (DE485_F232_val)
275 status = status + "FAST ";
276 else
277 status = status + "SLOW ";
278 }
279
280 if (LB_val)
281 status = status + "LB ";
282 if (!REb_val)
283 status = status + "RX ";
284 if (DE485_F232_val)
285 status = status + "TX ";
286
287 if (!TEb_val)
288 status = status + "TERM ";
289 }
290 return status;
291 }
292
293 //! Function to toggle the state of a pin.
294 void flipPin(uint8_t pin)
295 {
296 int pinValue = digitalRead(pin);
297 if (pinValue) // if pin is high
298 digitalWrite(pin, LOW); // change to low
299 else
300 digitalWrite(pin, HIGH); //otherwise, set high
301 }
302
303 //! Update the state of pins based on user selection.
304 void update(uint8_t user_command)
305 {
306 switch (user_command)
307 {
308 case 1:
309 {
310 // Flip SHDNb
311 flipPin(SHDNb);
312 break;
313 }
314 case 2:
315 {
316 // Flip 485/232b
317 flipPin(MODE485);
318 break;
319 }
320 case 3:
321 {
322 // Flip RE485b
323 flipPin(RE485b);
324 break;
325 }
326 case 4:
327 {
328 // Flip DE485/F232
329 flipPin(DE485_F232);
330 break;
331 }
332 case 5:
333 {
334 // Flip TE485b
335 flipPin(TE485b);
336 break;
337 }
338 case 6:
339 {
340 // Flip LB
341 flipPin(LB);
342 break;
343 }
344 case 7:
345 {
346 // Flip DI
347 di_toggle=0; // clear the toggle variable so the status will print correctly
348 flipPin(DI);
349 break;
350 }
351 case 8: // pwm on DI. Freq set by TCCR2B at top
352 {
353 // 31.4kHz 50% on DI*
354 analogWrite(DI,128); // 25% duty cycle is between 0 and 255
355 di_toggle = 1; // set flag for reporting status on this bit
356 break;
357 }
358 case 109: // 120 = 'x'
359 {
360 analogWrite(DI,0); // Stop 31.4 kHz signal
361 di_toggle = 0; // set flag for reporting status on this bit
362 break;
363 }
364 case 9: // pwm on DI. Freq set by TCCR2B at top
365 {
366 // 31.4kHz 10% on DI*
367 analogWrite(DI,26); // 10% duty cycle is between 0 and 255
368 di_toggle = 1; // set flag for reporting status on this bit
369 break;
370 }
371 case 10:
372 {
373 //SHUTDOWN
374 digitalWrite(SHDNb,LOW);
375 digitalWrite(MODE485,HIGH);
376 digitalWrite(RE485b,HIGH);
377 digitalWrite(DE485_F232,LOW);
378 digitalWrite(TE485b,HIGH);
379 digitalWrite(LB,LOW);
380 //digitalWrite(DI,LOW);
381 break;
382 }
383 case 11:
384 {
385 //232 RX + TX SLOW
386 digitalWrite(SHDNb,HIGH);
387 digitalWrite(MODE485,LOW);
388 digitalWrite(RE485b,HIGH);
389 digitalWrite(DE485_F232,LOW);
390 digitalWrite(TE485b,HIGH);
391 digitalWrite(LB,LOW);
392 //digitalWrite(DI,LOW);
393 break;
394 }
395 case 12:
396 {
397 //232 RX + TX FAST
398 digitalWrite(SHDNb,HIGH);
399 digitalWrite(MODE485,LOW);
400 digitalWrite(RE485b,HIGH);
401 digitalWrite(DE485_F232,HIGH);
402 digitalWrite(TE485b,HIGH);
403 digitalWrite(LB,LOW);
404 //digitalWrite(DI,LOW);
405 break;
406 }
407 case 13:
408 {
409 //232 LOOPBACK + TX SLOW
410 digitalWrite(SHDNb,HIGH);
411 digitalWrite(MODE485,LOW);
412 digitalWrite(RE485b,HIGH);
413 digitalWrite(DE485_F232,LOW);
414 digitalWrite(TE485b,HIGH);
415 digitalWrite(LB,HIGH);
416 //digitalWrite(DI,LOW);
417 break;
418 }
419 case 14:
420 {
421 //232 LOOPBACK + TX FAST
422 digitalWrite(SHDNb,HIGH);
423 digitalWrite(MODE485,LOW);
424 digitalWrite(RE485b,HIGH);
425 digitalWrite(DE485_F232,HIGH);
426 digitalWrite(TE485b,HIGH);
427 digitalWrite(LB,HIGH);
428 //digitalWrite(DI,LOW);
429 break;
430 }
431 case 15:
432 {
433 //485 READY
434 digitalWrite(SHDNb,HIGH);
435 digitalWrite(MODE485,HIGH);
436 digitalWrite(RE485b,HIGH);
437 digitalWrite(DE485_F232,LOW);
438 digitalWrite(TE485b,HIGH);
439 digitalWrite(LB,LOW);
440 //digitalWrite(DI,LOW);
441 break;
442 }
443 case 16:
444 {
445 //485 RX
446 digitalWrite(SHDNb,HIGH);
447 digitalWrite(MODE485,HIGH);
448 digitalWrite(RE485b,LOW);
449 digitalWrite(DE485_F232,LOW);
450 digitalWrite(TE485b,HIGH);
451 digitalWrite(LB,LOW);
452 //digitalWrite(DI,LOW);
453 break;
454 }
455 case 17:
456 {
457 //485 TX
458 digitalWrite(SHDNb,HIGH);
459 digitalWrite(MODE485,HIGH);
460 digitalWrite(RE485b,HIGH);
461 digitalWrite(DE485_F232,HIGH);
462 digitalWrite(TE485b,HIGH);
463 digitalWrite(LB,LOW);
464 //digitalWrite(DI,LOW);
465 break;
466 }
467 case 18:
468 {
469 //485 RX + TX
470 digitalWrite(SHDNb,HIGH);
471 digitalWrite(MODE485,HIGH);
472 digitalWrite(RE485b,LOW);
473 digitalWrite(DE485_F232,HIGH);
474 digitalWrite(TE485b,HIGH);
475 digitalWrite(LB,LOW);
476 //digitalWrite(DI,LOW);
477 break;
478 }
479 case 19:
480 {
481 //485 TERMINATION
482 digitalWrite(SHDNb,HIGH);
483 digitalWrite(MODE485,HIGH);
484 digitalWrite(RE485b,HIGH);
485 digitalWrite(DE485_F232,LOW);
486 digitalWrite(TE485b,LOW);
487 digitalWrite(LB,LOW);
488 //digitalWrite(DI,LOW);
489 break;
490 }
491 case 20:
492 {
493 //485 RX + TERMINATION
494 digitalWrite(SHDNb,HIGH);
495 digitalWrite(MODE485,HIGH);
496 digitalWrite(RE485b,LOW);
497 digitalWrite(DE485_F232,LOW);
498 digitalWrite(TE485b,LOW);
499 digitalWrite(LB,LOW);
500 //digitalWrite(DI,LOW);
501 break;
502 }
503 case 21:
504 {
505 //485 TX + TERMINATION
506 digitalWrite(SHDNb,HIGH);
507 digitalWrite(MODE485,HIGH);
508 digitalWrite(RE485b,HIGH);
509 digitalWrite(DE485_F232,HIGH);
510 digitalWrite(TE485b,LOW);
511 digitalWrite(LB,LOW);
512 //digitalWrite(DI,LOW);
513 break;
514 }
515 case 22:
516 {
517 //485 RX + TX + TERMINATION
518 digitalWrite(SHDNb,HIGH);
519 digitalWrite(MODE485,HIGH);
520 digitalWrite(RE485b,LOW);
521 digitalWrite(DE485_F232,HIGH);
522 digitalWrite(TE485b,LOW);
523 digitalWrite(LB,LOW);
524 //digitalWrite(DI,LOW);
525 break;
526 }
527 case 23:
528 {
529 //485 LOOPBACK
530 digitalWrite(SHDNb,HIGH);
531 digitalWrite(MODE485,HIGH);
532 digitalWrite(RE485b,LOW);
533 digitalWrite(DE485_F232,LOW);
534 digitalWrite(TE485b,HIGH);
535 digitalWrite(LB,HIGH);
536 //digitalWrite(DI,LOW);
537 break;
538 }
539 case 24:
540 {
541 //485 LOOPBACK + TX
542 digitalWrite(SHDNb,HIGH);
543 digitalWrite(MODE485,HIGH);
544 digitalWrite(RE485b,LOW);
545 digitalWrite(DE485_F232,HIGH);
546 digitalWrite(TE485b,HIGH);
547 digitalWrite(LB,HIGH);
548 //digitalWrite(DI,LOW);
549 break;
550 }
551 case 25:
552 {
553 //485 LOOPBACK + TERM
554 digitalWrite(SHDNb,HIGH);
555 digitalWrite(MODE485,HIGH);
556 digitalWrite(RE485b,LOW);
557 digitalWrite(DE485_F232,LOW);
558 digitalWrite(TE485b,LOW);
559 digitalWrite(LB,HIGH);
560 //digitalWrite(DI,LOW);
561 break;
562 }
563 case 26:
564 {
565 //485 LOOPBACK + TX + TERM
566 digitalWrite(SHDNb,HIGH);
567 digitalWrite(MODE485,HIGH);
568 digitalWrite(RE485b,LOW);
569 digitalWrite(DE485_F232,HIGH);
570 digitalWrite(TE485b,LOW);
571 digitalWrite(LB,HIGH);
572 //digitalWrite(DI,LOW);
573 break;
574 }
575
576 }
577 }
SDNb_val
static int SDNb_val
Definition: DC2364A.ino:91
byteReceived
static int byteReceived
Definition: DC2364A.ino:88
find_status
String find_status(void)
Returns a descriptive string based on status of pins.
Definition: DC2364A.ino:256
user_command
unsigned char user_command
Definition: Serial_EEPROM_Test.ino:75
TE485b
#define TE485b
Definition: DC2364A.ino:82
di_toggle
static int di_toggle
Definition: DC2364A.ino:90
status
String status(void)
Returns a descriptive string based on status of pins.
Definition: DC2364A.ino:217
DI
#define DI
Definition: DC2364A.ino:78
REb_val
static int REb_val
Definition: DC2364A.ino:91
LB_val
static int LB_val
Definition: DC2364A.ino:91
RE485b
#define RE485b
Definition: DC2364A.ino:80
DE485_F232_val
static int DE485_F232_val
Definition: DC2364A.ino:91
print_title
static void print_title(void)
Prints the title block when program first starts.
Definition: DC2364A.ino:150
flipPin
static void flipPin(uint8_t pin)
Function to toggle the state of a pin.
Definition: DC2364A.ino:294
MODE485_val
static int MODE485_val
Definition: DC2364A.ino:91
DI_val
static int DI_val
Definition: DC2364A.ino:91
loop
static void loop()
Repeats Linduino loop.
Definition: DC2364A.ino:126
RO
#define RO
Definition: DC2364A.ino:77
DE485_F232
#define DE485_F232
Definition: DC2364A.ino:79
MODE485
#define MODE485
Definition: DC2364A.ino:81
LB
#define LB
Definition: DC2364A.ino:83
update
static void update(uint8_t user_command)
Update the state of pins based on user selection.
Definition: DC2364A.ino:304
UserInterface.h
read_int
int32_t read_int()
Definition: UserInterface.cpp:93
setup
static void setup()
Initialize Linduino.
Definition: DC2364A.ino:94
TEb_val
static int TEb_val
Definition: DC2364A.ino:91
SHDNb
#define SHDNb
Definition: DC2364A.ino:84
byteSend
static int byteSend
Definition: DC2364A.ino:89
print_status
static void print_status(void)
Prints the status of each pin based on user selection.
Definition: DC2364A.ino:176