BayEOSArduino Library
SDI12_boards.h
Go to the documentation of this file.
1 
12 /* ======================== Arduino SDI-12 =================================
13 An Arduino library for SDI-12 communication with a wide variety of environmental
14 sensors. This library provides a general software solution, without requiring
15  ======================== Arduino SDI-12 =================================*/
16 
17 #ifndef SRC_SDI12_BOARDS_H_
18 #define SRC_SDI12_BOARDS_H_
19 
20 #include <Arduino.h>
21 
22 #if defined(ESP32) || defined(ESP8266)
24 typedef uint32_t sdi12timer_t;
25 #else
27 typedef uint8_t sdi12timer_t;
28 #endif
29 
33 class SDI12Timer {
34  public:
38  SDI12Timer();
60 
61 // Most 'standard' AVR boards
62 //
63 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || \
64  defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || \
65  defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644__) || \
66  defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega1284__)
67 
71 #define TIMER_IN_USE_STR "TCNT1"
75 #define TCNTX TCNT1 // Using Timer 1
76 
77 #if F_CPU == 16000000L
81 #define PRESCALE_IN_USE_STR "1024"
89 #define TICKS_PER_BIT 13
95 #define BITS_PER_TICK_Q10 79
102 #define RX_WINDOW_FUDGE 2
103 
104 #elif F_CPU == 12000000L
108 #define PRESCALE_IN_USE_STR "1024"
116 #define TICKS_PER_BIT 10
122 #define BITS_PER_TICK_Q10 105
129 #define RX_WINDOW_FUDGE 2
130 
131 #elif F_CPU == 8000000L
135 #define PRESCALE_IN_USE_STR "256"
143 #define TICKS_PER_BIT 26
149 #define BITS_PER_TICK_Q10 39
156 #define RX_WINDOW_FUDGE 10
157 
158  // #define PRESCALE_IN_USE_STR "1024"
159  // #define TICKS_PER_BIT 6
160  // // 8MHz / 1024 prescaler = 31250 'ticks'/sec = 128 µs / 'tick'
161  // // (1 sec/1200 bits) * (1 tick/128 µs) = 6.5104166667 ticks/bit
162  // #define BITS_PER_TICK_Q10 157
163  // // 1/(6.5104166667 ticks/bit) * 2^10 = 157.2864
164  // #define RX_WINDOW_FUDGE 5
165 
166 #endif
167 
168 
169 // ATtiny boards (ie, adafruit trinket)
170 //
171 #elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
172 
176 #define TIMER_IN_USE_STR "TCNT1"
180 #define TCNTX TCNT1 // Using Timer 1
181 
182 #if F_CPU == 16000000L
186 #define PRESCALE_IN_USE_STR "1024"
194 #define TICKS_PER_BIT 13
200 #define BITS_PER_TICK_Q10 79
207 #define RX_WINDOW_FUDGE 2
208 
209 #elif F_CPU == 8000000L
210 #define PRESCALE_IN_USE_STR "512"
218 #define TICKS_PER_BIT 13
224 #define BITS_PER_TICK_Q10 79
231 #define RX_WINDOW_FUDGE 5
232 
233 #endif
234 
235 
236 // Arduino Leonardo & Yun and other 32U4 boards
237 //
238 #elif defined(ARDUINO_AVR_YUN) || defined(ARDUINO_AVR_LEONARDO) || \
239  defined(__AVR_ATmega32U4__)
240 
244 #define TIMER_IN_USE_STR "TCNT4"
248 #define TCNTX TCNT4 // Using Timer 4
249 
250 #if F_CPU == 16000000L
254 #define PRESCALE_IN_USE_STR "1024"
262 #define TICKS_PER_BIT 13
268 #define BITS_PER_TICK_Q10 79
275 #define RX_WINDOW_FUDGE 2
276 
277 #elif F_CPU == 8000000L
281 #define PRESCALE_IN_USE_STR "512"
289 #define TICKS_PER_BIT 13
295 #define BITS_PER_TICK_Q10 79
302 #define RX_WINDOW_FUDGE 5
303 
304 #endif
305 
306 
307 // Arduino Zero other SAMD21 boards
308 //
309 #elif defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_ARCH_SAMD) || \
310  defined(__SAMD21G18A__) || defined(__SAMD21J18A__) || defined(__SAMD21E18A__)
311 
315 #define TIMER_IN_USE_STR "GCLK4-TC3"
319 #define TCNTX REG_TC3_COUNT8_COUNT // Using Timer 3 with generic clock 4
320 
324 #define PRESCALE_IN_USE_STR "3x1024"
333 #define TICKS_PER_BIT 13
339 #define BITS_PER_TICK_Q10 79
346 #define RX_WINDOW_FUDGE 2
347 
348 // Espressif ESP32/ESP8266 boards
349 //
350 #elif defined(ESP32) || defined(ESP8266)
364  sdi12timer_t SDI12TimerRead(void);
365 
374 #define TICKS_PER_BIT 13
380 #define BITS_PER_TICK_Q10 79
387 #define RX_WINDOW_FUDGE 2
388 
389 // Unknown board
390 #else
391 #error "Please define your board timer and pins"
392 #endif
393 };
394 
395 #endif // SRC_SDI12_BOARDS_H_
uint8_t sdi12timer_t
Definition: SDI12_boards.h:27
The class used to define the processor timer for the SDI-12 serial emulation.
Definition: SDI12_boards.h:33
void resetSDI12TimerPrescale(void)
Reset the processor timer prescaler to whatever it was prior to being adjusted for this library.
void configSDI12TimerPrescale(void)
Set the processor timer prescaler such that the 10 bits of an SDI-12 character are divided into the r...
SDI12Timer()
Construct a new SDI12Timer.
Definition: SDI12_boards.cpp:21