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:
Wed Sep 05 13:15:19 2018 -0700
Revision:
32:12f725e0235e
Parent:
31:f162f9b02b6a
Child:
33:35e3ab4d5ba7
Beep try wait_us

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
charlesdavidyoung 5:4f90b458dbdf 29 // LED on processor board
charlesdavidyoung 5:4f90b458dbdf 30 DigitalOut led1(LED1);
walter76 1:75827d765e34 31
walter76 1:75827d765e34 32 // definitions of peripherals and devices
Charles David Young 7:9f975e00600c 33
charlesdavidyoung 17:6eed17197004 34 I2C i2c(D4, D5); // I2C port
charlesdavidyoung 17:6eed17197004 35 Ticker SecTenth_Beat; // .1 second ticker
walter76 1:75827d765e34 36 Ticker Sec_Beat; // 1 second ticker
walter76 1:75827d765e34 37 Serial PC(USBTX, USBRX); // Virtual COM via USB (PC connection)
walter76 1:75827d765e34 38 Serial GPS(D1, D0); // Serial port for GPS module
walter76 0:6d1742703713 39
walter76 0:6d1742703713 40 // Global variables
walter76 1:75827d765e34 41 time_t seconds; // Real-Time Clock (RTC) timestamp
walter76 1:75827d765e34 42 unsigned int value = 0; // displayed value on the 6-digits of the display
walter76 1:75827d765e34 43 uint32_t Count1, Count2; // pulse counters (32-bit)
walter76 1:75827d765e34 44 char Text[40]=""; // used to send messages over the serial port
walter76 1:75827d765e34 45 uint8_t Disp_mode = 0x01, Disp_unit = 0xA0; // status of 1st row and 2nd rows of LEDs
kd5byb 4:b17c6556cf1f 46 bool Stopped = 0; // status of counting activity
charlesdavidyoung 5:4f90b458dbdf 47 bool StartStopPressed = 0;// status of counting activity
walter76 1:75827d765e34 48 double ADC_val; // used to read ADC value
charlesdavidyoung 24:db7494389c03 49 float direction = 0;
walter76 0:6d1742703713 50
walter76 1:75827d765e34 51 // ----- Prototypes of routines (defined below the main) -------------------
Charles David Young 16:b2e77eb76ab4 52 void UpdateInput(void); // periodically called by the ticker
Charles David Young 16:b2e77eb76ab4 53 void UpdateOutput(void); // periodically called by the ticker
walter76 1:75827d765e34 54 void Count1_up(void); // called every time an edge is detected on TRIG1 pin
walter76 1:75827d765e34 55 void Count2_up(void); // called every time an edge is detected on TRIG2 pin
walter76 1:75827d765e34 56 void Beep(void); // used to generate a short beep (buzzer)
walter76 0:6d1742703713 57
walter76 0:6d1742703713 58 //==============================================================================
walter76 0:6d1742703713 59 //==============================================================================
walter76 0:6d1742703713 60
walter76 0:6d1742703713 61 int main()
walter76 0:6d1742703713 62 {
walter76 0:6d1742703713 63
Charles David Young 28:c81d00ec28a3 64 PC.baud(115200); // set baud-rate of virtual COM port (PC connection)
Charles David Young 28:c81d00ec28a3 65 PC.printf("\nGEO COUNTER V1 2108");
Charles David Young 28:c81d00ec28a3 66 PC.printf(__DATE__);
Charles David Young 28:c81d00ec28a3 67 PC.printf(" ");
Charles David Young 28:c81d00ec28a3 68 PC.printf(__TIME__);
walter76 1:75827d765e34 69
Charles David Young 28:c81d00ec28a3 70 GPS.baud(9600); // set the baud-rate of the serial port dedicated to the GPS
walter76 0:6d1742703713 71
Charles David Young 28:c81d00ec28a3 72 CS1 = 1; // presets CS of MAX7219
Charles David Young 28:c81d00ec28a3 73 CS2 = 1; // preset CS of 74HC595
walter76 1:75827d765e34 74
Charles David Young 28:c81d00ec28a3 75 DigitsDisplay.Display_6D_write(0x543210);
Charles David Young 28:c81d00ec28a3 76 DigitsDisplay.Display_2D_write(0);
walter76 0:6d1742703713 77
Charles David Young 28:c81d00ec28a3 78 // RTC is supposed to be loose time at power down (no backup battery)
Charles David Young 28:c81d00ec28a3 79 // An initialization is performed anyway
Charles David Young 28:c81d00ec28a3 80 set_time(0); // Set time
charlesdavidyoung 24:db7494389c03 81
Charles David Young 28:c81d00ec28a3 82 PWM.period_ms(3); // set the PWM period
Charles David Young 28:c81d00ec28a3 83 PWM.write(0.8); // set the PWM duty-cycle
walter76 1:75827d765e34 84
Charles David Young 28:c81d00ec28a3 85 // Enable interrupts on rising edge of digital inputs TRIG1 & TRIG2
Charles David Young 28:c81d00ec28a3 86 TRIG1.rise(&Count1_up);
Charles David Young 28:c81d00ec28a3 87 TRIG2.rise(&Count2_up);
Charles David Young 28:c81d00ec28a3 88
Charles David Young 28:c81d00ec28a3 89 Beep(); // initial beep
walter76 1:75827d765e34 90
Charles David Young 28:c81d00ec28a3 91 // set the 1 sec ticker to periodically call the Update() routine
Charles David Young 28:c81d00ec28a3 92 // NOTE: this is also the 1-sec time base for counters. A better approach
Charles David Young 28:c81d00ec28a3 93 // would replace the ticker with an interrupt from the RTC (to be implemented)
Charles David Young 28:c81d00ec28a3 94 SecTenth_Beat.attach_us(&UpdateInput, 100000);
Charles David Young 28:c81d00ec28a3 95 Sec_Beat.attach_us(&UpdateOutput, 1000000);
Charles David Young 28:c81d00ec28a3 96 //RTC::attach(&Update, RTC::Second);
Charles David Young 28:c81d00ec28a3 97 //RTC::detach(RTC::Second);
walter76 1:75827d765e34 98
Charles David Young 28:c81d00ec28a3 99 // main loop does nothing as all activities are interrupt driven
Charles David Young 28:c81d00ec28a3 100 while(1)
Charles David Young 28:c81d00ec28a3 101 {
Charles David Young 28:c81d00ec28a3 102 // dance (or drink a beer)
Charles David Young 28:c81d00ec28a3 103 }
walter76 0:6d1742703713 104 }
walter76 0:6d1742703713 105
walter76 0:6d1742703713 106
walter76 1:75827d765e34 107 //-------- END OF MAIN --------------
walter76 0:6d1742703713 108 //==============================================================================
walter76 0:6d1742703713 109
walter76 1:75827d765e34 110 // Definition of routines
walter76 1:75827d765e34 111
walter76 1:75827d765e34 112 //---------------------------------------------------------------------------
walter76 1:75827d765e34 113 // Update values to be displayed
Charles David Young 10:f48cc6be5ae8 114 void logToPC()
Charles David Young 10:f48cc6be5ae8 115 {
Charles David Young 10:f48cc6be5ae8 116 PC.printf("\nADC: %.02f", ADC_val);
Charles David Young 10:f48cc6be5ae8 117 PC.printf(Stopped ? " stopped" : " started");
Charles David Young 10:f48cc6be5ae8 118 // Timestamp to PC (debug)
Charles David Young 10:f48cc6be5ae8 119 seconds = time(NULL); // get current time
Charles David Young 10:f48cc6be5ae8 120 strftime(Text, 50, "%H:%M:%S", localtime(&seconds));
Charles David Young 10:f48cc6be5ae8 121 PC.printf(" RTC: %s, CNT1: %7d CNT2: %7d",Text, Count1, Count2);
charlesdavidyoung 24:db7494389c03 122 PC.printf(" wheel %f", direction);
Charles David Young 10:f48cc6be5ae8 123 }
walter76 1:75827d765e34 124
Charles David Young 16:b2e77eb76ab4 125 void UpdateOutput()
charlesdavidyoung 18:ef5678ab1889 126 {
Charles David Young 28:c81d00ec28a3 127 // This must be called periodically to update the LEDs
Charles David Young 28:c81d00ec28a3 128 ModeSwitch.UpdateOutput();
charlesdavidyoung 24:db7494389c03 129
Charles David Young 16:b2e77eb76ab4 130 if(Stopped)
Charles David Young 16:b2e77eb76ab4 131 {
Charles David Young 16:b2e77eb76ab4 132 // disable interrupts on TRIG1 and TRIG2
Charles David Young 16:b2e77eb76ab4 133 TRIG1.rise(NULL);
Charles David Young 16:b2e77eb76ab4 134 TRIG2.rise(NULL);
Charles David Young 16:b2e77eb76ab4 135
Charles David Young 16:b2e77eb76ab4 136 // show selected content on main display
Charles David Young 25:0161bf1adc81 137 DigitsDisplay.Display_6D_write(Count1); // refresh the main display
Charles David Young 28:c81d00ec28a3 138 DigitsDisplay.Display_2D_write(0);
Charles David Young 16:b2e77eb76ab4 139 }
Charles David Young 16:b2e77eb76ab4 140
Charles David Young 16:b2e77eb76ab4 141 else
Charles David Young 16:b2e77eb76ab4 142 {
Charles David Young 28:c81d00ec28a3 143 DigitsDisplay.Display_6D_write(Count1); // refresh the main display
Charles David Young 28:c81d00ec28a3 144 DigitsDisplay.Display_2D_write(0); // TBD
Charles David Young 7:9f975e00600c 145
Charles David Young 28:c81d00ec28a3 146 Count1 = 0; // clear both counters
Charles David Young 28:c81d00ec28a3 147 Count2 = 0;
Charles David Young 16:b2e77eb76ab4 148 }
Charles David Young 16:b2e77eb76ab4 149 }
Charles David Young 16:b2e77eb76ab4 150
Charles David Young 16:b2e77eb76ab4 151 void UpdateInput()
charlesdavidyoung 24:db7494389c03 152 {
charlesdavidyoung 24:db7494389c03 153 // This must be called periodically to monitor switch input
charlesdavidyoung 24:db7494389c03 154 direction = ModeSwitch.UpdateInput();
Charles David Young 25:0161bf1adc81 155 if (direction > 0)
charlesdavidyoung 24:db7494389c03 156 {
Charles David Young 25:0161bf1adc81 157 DigitsDisplay.Display_brightness_up();
charlesdavidyoung 24:db7494389c03 158 }
charlesdavidyoung 24:db7494389c03 159 else
Charles David Young 25:0161bf1adc81 160 if (direction < 0)
Charles David Young 28:c81d00ec28a3 161 {
Charles David Young 28:c81d00ec28a3 162 DigitsDisplay.Display_brightness_down();
Charles David Young 28:c81d00ec28a3 163 }
charlesdavidyoung 24:db7494389c03 164
Charles David Young 16:b2e77eb76ab4 165 ADC_val = KEYB.read(); // read voltage from keyboard
Charles David Young 16:b2e77eb76ab4 166 if ( (ADC_val<0.1) // START/STOP pushbutton pressed
Charles David Young 16:b2e77eb76ab4 167 && (!StartStopPressed))
Charles David Young 16:b2e77eb76ab4 168 {
Charles David Young 16:b2e77eb76ab4 169 StartStopPressed = true;
Charles David Young 16:b2e77eb76ab4 170 Stopped=!Stopped; // toggle status
Charles David Young 16:b2e77eb76ab4 171 }
Charles David Young 16:b2e77eb76ab4 172 else
Charles David Young 16:b2e77eb76ab4 173 StartStopPressed = false;
charlesdavidyoung 5:4f90b458dbdf 174
Charles David Young 16:b2e77eb76ab4 175 if((ADC_val>0.6)&&(ADC_val<0.7)) // CLEAR pushbutton pressed
Charles David Young 16:b2e77eb76ab4 176 {
Charles David Young 16:b2e77eb76ab4 177 Count1 = 0; // clear counters
Charles David Young 16:b2e77eb76ab4 178 Count2 = 0;
charlesdavidyoung 5:4f90b458dbdf 179 }
Charles David Young 16:b2e77eb76ab4 180 logToPC();
Charles David Young 16:b2e77eb76ab4 181 return;
Charles David Young 16:b2e77eb76ab4 182 }
walter76 1:75827d765e34 183
walter76 0:6d1742703713 184 //---------------------------------------------------------------------------
walter76 1:75827d765e34 185 // Increment CNT1 every time a rising edge is detected on TRIG1 (interrupt)
walter76 1:75827d765e34 186
walter76 1:75827d765e34 187 void Count1_up(void)
walter76 1:75827d765e34 188 {
Charles David Young 30:2fa7fcabf7c7 189 Beep();
Charles David Young 28:c81d00ec28a3 190 Count1++;
Charles David Young 28:c81d00ec28a3 191 return;
walter76 1:75827d765e34 192 }
walter76 1:75827d765e34 193
walter76 1:75827d765e34 194 //---------------------------------------------------------------------------
walter76 1:75827d765e34 195 // Increment CNT1 every time a rising edge is detected on TRIG2 (interrupt)
walter76 1:75827d765e34 196
walter76 1:75827d765e34 197 void Count2_up(void)
walter76 1:75827d765e34 198 {
Charles David Young 30:2fa7fcabf7c7 199 Beep();
Charles David Young 28:c81d00ec28a3 200 Count2++;
Charles David Young 28:c81d00ec28a3 201 return;
walter76 1:75827d765e34 202 }
walter76 1:75827d765e34 203
walter76 1:75827d765e34 204
walter76 1:75827d765e34 205 //---------------------------------------------------------------------------
walter76 1:75827d765e34 206 //Generates a short beep via BUZZ
walter76 1:75827d765e34 207
walter76 1:75827d765e34 208 void Beep(void)
walter76 1:75827d765e34 209 {
Charles David Young 28:c81d00ec28a3 210 BUZZ = 1; // turn-on the buzzer
Charles David Young 32:12f725e0235e 211 wait_us(100); // wait
Charles David Young 28:c81d00ec28a3 212 BUZZ = 0; // turn-off the buzzer
Charles David Young 28:c81d00ec28a3 213 return;
walter76 0:6d1742703713 214 }
walter76 0:6d1742703713 215
walter76 1:75827d765e34 216 //-------- END OF FILE --------------
walter76 1:75827d765e34 217 //==============================================================================