Charles Young's development fork. Going forward I only want to push mature code to main repository.

Dependencies:   mbed

Fork of GEO_COUNTER_L432KC by Geo Electronics "Geo Counter"

Committer:
Charles David Young
Date:
Thu Sep 06 05:37:59 2018 -0700
Revision:
34:10550b327e3d
Parent:
33:35e3ab4d5ba7
Child:
37:6f7bb913e08b
Implemented modes switch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
walter76 2:ac0ed3d84d44 1 // GEO COUNTER V1 firmware
walter76 2:ac0ed3d84d44 2 // This FW provides a basic operation of GEO-COUNTER
walter76 2:ac0ed3d84d44 3 //
walter76 2:ac0ed3d84d44 4 // Latest review: August 27, 2018 - Walter Trovo
walter76 1:75827d765e34 5 //
Charles David Young 25:0161bf1adc81 6 // Sep 5, 2018: Charles Young: Created LED7segDisplay class
charlesdavidyoung 24:db7494389c03 7 // Sep 5, 2018: Charles Young: Still developing mode selection. LEDs turn off
charlesdavidyoung 24:db7494389c03 8 // when not in mode selection. Pressing once enters mode selection.
charlesdavidyoung 24:db7494389c03 9 // Pressing again enters submode. Temporarily the rotary switch
charlesdavidyoung 24:db7494389c03 10 // adjusts the display brightness. This is just for testing.
charlesdavidyoung 24:db7494389c03 11 // Sep 4, 2018: Charles Young: Created RotarySwitch class to manage mode selection
walter76 1:75827d765e34 12 // Feb 14, 2018: initial release aimed to test the counters, the serial port
walter76 1:75827d765e34 13 // the PWM output and the MAX7219 operation.
walter76 1:75827d765e34 14 // Feb 15, 2018: Removed MAX7219 libray (replaced with custom routine).
walter76 1:75827d765e34 15 // Added 74HC595 routine. Added beep. Added Gate_write
walter76 1:75827d765e34 16 //
walter76 2:ac0ed3d84d44 17
walter76 1:75827d765e34 18
walter76 1:75827d765e34 19 // this block includes key libraries
walter76 1:75827d765e34 20 #include <string> // strings management
charlesdavidyoung 24:db7494389c03 21 #include "RotarySwitch.hpp"
Charles David Young 25:0161bf1adc81 22 #include "LED7segDisplay.hpp"
charlesdavidyoung 20:fb73eaaf0894 23
charlesdavidyoung 24:db7494389c03 24 // Everything associated with the rotary switch and the associated LEDs
charlesdavidyoung 24:db7494389c03 25 // is hidden in this class.
charlesdavidyoung 20:fb73eaaf0894 26 RotarySwitch ModeSwitch;
Charles David Young 25:0161bf1adc81 27 LED7segDisplay DigitsDisplay;
walter76 1:75827d765e34 28
Charles David Young 34:10550b327e3d 29 enum Modes {
Charles David Young 34:10550b327e3d 30 CNT1,
Charles David Young 34:10550b327e3d 31 CNT2,
Charles David Young 34:10550b327e3d 32 PROSPECT,
Charles David Young 34:10550b327e3d 33 VOLTS,
Charles David Young 34:10550b327e3d 34 NULL1,
Charles David Young 34:10550b327e3d 35 NULL1,
Charles David Young 34:10550b327e3d 36 NULL3,
Charles David Young 34:10550b327e3d 37 DIM,
Charles David Young 34:10550b327e3d 38 NumberOfModes
Charles David Young 34:10550b327e3d 39 };
Charles David Young 34:10550b327e3d 40 uint8_t currentMode = CNT1;
Charles David Young 34:10550b327e3d 41
charlesdavidyoung 5:4f90b458dbdf 42 // LED on processor board
charlesdavidyoung 5:4f90b458dbdf 43 DigitalOut led1(LED1);
walter76 1:75827d765e34 44
walter76 1:75827d765e34 45 // definitions of peripherals and devices
Charles David Young 7:9f975e00600c 46
charlesdavidyoung 17:6eed17197004 47 I2C i2c(D4, D5); // I2C port
charlesdavidyoung 17:6eed17197004 48 Ticker SecTenth_Beat; // .1 second ticker
walter76 1:75827d765e34 49 Ticker Sec_Beat; // 1 second ticker
walter76 1:75827d765e34 50 Serial PC(USBTX, USBRX); // Virtual COM via USB (PC connection)
walter76 1:75827d765e34 51 Serial GPS(D1, D0); // Serial port for GPS module
walter76 0:6d1742703713 52
walter76 0:6d1742703713 53 // Global variables
walter76 1:75827d765e34 54 time_t seconds; // Real-Time Clock (RTC) timestamp
walter76 1:75827d765e34 55 unsigned int value = 0; // displayed value on the 6-digits of the display
walter76 1:75827d765e34 56 uint32_t Count1, Count2; // pulse counters (32-bit)
walter76 1:75827d765e34 57 char Text[40]=""; // used to send messages over the serial port
walter76 1:75827d765e34 58 uint8_t Disp_mode = 0x01, Disp_unit = 0xA0; // status of 1st row and 2nd rows of LEDs
kd5byb 4:b17c6556cf1f 59 bool Stopped = 0; // status of counting activity
charlesdavidyoung 5:4f90b458dbdf 60 bool StartStopPressed = 0;// status of counting activity
walter76 1:75827d765e34 61 double ADC_val; // used to read ADC value
charlesdavidyoung 24:db7494389c03 62 float direction = 0;
walter76 0:6d1742703713 63
walter76 1:75827d765e34 64 // ----- Prototypes of routines (defined below the main) -------------------
Charles David Young 16:b2e77eb76ab4 65 void UpdateInput(void); // periodically called by the ticker
Charles David Young 16:b2e77eb76ab4 66 void UpdateOutput(void); // periodically called by the ticker
walter76 1:75827d765e34 67 void Count1_up(void); // called every time an edge is detected on TRIG1 pin
walter76 1:75827d765e34 68 void Count2_up(void); // called every time an edge is detected on TRIG2 pin
walter76 1:75827d765e34 69 void Beep(void); // used to generate a short beep (buzzer)
walter76 0:6d1742703713 70
walter76 0:6d1742703713 71 //==============================================================================
walter76 0:6d1742703713 72 //==============================================================================
walter76 0:6d1742703713 73
walter76 0:6d1742703713 74 int main()
walter76 0:6d1742703713 75 {
walter76 0:6d1742703713 76
Charles David Young 28:c81d00ec28a3 77 PC.baud(115200); // set baud-rate of virtual COM port (PC connection)
Charles David Young 28:c81d00ec28a3 78 PC.printf("\nGEO COUNTER V1 2108");
Charles David Young 28:c81d00ec28a3 79 PC.printf(__DATE__);
Charles David Young 28:c81d00ec28a3 80 PC.printf(" ");
Charles David Young 28:c81d00ec28a3 81 PC.printf(__TIME__);
walter76 1:75827d765e34 82
Charles David Young 28:c81d00ec28a3 83 GPS.baud(9600); // set the baud-rate of the serial port dedicated to the GPS
walter76 0:6d1742703713 84
Charles David Young 28:c81d00ec28a3 85 CS1 = 1; // presets CS of MAX7219
Charles David Young 28:c81d00ec28a3 86 CS2 = 1; // preset CS of 74HC595
walter76 1:75827d765e34 87
Charles David Young 28:c81d00ec28a3 88 DigitsDisplay.Display_6D_write(0x543210);
Charles David Young 28:c81d00ec28a3 89 DigitsDisplay.Display_2D_write(0);
walter76 0:6d1742703713 90
Charles David Young 28:c81d00ec28a3 91 // RTC is supposed to be loose time at power down (no backup battery)
Charles David Young 28:c81d00ec28a3 92 // An initialization is performed anyway
Charles David Young 28:c81d00ec28a3 93 set_time(0); // Set time
charlesdavidyoung 24:db7494389c03 94
Charles David Young 28:c81d00ec28a3 95 PWM.period_ms(3); // set the PWM period
Charles David Young 28:c81d00ec28a3 96 PWM.write(0.8); // set the PWM duty-cycle
walter76 1:75827d765e34 97
Charles David Young 28:c81d00ec28a3 98 Beep(); // initial beep
walter76 1:75827d765e34 99
Charles David Young 28:c81d00ec28a3 100 // set the 1 sec ticker to periodically call the Update() routine
Charles David Young 28:c81d00ec28a3 101 // NOTE: this is also the 1-sec time base for counters. A better approach
Charles David Young 28:c81d00ec28a3 102 // would replace the ticker with an interrupt from the RTC (to be implemented)
Charles David Young 28:c81d00ec28a3 103 SecTenth_Beat.attach_us(&UpdateInput, 100000);
Charles David Young 28:c81d00ec28a3 104 Sec_Beat.attach_us(&UpdateOutput, 1000000);
Charles David Young 28:c81d00ec28a3 105 //RTC::attach(&Update, RTC::Second);
Charles David Young 28:c81d00ec28a3 106 //RTC::detach(RTC::Second);
walter76 1:75827d765e34 107
Charles David Young 28:c81d00ec28a3 108 // main loop does nothing as all activities are interrupt driven
Charles David Young 28:c81d00ec28a3 109 while(1)
Charles David Young 28:c81d00ec28a3 110 {
Charles David Young 28:c81d00ec28a3 111 // dance (or drink a beer)
Charles David Young 28:c81d00ec28a3 112 }
walter76 0:6d1742703713 113 }
walter76 0:6d1742703713 114
walter76 0:6d1742703713 115
walter76 1:75827d765e34 116 //-------- END OF MAIN --------------
walter76 0:6d1742703713 117 //==============================================================================
walter76 0:6d1742703713 118
walter76 1:75827d765e34 119 // Definition of routines
walter76 1:75827d765e34 120
walter76 1:75827d765e34 121 //---------------------------------------------------------------------------
walter76 1:75827d765e34 122 // Update values to be displayed
Charles David Young 10:f48cc6be5ae8 123 void logToPC()
Charles David Young 10:f48cc6be5ae8 124 {
Charles David Young 10:f48cc6be5ae8 125 PC.printf("\nADC: %.02f", ADC_val);
Charles David Young 10:f48cc6be5ae8 126 PC.printf(Stopped ? " stopped" : " started");
Charles David Young 10:f48cc6be5ae8 127 // Timestamp to PC (debug)
Charles David Young 10:f48cc6be5ae8 128 seconds = time(NULL); // get current time
Charles David Young 10:f48cc6be5ae8 129 strftime(Text, 50, "%H:%M:%S", localtime(&seconds));
Charles David Young 10:f48cc6be5ae8 130 PC.printf(" RTC: %s, CNT1: %7d CNT2: %7d",Text, Count1, Count2);
charlesdavidyoung 24:db7494389c03 131 PC.printf(" wheel %f", direction);
Charles David Young 10:f48cc6be5ae8 132 }
walter76 1:75827d765e34 133
Charles David Young 16:b2e77eb76ab4 134 void UpdateOutput()
charlesdavidyoung 18:ef5678ab1889 135 {
Charles David Young 28:c81d00ec28a3 136 // This must be called periodically to update the LEDs
Charles David Young 28:c81d00ec28a3 137 ModeSwitch.UpdateOutput();
charlesdavidyoung 24:db7494389c03 138
Charles David Young 34:10550b327e3d 139 switch (currentMode) {
Charles David Young 34:10550b327e3d 140 case CNT1:
Charles David Young 34:10550b327e3d 141 DigitsDisplay.Display_6D_write(Count1);
Charles David Young 34:10550b327e3d 142 DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 143 break;
Charles David Young 34:10550b327e3d 144 case CNT2:
Charles David Young 34:10550b327e3d 145 DigitsDisplay.Display_6D_write(Count2);
Charles David Young 34:10550b327e3d 146 DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 147 break;
Charles David Young 34:10550b327e3d 148 case PROSPECT:
Charles David Young 34:10550b327e3d 149 DigitsDisplay.Display_6D_write(Count1);
Charles David Young 34:10550b327e3d 150 DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 151 break;
Charles David Young 34:10550b327e3d 152 case VOLTS:
Charles David Young 34:10550b327e3d 153 DigitsDisplay.Display_6D_write(0);
Charles David Young 34:10550b327e3d 154 DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 155 break;
Charles David Young 34:10550b327e3d 156 case NULL1:
Charles David Young 34:10550b327e3d 157 DigitsDisplay.Display_6D_write(0);
Charles David Young 34:10550b327e3d 158 DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 159 break;
Charles David Young 34:10550b327e3d 160 case NULL1:
Charles David Young 34:10550b327e3d 161 DigitsDisplay.Display_6D_write(0);
Charles David Young 34:10550b327e3d 162 DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 163 break;
Charles David Young 34:10550b327e3d 164 case NULL3:
Charles David Young 34:10550b327e3d 165 DigitsDisplay.Display_6D_write(0);
Charles David Young 34:10550b327e3d 166 DigitsDisplay.Display_2D_write(0);
Charles David Young 34:10550b327e3d 167 break;
Charles David Young 34:10550b327e3d 168 case DIM:
Charles David Young 34:10550b327e3d 169 if (direction > 0)
Charles David Young 34:10550b327e3d 170 {
Charles David Young 34:10550b327e3d 171 DigitsDisplay.Display_brightness_up();
Charles David Young 34:10550b327e3d 172 }
Charles David Young 34:10550b327e3d 173 else
Charles David Young 34:10550b327e3d 174 if (direction < 0)
Charles David Young 34:10550b327e3d 175 {
Charles David Young 34:10550b327e3d 176 DigitsDisplay.Display_brightness_down();
Charles David Young 34:10550b327e3d 177 }
Charles David Young 34:10550b327e3d 178 DigitsDisplay.Display_2D_write(DigitsDisplay.GetBrightness());
Charles David Young 34:10550b327e3d 179 break;
Charles David Young 34:10550b327e3d 180 default:
Charles David Young 34:10550b327e3d 181 break;
Charles David Young 34:10550b327e3d 182 }
Charles David Young 34:10550b327e3d 183
Charles David Young 16:b2e77eb76ab4 184 if(Stopped)
Charles David Young 16:b2e77eb76ab4 185 {
Charles David Young 16:b2e77eb76ab4 186 // disable interrupts on TRIG1 and TRIG2
Charles David Young 16:b2e77eb76ab4 187 TRIG1.rise(NULL);
Charles David Young 16:b2e77eb76ab4 188 TRIG2.rise(NULL);
Charles David Young 16:b2e77eb76ab4 189 }
Charles David Young 16:b2e77eb76ab4 190 else
Charles David Young 16:b2e77eb76ab4 191 {
Charles David Young 34:10550b327e3d 192 // Enable interrupts on rising edge of digital inputs TRIG1 & TRIG2
Charles David Young 34:10550b327e3d 193 TRIG1.rise(&Count1_up);
Charles David Young 34:10550b327e3d 194 TRIG2.rise(&Count2_up);
Charles David Young 7:9f975e00600c 195
Charles David Young 28:c81d00ec28a3 196 Count1 = 0; // clear both counters
Charles David Young 28:c81d00ec28a3 197 Count2 = 0;
Charles David Young 16:b2e77eb76ab4 198 }
Charles David Young 16:b2e77eb76ab4 199 }
Charles David Young 16:b2e77eb76ab4 200
Charles David Young 16:b2e77eb76ab4 201 void UpdateInput()
charlesdavidyoung 24:db7494389c03 202 {
Charles David Young 33:35e3ab4d5ba7 203 DigitsDisplay.Display_2D_write(Count1); // Just to test 2D
Charles David Young 33:35e3ab4d5ba7 204
charlesdavidyoung 24:db7494389c03 205 // This must be called periodically to monitor switch input
charlesdavidyoung 24:db7494389c03 206 direction = ModeSwitch.UpdateInput();
Charles David Young 34:10550b327e3d 207 currentMode = ModeSwitch.GetPosition();
Charles David Young 34:10550b327e3d 208
Charles David Young 34:10550b327e3d 209 switch (currentMode) {
Charles David Young 34:10550b327e3d 210 case CNT1:
Charles David Young 34:10550b327e3d 211 break;
Charles David Young 34:10550b327e3d 212 case CNT2:
Charles David Young 34:10550b327e3d 213 break;
Charles David Young 34:10550b327e3d 214 case PROSPECT:
Charles David Young 34:10550b327e3d 215 break;
Charles David Young 34:10550b327e3d 216 case VOLTS:
Charles David Young 34:10550b327e3d 217 break;
Charles David Young 34:10550b327e3d 218 case NULL1:
Charles David Young 34:10550b327e3d 219 break;
Charles David Young 34:10550b327e3d 220 case NULL1:
Charles David Young 34:10550b327e3d 221 break;
Charles David Young 34:10550b327e3d 222 case NULL3:
Charles David Young 34:10550b327e3d 223 break;
Charles David Young 34:10550b327e3d 224 case DIM:
Charles David Young 34:10550b327e3d 225 if (direction > 0)
Charles David Young 34:10550b327e3d 226 {
Charles David Young 34:10550b327e3d 227 DigitsDisplay.Display_brightness_up();
Charles David Young 34:10550b327e3d 228 }
Charles David Young 34:10550b327e3d 229 else
Charles David Young 34:10550b327e3d 230 if (direction < 0)
Charles David Young 34:10550b327e3d 231 {
Charles David Young 34:10550b327e3d 232 DigitsDisplay.Display_brightness_down();
Charles David Young 34:10550b327e3d 233 }
Charles David Young 34:10550b327e3d 234 break;
Charles David Young 34:10550b327e3d 235 default:
Charles David Young 34:10550b327e3d 236 break;
charlesdavidyoung 24:db7494389c03 237 }
Charles David Young 34:10550b327e3d 238
Charles David Young 16:b2e77eb76ab4 239 ADC_val = KEYB.read(); // read voltage from keyboard
Charles David Young 16:b2e77eb76ab4 240 if ( (ADC_val<0.1) // START/STOP pushbutton pressed
Charles David Young 16:b2e77eb76ab4 241 && (!StartStopPressed))
Charles David Young 16:b2e77eb76ab4 242 {
Charles David Young 16:b2e77eb76ab4 243 StartStopPressed = true;
Charles David Young 16:b2e77eb76ab4 244 Stopped=!Stopped; // toggle status
Charles David Young 16:b2e77eb76ab4 245 }
Charles David Young 16:b2e77eb76ab4 246 else
Charles David Young 16:b2e77eb76ab4 247 StartStopPressed = false;
charlesdavidyoung 5:4f90b458dbdf 248
Charles David Young 16:b2e77eb76ab4 249 if((ADC_val>0.6)&&(ADC_val<0.7)) // CLEAR pushbutton pressed
Charles David Young 16:b2e77eb76ab4 250 {
Charles David Young 16:b2e77eb76ab4 251 Count1 = 0; // clear counters
Charles David Young 16:b2e77eb76ab4 252 Count2 = 0;
charlesdavidyoung 5:4f90b458dbdf 253 }
Charles David Young 16:b2e77eb76ab4 254 logToPC();
Charles David Young 16:b2e77eb76ab4 255 return;
Charles David Young 16:b2e77eb76ab4 256 }
walter76 1:75827d765e34 257
walter76 0:6d1742703713 258 //---------------------------------------------------------------------------
walter76 1:75827d765e34 259 // Increment CNT1 every time a rising edge is detected on TRIG1 (interrupt)
walter76 1:75827d765e34 260
walter76 1:75827d765e34 261 void Count1_up(void)
walter76 1:75827d765e34 262 {
Charles David Young 33:35e3ab4d5ba7 263 //Beep();
Charles David Young 28:c81d00ec28a3 264 Count1++;
Charles David Young 28:c81d00ec28a3 265 return;
walter76 1:75827d765e34 266 }
walter76 1:75827d765e34 267
walter76 1:75827d765e34 268 //---------------------------------------------------------------------------
walter76 1:75827d765e34 269 // Increment CNT1 every time a rising edge is detected on TRIG2 (interrupt)
walter76 1:75827d765e34 270
walter76 1:75827d765e34 271 void Count2_up(void)
walter76 1:75827d765e34 272 {
Charles David Young 33:35e3ab4d5ba7 273 //Beep();
Charles David Young 28:c81d00ec28a3 274 Count2++;
Charles David Young 28:c81d00ec28a3 275 return;
walter76 1:75827d765e34 276 }
walter76 1:75827d765e34 277
walter76 1:75827d765e34 278
walter76 1:75827d765e34 279 //---------------------------------------------------------------------------
walter76 1:75827d765e34 280 //Generates a short beep via BUZZ
walter76 1:75827d765e34 281
walter76 1:75827d765e34 282 void Beep(void)
walter76 1:75827d765e34 283 {
Charles David Young 28:c81d00ec28a3 284 BUZZ = 1; // turn-on the buzzer
Charles David Young 33:35e3ab4d5ba7 285 wait_us(100); // wait
Charles David Young 28:c81d00ec28a3 286 BUZZ = 0; // turn-off the buzzer
Charles David Young 28:c81d00ec28a3 287 return;
walter76 0:6d1742703713 288 }
walter76 0:6d1742703713 289
walter76 1:75827d765e34 290 //-------- END OF FILE --------------
walter76 1:75827d765e34 291 //==============================================================================