@@ -37,9 +37,10 @@ SevSeg myDisplay; //Create an instance of the object
37
37
#define OPENSEGMENT 2
38
38
#define DISPLAY_TYPE OPENSEGMENT
39
39
40
- // Global variables for the analog pins
41
- unsigned int analogValue6 = 0 ;
40
+ // Global variables
41
+ unsigned int analogValue6 = 0 ; // These are used in analog meter mode
42
42
unsigned int analogValue7 = 0 ;
43
+ char deviceMode; // This variable is useds to select which mode the device should be in
43
44
44
45
// Struct for circular data buffer data received over UART, SPI and I2C are all sent into a single buffer
45
46
struct dataBuffer
@@ -103,6 +104,7 @@ void setup()
103
104
setupSPI (); // Initialize SPI stuff (enable, mode, interrupts)
104
105
setupTWI (); // Initialize I2C stuff (address, interrupt, enable)
105
106
setupAnalog (); // Initialize the analog inputs
107
+ setupMode (); // Determine which mode we should be in
106
108
107
109
interrupts (); // Turn interrupts on, and les' go
108
110
@@ -111,17 +113,128 @@ void setup()
111
113
display.digits [1 ] = 2 ;
112
114
display.digits [2 ] = 3 ;
113
115
display.digits [3 ] = 4 ;
114
-
116
+
115
117
myDisplay.SetBrightness (100 ); // Set the display to 100% bright
116
118
}
117
119
118
120
// The display is constantly PWM'd in the loop()
119
121
void loop ()
120
122
{
121
- myDisplay.DisplayString (display.digits , display.decimals ); // (numberToDisplay, decimal point location)
123
+ if (deviceMode == MODE_DATA)
124
+ {
125
+ // Go into normal mode, monitoring UART/SPI/I2C
126
+ while (deviceMode == MODE_DATA)
127
+ {
128
+ // Just hang out and update the display as new data comes in
129
+ myDisplay.DisplayString (display.digits , display.decimals ); // (numberToDisplay, decimal point location)
130
+
131
+ serialEvent (); // Check the serial buffer for new data
132
+ }
133
+ }
134
+ else if (deviceMode == MODE_COUNTER)
135
+ {
136
+ // Turn off the SPI and watch for increment pulses on the SDO pin, decrement on SDI
137
+ SPCR = 0 ; // Disable all SPI interrupts that may be turned on
138
+
139
+ int counterIncrement = SPI_MISO; // Labeled SDO
140
+ int counterDecrement = SPI_MOSI; // Labeled SDI
141
+
142
+ pinMode (counterIncrement, INPUT_PULLUP);
143
+ pinMode (counterDecrement, INPUT_PULLUP);
144
+
145
+ int counter = 0 ; // Watches the overall count
146
+ boolean incrementCounted = false ; // Watches the toggle the counter pins
147
+ boolean decrementCounted = false ;
148
+
149
+ while (deviceMode == MODE_COUNTER) // Loop until we receive a different mode command
150
+ {
151
+ // Check to see if there has been a low/high pulse on increment
152
+ if (digitalRead (counterIncrement == LOW))
153
+ {
154
+ if (incrementCounted == false ) // Only increment counter if this is a new pulse
155
+ {
156
+ counter++;
157
+ incrementCounted = true ; // We have now counted this pulse
158
+ }
159
+ }
160
+ else
161
+ {
162
+ // The increment pin is high, so sdo can be counted again
163
+ incrementCounted = false ;
164
+ }
165
+
166
+ // Check to see if there has been a low/high pulse on increment
167
+ if (digitalRead (counterDecrement == LOW))
168
+ {
169
+ if (decrementCounted == false ) // Only increment counter if this is a new pulse
170
+ {
171
+ counter--;
172
+ decrementCounted = true ; // We have now counted this pulse
173
+ }
174
+ }
175
+ else
176
+ {
177
+ // The increment pin is high, so sdo can be counted again
178
+ decrementCounted = false ;
179
+ }
180
+
181
+ // Display this count
182
+ // char tempString[10]; //Used for sprintf
183
+ sprintf (display.digits , " %4d" , counter); // Convert counter into a string that is right adjusted
184
+
185
+
186
+ // int tempCounter = counter;
187
+ // for(int x = 0 ; x < 4 ; x++)
188
+ // {
189
+ // display.digits[3 - x] = (tempCounter % 10); //Pull off the right most digit and store in display array
190
+ // tempCounter /= 10; //Shave number down by one digit
191
+ // }
192
+
193
+ myDisplay.DisplayString (display.digits , 0 ); // (numberToDisplay, no decimals during counter mode)
194
+
195
+ serialEvent (); // Check the serial buffer for new data
196
+ }
197
+
198
+ }
199
+ else if (deviceMode == MODE_ANALOG)
200
+ {
201
+ // Do nothing but analog reads
202
+
203
+ while (deviceMode == MODE_ANALOG)
204
+ {
205
+ analogValue6 = analogRead (A6);
206
+ analogValue7 = analogRead (A7);
207
+
208
+ // Serial.print("A6: ");
209
+ // Serial.print(analogValue6);
210
+ // Serial.print(" A7: ");
211
+ // Serial.print(analogValue7);
212
+
213
+ // Do calculation for 1st voltage meter
214
+ float fvoltage6 = ((analogValue6 * 50 ) / (float )1024 );
215
+ int voltage6 = round (fvoltage6);
216
+ display.digits [0 ] = voltage6 / 10 ;
217
+ display.digits [1 ] = voltage6 % 10 ;
218
+
219
+ // Do calculation for 2nd voltage meter
220
+ float fvoltage7 = ((analogValue7 * 50 ) / (float )1024 );
221
+ int voltage7 = round (fvoltage7);
222
+ display.digits [2 ] = voltage7 / 10 ;
223
+ display.digits [3 ] = voltage7 % 10 ;
224
+
225
+ display.decimals = ((1 <<DECIMAL1) | (1 <<DECIMAL3)); // Turn on the decimals next to digit1 and digit3
226
+ myDisplay.DisplayString (display.digits , display.decimals ); // (numberToDisplay, decimal point location)
227
+
228
+ serialEvent (); // Check the serial buffer for new data
229
+ }
230
+
231
+ }
232
+
233
+ // We will loop if we've received a new device mode command
122
234
}
123
235
124
236
// This is effectively the UART0 byte received interrupt routine
237
+ // But not quite: serialEvent is only called after each loop() interation
125
238
void serialEvent ()
126
239
{
127
240
while (Serial.available ())
@@ -192,15 +305,19 @@ void updateBufferData()
192
305
break ;
193
306
case BAUD_CMD: // Baud setting mode
194
307
EEPROM.write (BAUD_ADDRESS, c); // Update EEPROM with new baud setting
195
- setupUART (); // Checks to see if this baud rate is valis and turns on UART at this speed
308
+ setupUART (); // Checks to see if this baud rate is valid and turns on UART at this speed
196
309
break ;
197
310
case CURSOR_CMD: // Set the cursor
198
311
if (c <= 3 ) // Limited error checking, if >3 cursor command will have no effect
199
312
display.cursor = c; // Update the cursor value
200
313
break ;
201
314
case TWI_ADDRESS_CMD: // Set the I2C Address
202
315
EEPROM.write (TWI_ADDRESS_ADDRESS, c); // Update the EEPROM value
203
- setupTWI (); // Checks to see if I2C address is valid an begins I2C
316
+ setupTWI (); // Checks to see if I2C address is valid and begins I2C
317
+ break ;
318
+ case MODE_CMD: // Set the device mode (example: data, analog, counter)
319
+ EEPROM.write (MODE_ADDRESS, c); // Update the EEPROM value
320
+ setupMode (); // Checks to see if this mode is valid and then enters new mode
204
321
break ;
205
322
case FACTORY_RESET_CMD: // Factory reset
206
323
setDefaultSettings (); // Reset baud, brightness, and TWI address
@@ -321,11 +438,11 @@ void setupDisplay()
321
438
322
439
// Initialize the SevSeg library with all the pins needed for this type of display
323
440
myDisplay.Begin (displayType, numberOfDigits,
324
- digit1, digit2, digit3, digit4,
325
- digitColon, digitApostrophe,
326
- segA, segB, segC, segD, segE, segF, segG,
327
- segDP,
328
- segmentColon, segmentApostrophe);
441
+ digit1, digit2, digit3, digit4,
442
+ digitColon, digitApostrophe,
443
+ segA, segB, segC, segD, segE, segF, segG,
444
+ segDP,
445
+ segmentColon, segmentApostrophe);
329
446
#endif
330
447
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
331
448
}
@@ -389,6 +506,20 @@ void setupUART()
389
506
390
507
}
391
508
509
+ // This function reads the MODE setting from EEPROM and checks to see if there are
510
+ // any hardware settings (closed jumpers for example) that puts the device into a
511
+ // certain mode. Available modes are regular, analog meter, and counter modes.
512
+ void setupMode ()
513
+ {
514
+ deviceMode = EEPROM.read (MODE_ADDRESS); // Read the mode the device should be in
515
+
516
+ if (deviceMode > MODE_COUNTER)
517
+ { // If the mode is invalid, goto default mode
518
+ deviceMode = MODE_DEFAULT;
519
+ EEPROM.write (MODE_ADDRESS, MODE_DEFAULT);
520
+ }
521
+ }
522
+
392
523
// This sets up the two analog inputs
393
524
void setupAnalog ()
394
525
{
@@ -465,7 +596,7 @@ void checkEmergencyReset(void)
465
596
constantDisplay (" 0-00" , 500 );
466
597
constantDisplay (" -000" , 500 );
467
598
}
468
-
599
+
469
600
// Once we breakout of this loop (pin on RX is removed), system will init with new default settings
470
601
}
471
602
@@ -478,7 +609,7 @@ void constantDisplay (char *theString, long amountOfTime)
478
609
}
479
610
480
611
// In case of emergency, resets all the system settings to safe values
481
- // This will reset baud, TWI address, and brightness to default values
612
+ // This will reset baud, TWI address, brightness, and mode to default values
482
613
void setDefaultSettings (void )
483
614
{
484
615
// Reset UART to 9600bps
@@ -490,5 +621,13 @@ void setDefaultSettings(void)
490
621
491
622
// Reset the I2C address to the default 0x71
492
623
EEPROM.write (TWI_ADDRESS_ADDRESS, TWI_ADDRESS_DEFAULT);
624
+
625
+ // Reset the mode to the default data interface
626
+ EEPROM.write (MODE_ADDRESS, MODE_DEFAULT);
627
+ deviceMode = MODE_DEFAULT; // Return device's mode to default
493
628
}
494
629
630
+
631
+
632
+
633
+
0 commit comments