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:
charlesdavidyoung
Date:
Mon Sep 03 18:49:24 2018 +0000
Revision:
18:ef5678ab1889
Parent:
17:6eed17197004
Child:
20:fb73eaaf0894
Added wheel timout to state machine.  LED blinks when mode is being selected.

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 //
walter76 1:75827d765e34 6 // Feb 14, 2018: initial release aimed to test the counters, the serial port
walter76 1:75827d765e34 7 // the PWM output and the MAX7219 operation.
walter76 1:75827d765e34 8 // Feb 15, 2018: Removed MAX7219 libray (replaced with custom routine).
walter76 1:75827d765e34 9 // Added 74HC595 routine. Added beep. Added Gate_write
walter76 1:75827d765e34 10 //
walter76 2:ac0ed3d84d44 11
walter76 1:75827d765e34 12
walter76 1:75827d765e34 13 // this block includes key libraries
walter76 1:75827d765e34 14 #include "mbed.h" // global Mbed library (always needed)
walter76 1:75827d765e34 15 #include <string> // strings management
walter76 1:75827d765e34 16 #include "QEI.h" // Quadrature Encoder functions
walter76 1:75827d765e34 17
walter76 1:75827d765e34 18 // definitions of fixed parameters
walter76 0:6d1742703713 19
walter76 1:75827d765e34 20 #define DEC_MODE 0x09FF // BCD decoding on all digits
walter76 1:75827d765e34 21 #define BRIGHTNESS 0x0A0F // max brightness
walter76 1:75827d765e34 22 #define SCAN_LIM 0x0B07 // use all 8 digits
walter76 1:75827d765e34 23 #define TURN_ON 0x0C01 // no shutdown (operating)
walter76 1:75827d765e34 24 #define SHUTDOWN 0x0C00 // shutdown
walter76 1:75827d765e34 25 #define TEST 0x0F00 // no test
walter76 1:75827d765e34 26
walter76 1:75827d765e34 27 #define DT 1 // delay time in us for SPI emulation
walter76 1:75827d765e34 28
walter76 1:75827d765e34 29 #define TGATE 10 // gate time (currently fixed for testing purpose)
walter76 1:75827d765e34 30 #define MAX_VAL 999999 // Max value managed by the 6-digits display
walter76 0:6d1742703713 31
walter76 1:75827d765e34 32
walter76 1:75827d765e34 33 #define CPM 0x01
walter76 1:75827d765e34 34 #define CPS 0x02
walter76 1:75827d765e34 35 #define PLS 0x04
walter76 1:75827d765e34 36 #define VOLTS 0x08
walter76 1:75827d765e34 37 #define CNT1 0x10
walter76 1:75827d765e34 38 #define CNT2 0x20
walter76 1:75827d765e34 39 #define HV 0x40
walter76 1:75827d765e34 40 #define MENU 0x80
charlesdavidyoung 13:0e7b06af9a2a 41 uint8_t LED_statuses[] = {CPM, CPS, PLS, VOLTS, CNT1, CNT2, HV, MENU};
Charles David Young 7:9f975e00600c 42 uint8_t LED_status = CPM;
Charles David Young 7:9f975e00600c 43 uint8_t LED_status_index = 0;
walter76 1:75827d765e34 44
walter76 1:75827d765e34 45 // definitions of the input/outputs (pins)
walter76 1:75827d765e34 46 DigitalOut AUX (D2); // AUX control for GPS module
walter76 0:6d1742703713 47 InterruptIn TRIG1 (D3); // Counter 1 trigger
walter76 0:6d1742703713 48 InterruptIn TRIG2 (D6); // Counter 2 trigger
charlesdavidyoung 15:808e98afe32b 49 DigitalIn QEPB (D9); // Quadrature encoder pushbutton
walter76 0:6d1742703713 50 PwmOut PWM (D10); // PWM output
walter76 1:75827d765e34 51 DigitalOut BUZZ (D13); // Buzzer
walter76 0:6d1742703713 52
walter76 1:75827d765e34 53 AnalogIn AIN0 (A0); // ADC input 0 (High Voltage)
walter76 1:75827d765e34 54 AnalogIn AIN1 (A1); // ADC input 1 (aux)
walter76 1:75827d765e34 55 DigitalOut CS2 (A2); // 74HC595 RCLK (pin 12)
walter76 1:75827d765e34 56 DigitalOut CS1 (A3); // MAX7219 CS (pin 12)
walter76 1:75827d765e34 57 DigitalOut SCK (A4); // 74HC595 SRCLK (pin 11) & MAX7219 SCK (pin 13)
walter76 1:75827d765e34 58 AnalogIn KEYB (A5); // Keyboard input (SW2 & SW3)
walter76 1:75827d765e34 59 DigitalOut MOSI (A6); // 74HC595 SER (pin 14) & MAX7219 DIN (pin 1)
walter76 1:75827d765e34 60 DigitalIn UN (A7); // Unused (in V1 PCB A5 and A7 must be connected)
walter76 1:75827d765e34 61
charlesdavidyoung 5:4f90b458dbdf 62 // LED on processor board
charlesdavidyoung 5:4f90b458dbdf 63 DigitalOut led1(LED1);
walter76 1:75827d765e34 64
walter76 1:75827d765e34 65 // definitions of peripherals and devices
Charles David Young 7:9f975e00600c 66
Charles David Young 7:9f975e00600c 67 // QEI class supports the mode wheel. In its most basic function it will tell
Charles David Young 7:9f975e00600c 68 // us which direction the wheel is moving (right or left) so that we can
Charles David Young 7:9f975e00600c 69 // use it to select the current mode.
walter76 1:75827d765e34 70 QEI Wheel(D12, D11, NC, 16); // Quadrature encoder
Charles David Young 7:9f975e00600c 71 int WheelCurrent = 0;
Charles David Young 7:9f975e00600c 72 int WheelPrevious = 0;
Charles David Young 16:b2e77eb76ab4 73 bool QEPBpressed = false; // only react to button when pressed
charlesdavidyoung 18:ef5678ab1889 74 int WheelStateTimer = 0;
charlesdavidyoung 18:ef5678ab1889 75 const int WheelStateTimeout = 5; //timeout for wheel button pushed
Charles David Young 16:b2e77eb76ab4 76 enum WheelState {
Charles David Young 16:b2e77eb76ab4 77 WHEEL_INACTIVE = 0,
Charles David Young 16:b2e77eb76ab4 78 WHEEL_MODE_SELECT = 1,
Charles David Young 16:b2e77eb76ab4 79 WHEEL_SUBMODE_SELECT = 2
Charles David Young 16:b2e77eb76ab4 80 };
Charles David Young 16:b2e77eb76ab4 81 enum WheelStateEvent {
Charles David Young 16:b2e77eb76ab4 82 WHEEL_Pressed = 0,
Charles David Young 16:b2e77eb76ab4 83 WHEEL_Timeout = 1
Charles David Young 16:b2e77eb76ab4 84 };
Charles David Young 16:b2e77eb76ab4 85 WheelState currentWheelState;
Charles David Young 7:9f975e00600c 86
charlesdavidyoung 17:6eed17197004 87 I2C i2c(D4, D5); // I2C port
charlesdavidyoung 17:6eed17197004 88 Ticker SecTenth_Beat; // .1 second ticker
walter76 1:75827d765e34 89 Ticker Sec_Beat; // 1 second ticker
walter76 1:75827d765e34 90 Serial PC(USBTX, USBRX); // Virtual COM via USB (PC connection)
walter76 1:75827d765e34 91 Serial GPS(D1, D0); // Serial port for GPS module
walter76 0:6d1742703713 92
walter76 0:6d1742703713 93 // Global variables
walter76 1:75827d765e34 94 uint8_t Disp_Digit[8]; // used to manage 8-digits through MAX7219
walter76 1:75827d765e34 95 uint16_t Stream; // used to stream out serial data to MAX7219
walter76 1:75827d765e34 96 time_t seconds; // Real-Time Clock (RTC) timestamp
walter76 1:75827d765e34 97 unsigned int value = 0; // displayed value on the 6-digits of the display
walter76 1:75827d765e34 98 uint8_t gate = TGATE; // displayed value on the 2-digits display
walter76 1:75827d765e34 99 uint32_t Count1, Count2; // pulse counters (32-bit)
walter76 1:75827d765e34 100 char Text[40]=""; // used to send messages over the serial port
walter76 1:75827d765e34 101 uint8_t Disp_mode = 0x01, Disp_unit = 0xA0; // status of 1st row and 2nd rows of LEDs
kd5byb 4:b17c6556cf1f 102 bool Stopped = 0; // status of counting activity
charlesdavidyoung 5:4f90b458dbdf 103 bool StartStopPressed = 0;// status of counting activity
walter76 1:75827d765e34 104 double ADC_val; // used to read ADC value
walter76 0:6d1742703713 105
walter76 1:75827d765e34 106 // ----- Prototypes of routines (defined below the main) -------------------
Charles David Young 16:b2e77eb76ab4 107 void UpdateInput(void); // periodically called by the ticker
Charles David Young 16:b2e77eb76ab4 108 void UpdateOutput(void); // periodically called by the ticker
walter76 1:75827d765e34 109 void Count1_up(void); // called every time an edge is detected on TRIG1 pin
walter76 1:75827d765e34 110 void Count2_up(void); // called every time an edge is detected on TRIG2 pin
walter76 1:75827d765e34 111 void Beep(void); // used to generate a short beep (buzzer)
walter76 1:75827d765e34 112 void LEDs_write(unsigned short); // write to 74HC595 (8x LEDs)
walter76 1:75827d765e34 113 void Display_init(void); // initialize MAX7219
walter76 1:75827d765e34 114 void Display_6D_write(uint8_t); // write to MAX7219 (Main 6-digits display)
walter76 1:75827d765e34 115 void Display_2D_write(unsigned short); // write to MAX7219 (Gate 2-digits display)
charlesdavidyoung 17:6eed17197004 116 void WheelStateMachine(WheelStateEvent event);
walter76 0:6d1742703713 117
walter76 0:6d1742703713 118 //==============================================================================
walter76 0:6d1742703713 119 //==============================================================================
walter76 0:6d1742703713 120
walter76 0:6d1742703713 121 int main()
walter76 0:6d1742703713 122 {
walter76 0:6d1742703713 123
walter76 1:75827d765e34 124 PC.baud(115200); // set baud-rate of virtual COM port (PC connection)
charlesdavidyoung 5:4f90b458dbdf 125 PC.printf("\nGEO COUNTER V1 2108");
walter76 1:75827d765e34 126 PC.printf(__DATE__);
walter76 1:75827d765e34 127 PC.printf(" ");
walter76 1:75827d765e34 128 PC.printf(__TIME__);
walter76 1:75827d765e34 129
walter76 2:ac0ed3d84d44 130 GPS.baud(9600); // set the baud-rate of the serial port dedicated to the GPS
walter76 0:6d1742703713 131
walter76 1:75827d765e34 132 CS1 = 1; // presets CS of MAX7219
walter76 1:75827d765e34 133 CS2 = 1; // preset CS of 74HC595
walter76 1:75827d765e34 134
charlesdavidyoung 13:0e7b06af9a2a 135 Display_6D_write(0x543210);
walter76 1:75827d765e34 136 Display_2D_write(TGATE);
walter76 1:75827d765e34 137 Display_init(); // initialize MAX7219
walter76 0:6d1742703713 138
walter76 1:75827d765e34 139 // RTC is supposed to be loose time at power down (no backup battery)
walter76 1:75827d765e34 140 // An initialization is performed anyway
walter76 1:75827d765e34 141 set_time(0); // Set time
walter76 0:6d1742703713 142
walter76 1:75827d765e34 143 Wheel.reset(); // clear the variable associated to the encoder
walter76 1:75827d765e34 144
walter76 1:75827d765e34 145 PWM.period_ms(3); // set the PWM period
walter76 1:75827d765e34 146 PWM.write(0.8); // set the PWM duty-cycle
walter76 1:75827d765e34 147
walter76 1:75827d765e34 148 LEDs_write(0x00); // initialize LEDs (CPM and CNT1 on)
walter76 1:75827d765e34 149 Beep(); // initial beep
walter76 1:75827d765e34 150
walter76 1:75827d765e34 151 // set the 1 sec ticker to periodically call the Update() routine
walter76 1:75827d765e34 152 // NOTE: this is also the 1-sec time base for counters. A better approach
walter76 1:75827d765e34 153 // would replace the ticker with an interrupt from the RTC (to be implemented)
charlesdavidyoung 17:6eed17197004 154 SecTenth_Beat.attach_us(&UpdateInput, 100000);
Charles David Young 16:b2e77eb76ab4 155 Sec_Beat.attach_us(&UpdateOutput, 1000000);
walter76 0:6d1742703713 156 //RTC::attach(&Update, RTC::Second);
walter76 1:75827d765e34 157 //RTC::detach(RTC::Second);
walter76 1:75827d765e34 158
walter76 1:75827d765e34 159 // main loop does nothing as all activities are interrupt driven
walter76 0:6d1742703713 160 while(1)
walter76 0:6d1742703713 161 {
walter76 2:ac0ed3d84d44 162 // dance (or drink a beer)
walter76 0:6d1742703713 163 }
walter76 0:6d1742703713 164 }
walter76 0:6d1742703713 165
walter76 0:6d1742703713 166
walter76 1:75827d765e34 167 //-------- END OF MAIN --------------
walter76 0:6d1742703713 168 //==============================================================================
walter76 0:6d1742703713 169
walter76 1:75827d765e34 170 // Definition of routines
walter76 1:75827d765e34 171
walter76 1:75827d765e34 172 //---------------------------------------------------------------------------
walter76 1:75827d765e34 173 // Update values to be displayed
Charles David Young 10:f48cc6be5ae8 174 void logToPC()
Charles David Young 10:f48cc6be5ae8 175 {
Charles David Young 10:f48cc6be5ae8 176 PC.printf("\nADC: %.02f", ADC_val);
Charles David Young 10:f48cc6be5ae8 177 PC.printf(Stopped ? " stopped" : " started");
Charles David Young 10:f48cc6be5ae8 178 // Timestamp to PC (debug)
Charles David Young 10:f48cc6be5ae8 179 seconds = time(NULL); // get current time
Charles David Young 10:f48cc6be5ae8 180 strftime(Text, 50, "%H:%M:%S", localtime(&seconds));
Charles David Young 10:f48cc6be5ae8 181 PC.printf(" RTC: %s, CNT1: %7d CNT2: %7d",Text, Count1, Count2);
charlesdavidyoung 15:808e98afe32b 182 PC.printf(" wheel %d %d", WheelCurrent, QEPB.read());
Charles David Young 10:f48cc6be5ae8 183 }
walter76 1:75827d765e34 184
Charles David Young 16:b2e77eb76ab4 185 void UpdateOutput()
charlesdavidyoung 18:ef5678ab1889 186 {
charlesdavidyoung 18:ef5678ab1889 187 // Blink mode LED
charlesdavidyoung 18:ef5678ab1889 188 if (WHEEL_MODE_SELECT == currentWheelState)
charlesdavidyoung 18:ef5678ab1889 189 LEDs_write(0);
charlesdavidyoung 18:ef5678ab1889 190
charlesdavidyoung 18:ef5678ab1889 191 if (WheelStateTimer)
charlesdavidyoung 18:ef5678ab1889 192 {
charlesdavidyoung 18:ef5678ab1889 193 WheelStateTimer++;
charlesdavidyoung 18:ef5678ab1889 194 if (++WheelStateTimer > WheelStateTimeout)
charlesdavidyoung 18:ef5678ab1889 195 {
charlesdavidyoung 18:ef5678ab1889 196 WheelStateMachine(WHEEL_Timeout);
charlesdavidyoung 18:ef5678ab1889 197 WheelStateTimer = 0;
charlesdavidyoung 18:ef5678ab1889 198 }
charlesdavidyoung 18:ef5678ab1889 199 }
charlesdavidyoung 18:ef5678ab1889 200
Charles David Young 16:b2e77eb76ab4 201 if(Stopped)
Charles David Young 16:b2e77eb76ab4 202 {
Charles David Young 16:b2e77eb76ab4 203 // disable interrupts on TRIG1 and TRIG2
Charles David Young 16:b2e77eb76ab4 204
Charles David Young 16:b2e77eb76ab4 205 TRIG1.rise(NULL);
Charles David Young 16:b2e77eb76ab4 206 TRIG2.rise(NULL);
Charles David Young 16:b2e77eb76ab4 207
Charles David Young 16:b2e77eb76ab4 208 // show zero gate time
Charles David Young 16:b2e77eb76ab4 209 gate = 0;
Charles David Young 16:b2e77eb76ab4 210 Display_2D_write(gate);
Charles David Young 16:b2e77eb76ab4 211
Charles David Young 16:b2e77eb76ab4 212 // show selected content on main display
Charles David Young 16:b2e77eb76ab4 213 value = (int)(Count1/TGATE);
Charles David Young 16:b2e77eb76ab4 214 Display_6D_write(value); // refresh the main display
Charles David Young 16:b2e77eb76ab4 215 }
Charles David Young 16:b2e77eb76ab4 216
Charles David Young 16:b2e77eb76ab4 217 else
Charles David Young 16:b2e77eb76ab4 218 {
Charles David Young 16:b2e77eb76ab4 219 // Enable interrupts on rising edge of digital inputs TRIG1 & TRIG2
Charles David Young 16:b2e77eb76ab4 220 TRIG1.rise(&Count1_up);
Charles David Young 16:b2e77eb76ab4 221 TRIG2.rise(&Count2_up);
Charles David Young 16:b2e77eb76ab4 222
Charles David Young 16:b2e77eb76ab4 223 if(gate==0) // show the counter value at the end of the gate time
Charles David Young 16:b2e77eb76ab4 224 {
Charles David Young 16:b2e77eb76ab4 225 value = (int)(Count1/TGATE);
Charles David Young 16:b2e77eb76ab4 226
Charles David Young 16:b2e77eb76ab4 227 Display_6D_write(value); // refresh the main display
Charles David Young 7:9f975e00600c 228
Charles David Young 16:b2e77eb76ab4 229 Count1 = 0; // clear both counters
Charles David Young 16:b2e77eb76ab4 230 Count2 = 0;
Charles David Young 16:b2e77eb76ab4 231 gate = TGATE;// and reload the gate time
Charles David Young 16:b2e77eb76ab4 232
Charles David Young 16:b2e77eb76ab4 233 }
Charles David Young 16:b2e77eb76ab4 234
Charles David Young 16:b2e77eb76ab4 235 Display_2D_write(gate); // show gate time countdown
Charles David Young 16:b2e77eb76ab4 236 gate--;
Charles David Young 16:b2e77eb76ab4 237 }
Charles David Young 16:b2e77eb76ab4 238 }
Charles David Young 16:b2e77eb76ab4 239
Charles David Young 16:b2e77eb76ab4 240 void UpdateInput()
Charles David Young 16:b2e77eb76ab4 241 {
Charles David Young 16:b2e77eb76ab4 242 LEDs_write(LED_statuses[LED_status_index]);
Charles David Young 16:b2e77eb76ab4 243
Charles David Young 16:b2e77eb76ab4 244 ADC_val = KEYB.read(); // read voltage from keyboard
Charles David Young 16:b2e77eb76ab4 245 if ( (ADC_val<0.1) // START/STOP pushbutton pressed
Charles David Young 16:b2e77eb76ab4 246 && (!StartStopPressed))
Charles David Young 16:b2e77eb76ab4 247 {
Charles David Young 16:b2e77eb76ab4 248 StartStopPressed = true;
Charles David Young 16:b2e77eb76ab4 249 Stopped=!Stopped; // toggle status
Charles David Young 16:b2e77eb76ab4 250 }
Charles David Young 16:b2e77eb76ab4 251 else
Charles David Young 16:b2e77eb76ab4 252 StartStopPressed = false;
charlesdavidyoung 5:4f90b458dbdf 253
Charles David Young 16:b2e77eb76ab4 254 if((ADC_val>0.6)&&(ADC_val<0.7)) // CLEAR pushbutton pressed
Charles David Young 16:b2e77eb76ab4 255 {
Charles David Young 16:b2e77eb76ab4 256 Count1 = 0; // clear counters
Charles David Young 16:b2e77eb76ab4 257 Count2 = 0;
charlesdavidyoung 5:4f90b458dbdf 258 }
charlesdavidyoung 5:4f90b458dbdf 259
charlesdavidyoung 18:ef5678ab1889 260 // react to wheel if in proper state
charlesdavidyoung 18:ef5678ab1889 261 WheelCurrent = int(Wheel.getPulses());
Charles David Young 16:b2e77eb76ab4 262 if ( (WHEEL_MODE_SELECT == currentWheelState)
Charles David Young 16:b2e77eb76ab4 263 || (WHEEL_SUBMODE_SELECT == currentWheelState))
Charles David Young 16:b2e77eb76ab4 264 {
charlesdavidyoung 18:ef5678ab1889 265 if (WHEEL_MODE_SELECT == currentWheelState)
charlesdavidyoung 18:ef5678ab1889 266 {
charlesdavidyoung 18:ef5678ab1889 267 if (WheelCurrent > WheelPrevious)
charlesdavidyoung 18:ef5678ab1889 268 LED_status_index = ++LED_status_index % sizeof(LED_statuses);
charlesdavidyoung 18:ef5678ab1889 269 else
charlesdavidyoung 18:ef5678ab1889 270 if (WheelCurrent < WheelPrevious)
charlesdavidyoung 18:ef5678ab1889 271 LED_status_index = --LED_status_index % sizeof(LED_statuses);
charlesdavidyoung 18:ef5678ab1889 272 }
charlesdavidyoung 18:ef5678ab1889 273 // Keep resetting WheelStateTimer as long as wheel is moving
charlesdavidyoung 18:ef5678ab1889 274 if (WheelPrevious != WheelCurrent)
charlesdavidyoung 18:ef5678ab1889 275 WheelStateTimer = 1;
Charles David Young 16:b2e77eb76ab4 276 }
charlesdavidyoung 18:ef5678ab1889 277 WheelPrevious = WheelCurrent;
Charles David Young 16:b2e77eb76ab4 278
Charles David Young 16:b2e77eb76ab4 279 // detect when wheel button is pressed but wait until it is released
Charles David Young 16:b2e77eb76ab4 280 // before doing anything
Charles David Young 16:b2e77eb76ab4 281 bool QEPBbutton = QEPB.read();
Charles David Young 16:b2e77eb76ab4 282 if ( (QEPBbutton)
Charles David Young 16:b2e77eb76ab4 283 && (!QEPBpressed))
Charles David Young 16:b2e77eb76ab4 284 {
Charles David Young 16:b2e77eb76ab4 285 QEPBpressed = true;
Charles David Young 16:b2e77eb76ab4 286 }
Charles David Young 16:b2e77eb76ab4 287 else
Charles David Young 16:b2e77eb76ab4 288 if ( (!QEPBbutton)
Charles David Young 16:b2e77eb76ab4 289 && (QEPBpressed))
Charles David Young 16:b2e77eb76ab4 290 {
Charles David Young 16:b2e77eb76ab4 291 QEPBpressed = false;
Charles David Young 16:b2e77eb76ab4 292 WheelStateMachine(WHEEL_Pressed);
Charles David Young 16:b2e77eb76ab4 293 }
walter76 0:6d1742703713 294
Charles David Young 16:b2e77eb76ab4 295 logToPC();
Charles David Young 16:b2e77eb76ab4 296 return;
Charles David Young 16:b2e77eb76ab4 297 }
walter76 1:75827d765e34 298
Charles David Young 16:b2e77eb76ab4 299 void WheelStateMachine(WheelStateEvent event)
Charles David Young 16:b2e77eb76ab4 300 {
charlesdavidyoung 17:6eed17197004 301 switch (currentWheelState) {
charlesdavidyoung 17:6eed17197004 302 case WHEEL_INACTIVE:
Charles David Young 16:b2e77eb76ab4 303 if (WHEEL_Pressed == event)
charlesdavidyoung 18:ef5678ab1889 304 {
Charles David Young 16:b2e77eb76ab4 305 currentWheelState = WHEEL_MODE_SELECT;
charlesdavidyoung 18:ef5678ab1889 306 WheelStateTimer = 1;
charlesdavidyoung 18:ef5678ab1889 307 }
Charles David Young 16:b2e77eb76ab4 308 break;
charlesdavidyoung 17:6eed17197004 309 case WHEEL_MODE_SELECT:
Charles David Young 16:b2e77eb76ab4 310 if (WHEEL_Pressed == event)
charlesdavidyoung 18:ef5678ab1889 311 {
Charles David Young 16:b2e77eb76ab4 312 currentWheelState = WHEEL_SUBMODE_SELECT;
charlesdavidyoung 18:ef5678ab1889 313 WheelStateTimer = 1;
charlesdavidyoung 18:ef5678ab1889 314 }
Charles David Young 16:b2e77eb76ab4 315 else
Charles David Young 16:b2e77eb76ab4 316 if (WHEEL_Timeout == event)
Charles David Young 16:b2e77eb76ab4 317 currentWheelState = WHEEL_INACTIVE;
Charles David Young 16:b2e77eb76ab4 318 break;
charlesdavidyoung 17:6eed17197004 319 case WHEEL_SUBMODE_SELECT:
Charles David Young 16:b2e77eb76ab4 320 if (WHEEL_Pressed == event)
charlesdavidyoung 18:ef5678ab1889 321 {
Charles David Young 16:b2e77eb76ab4 322 currentWheelState = WHEEL_MODE_SELECT;
charlesdavidyoung 18:ef5678ab1889 323 WheelStateTimer = 1;
charlesdavidyoung 18:ef5678ab1889 324 }
Charles David Young 16:b2e77eb76ab4 325 else
Charles David Young 16:b2e77eb76ab4 326 if (WHEEL_Timeout == event)
Charles David Young 16:b2e77eb76ab4 327 currentWheelState = WHEEL_INACTIVE;
Charles David Young 16:b2e77eb76ab4 328 break;
Charles David Young 16:b2e77eb76ab4 329 default:
charlesdavidyoung 17:6eed17197004 330 break;
Charles David Young 16:b2e77eb76ab4 331 }
walter76 0:6d1742703713 332 }
walter76 0:6d1742703713 333
walter76 0:6d1742703713 334 //---------------------------------------------------------------------------
walter76 1:75827d765e34 335 // Increment CNT1 every time a rising edge is detected on TRIG1 (interrupt)
walter76 1:75827d765e34 336
walter76 1:75827d765e34 337 void Count1_up(void)
walter76 1:75827d765e34 338 {
walter76 1:75827d765e34 339 Count1++;
walter76 1:75827d765e34 340 return;
walter76 1:75827d765e34 341 }
walter76 1:75827d765e34 342
walter76 1:75827d765e34 343
walter76 1:75827d765e34 344 //---------------------------------------------------------------------------
walter76 1:75827d765e34 345 // Increment CNT1 every time a rising edge is detected on TRIG2 (interrupt)
walter76 1:75827d765e34 346
walter76 1:75827d765e34 347 void Count2_up(void)
walter76 1:75827d765e34 348 {
walter76 1:75827d765e34 349 Count2++;
walter76 1:75827d765e34 350 return;
walter76 1:75827d765e34 351 }
walter76 1:75827d765e34 352
walter76 1:75827d765e34 353
walter76 1:75827d765e34 354 //---------------------------------------------------------------------------
walter76 1:75827d765e34 355 //Generates a short beep via BUZZ
walter76 1:75827d765e34 356
walter76 1:75827d765e34 357 void Beep(void)
walter76 1:75827d765e34 358 {
walter76 1:75827d765e34 359 BUZZ = 1; // turn-on the buzzer
walter76 1:75827d765e34 360 wait(0.3); // wait
walter76 1:75827d765e34 361 BUZZ = 0; // turn-off the buzzer
walter76 0:6d1742703713 362 return;
walter76 0:6d1742703713 363 }
walter76 0:6d1742703713 364
walter76 0:6d1742703713 365
walter76 0:6d1742703713 366 //---------------------------------------------------------------------------
walter76 1:75827d765e34 367 //Write to 74HC595 (LEDs) - Take care to avoid conflict with MAX7219
walter76 1:75827d765e34 368
walter76 1:75827d765e34 369 void LEDs_write(unsigned short data_val)
walter76 1:75827d765e34 370 {
walter76 1:75827d765e34 371 // Update 74HC595 shift registers
walter76 1:75827d765e34 372 unsigned short mask;
walter76 1:75827d765e34 373
walter76 1:75827d765e34 374 SCK = 0;
walter76 1:75827d765e34 375 wait_us(DT);
walter76 1:75827d765e34 376 CS2 = 0;
walter76 1:75827d765e34 377
walter76 1:75827d765e34 378 for(mask = 0x80; mask!= 0; mask>>= 1)
walter76 1:75827d765e34 379 {
walter76 1:75827d765e34 380 wait_us(DT);
walter76 1:75827d765e34 381 SCK = 0;
walter76 1:75827d765e34 382 if(mask & data_val)
walter76 1:75827d765e34 383 MOSI = 0;
walter76 1:75827d765e34 384 else
walter76 1:75827d765e34 385 MOSI = 1;
walter76 1:75827d765e34 386 wait_us(DT);
walter76 1:75827d765e34 387 SCK = 1;
walter76 1:75827d765e34 388 }
walter76 1:75827d765e34 389
walter76 1:75827d765e34 390 SCK = 0;
walter76 1:75827d765e34 391 wait_us(DT);
walter76 1:75827d765e34 392 CS2 = 1;
walter76 1:75827d765e34 393
walter76 0:6d1742703713 394 return;
walter76 0:6d1742703713 395 }
walter76 0:6d1742703713 396
walter76 0:6d1742703713 397
walter76 1:75827d765e34 398 //---------------------------------------------------------------------------
walter76 1:75827d765e34 399 // Initialize the MAX7219
walter76 1:75827d765e34 400
walter76 1:75827d765e34 401 void Display_init(void)
walter76 1:75827d765e34 402 {
walter76 1:75827d765e34 403 uint8_t i;
walter76 1:75827d765e34 404 uint16_t mask;
walter76 1:75827d765e34 405 uint16_t data_to_send[6] = {SHUTDOWN, TURN_ON, DEC_MODE, BRIGHTNESS, SCAN_LIM, TEST};
walter76 1:75827d765e34 406 //{SHUTDOWN, TURN_ON, DEC_MODE, BRIGHTNESS, SCAN_LIM, TEST};
walter76 1:75827d765e34 407 for(i = 0; i <6; i++)
walter76 1:75827d765e34 408 {
walter76 1:75827d765e34 409 CS1 = 0;
walter76 1:75827d765e34 410
walter76 1:75827d765e34 411 for(mask = 0x8000; mask!= 0; mask>>= 1)
walter76 1:75827d765e34 412 {
walter76 1:75827d765e34 413 wait_us(DT);
walter76 1:75827d765e34 414 SCK = 0;
walter76 1:75827d765e34 415 if(mask & data_to_send[i])
walter76 1:75827d765e34 416 MOSI = 1;
walter76 1:75827d765e34 417 else
walter76 1:75827d765e34 418 MOSI = 0;
walter76 1:75827d765e34 419 wait_us(DT);
walter76 1:75827d765e34 420 SCK = 1;
walter76 1:75827d765e34 421 }
walter76 1:75827d765e34 422
walter76 1:75827d765e34 423 wait_us(DT);
walter76 1:75827d765e34 424 SCK = 0;
walter76 1:75827d765e34 425 wait_us(DT);
walter76 1:75827d765e34 426 CS1 = 1;
walter76 1:75827d765e34 427 }
walter76 1:75827d765e34 428
walter76 1:75827d765e34 429 return;
walter76 1:75827d765e34 430 }
walter76 1:75827d765e34 431
walter76 1:75827d765e34 432
walter76 1:75827d765e34 433 //---------------------------------------------------------------------------
walter76 1:75827d765e34 434 // Refresh the 6 digits of the main display
walter76 1:75827d765e34 435
walter76 1:75827d765e34 436 void Display_6D_write(uint8_t value)
walter76 1:75827d765e34 437 {
walter76 1:75827d765e34 438
walter76 1:75827d765e34 439 uint8_t digit;
walter76 1:75827d765e34 440 uint16_t mask, data_to_send;
walter76 1:75827d765e34 441 char TextString[6];
walter76 1:75827d765e34 442
walter76 1:75827d765e34 443 // int to string, then string to digits
walter76 1:75827d765e34 444
walter76 1:75827d765e34 445 sprintf(TextString, "%6d", value); // int to string
walter76 1:75827d765e34 446
walter76 1:75827d765e34 447 for(uint8_t i=0; i<6; i++)
walter76 1:75827d765e34 448 {
walter76 1:75827d765e34 449 if(TextString[i] == ' ') // blank empty digits
walter76 1:75827d765e34 450 Disp_Digit[i] = 0xFF;
walter76 1:75827d765e34 451 else
walter76 1:75827d765e34 452 Disp_Digit[i] = TextString[i]-'0';
walter76 1:75827d765e34 453 }
walter76 1:75827d765e34 454
walter76 1:75827d765e34 455 // write to chip
walter76 1:75827d765e34 456
walter76 1:75827d765e34 457 SCK = 0;
walter76 1:75827d765e34 458 wait_us(DT);
walter76 1:75827d765e34 459
walter76 1:75827d765e34 460 for(digit = 1; digit <7; digit++)
walter76 1:75827d765e34 461 {
walter76 1:75827d765e34 462 // each stream consists of digit address and data to show
Charles David Young 6:05201ecabb95 463 data_to_send = 7 - digit;
walter76 1:75827d765e34 464 data_to_send<<=8;
walter76 1:75827d765e34 465 data_to_send = data_to_send | Disp_Digit[digit-1];
walter76 1:75827d765e34 466
walter76 1:75827d765e34 467 CS1 = 0;
walter76 1:75827d765e34 468
walter76 1:75827d765e34 469 for(mask = 0x8000; mask!= 0; mask>>= 1)
walter76 1:75827d765e34 470 {
walter76 1:75827d765e34 471 wait_us(DT);
walter76 1:75827d765e34 472 SCK = 0;
walter76 1:75827d765e34 473 if(mask & data_to_send)
walter76 1:75827d765e34 474 MOSI = 1;
walter76 1:75827d765e34 475 else
walter76 1:75827d765e34 476 MOSI = 0;
walter76 1:75827d765e34 477
walter76 1:75827d765e34 478 wait_us(DT);
walter76 1:75827d765e34 479 SCK = 1;
walter76 1:75827d765e34 480 }
walter76 1:75827d765e34 481
walter76 1:75827d765e34 482 wait_us(DT);
walter76 1:75827d765e34 483 SCK = 0;
walter76 1:75827d765e34 484 wait_us(DT);
walter76 1:75827d765e34 485 CS1 = 1;
walter76 1:75827d765e34 486 }
walter76 1:75827d765e34 487
walter76 1:75827d765e34 488 return;
walter76 1:75827d765e34 489 }
walter76 1:75827d765e34 490
walter76 1:75827d765e34 491
walter76 1:75827d765e34 492 //---------------------------------------------------------------------------
walter76 1:75827d765e34 493 // Refresh the 2 digits of the gate display
walter76 1:75827d765e34 494
walter76 1:75827d765e34 495 void Display_2D_write(unsigned short value)
walter76 1:75827d765e34 496 {
walter76 1:75827d765e34 497
walter76 1:75827d765e34 498 uint8_t digit;
walter76 1:75827d765e34 499 uint16_t mask, data_to_send;
walter76 1:75827d765e34 500 char TextString[2];
walter76 1:75827d765e34 501
walter76 1:75827d765e34 502 // int to string, then string to digits
walter76 1:75827d765e34 503
walter76 1:75827d765e34 504 sprintf(TextString, "%2d", value); // int to string
walter76 1:75827d765e34 505
walter76 1:75827d765e34 506 if(TextString[0] == ' ') // blank empty digits
walter76 1:75827d765e34 507 Disp_Digit[7] = 0xFF;
walter76 1:75827d765e34 508 else
walter76 1:75827d765e34 509 Disp_Digit[7] = TextString[0] - '0';
walter76 1:75827d765e34 510
walter76 1:75827d765e34 511 Disp_Digit[6] = TextString[1] - '0';
walter76 1:75827d765e34 512
walter76 1:75827d765e34 513 // write to chip
walter76 1:75827d765e34 514
walter76 1:75827d765e34 515 SCK = 0;
walter76 1:75827d765e34 516 wait_us(DT);
walter76 1:75827d765e34 517
walter76 1:75827d765e34 518 for(digit = 7; digit <9; digit++)
walter76 1:75827d765e34 519 {
walter76 1:75827d765e34 520 // each stream consists of digit address and data to show
walter76 1:75827d765e34 521 data_to_send = digit;
walter76 1:75827d765e34 522 data_to_send<<=8;
walter76 1:75827d765e34 523 data_to_send = data_to_send | Disp_Digit[digit-1];
walter76 1:75827d765e34 524
walter76 1:75827d765e34 525 CS1 = 0;
walter76 1:75827d765e34 526
walter76 1:75827d765e34 527 for(mask = 0x8000; mask!= 0; mask>>= 1)
walter76 1:75827d765e34 528 {
walter76 1:75827d765e34 529 wait_us(DT);
walter76 1:75827d765e34 530 SCK = 0;
walter76 1:75827d765e34 531
walter76 1:75827d765e34 532 if(mask & data_to_send)
walter76 1:75827d765e34 533 MOSI = 1;
walter76 1:75827d765e34 534 else
walter76 1:75827d765e34 535 MOSI = 0;
walter76 1:75827d765e34 536
walter76 1:75827d765e34 537 wait_us(DT);
walter76 1:75827d765e34 538 SCK = 1;
walter76 1:75827d765e34 539 }
walter76 1:75827d765e34 540
walter76 1:75827d765e34 541 wait_us(DT);
walter76 1:75827d765e34 542 SCK = 0;
walter76 1:75827d765e34 543 wait_us(DT);
walter76 1:75827d765e34 544 CS1 = 1;
walter76 1:75827d765e34 545 }
walter76 1:75827d765e34 546
walter76 1:75827d765e34 547 return;
walter76 1:75827d765e34 548 }
walter76 1:75827d765e34 549
walter76 1:75827d765e34 550 //-------- END OF FILE --------------
walter76 1:75827d765e34 551 //==============================================================================
walter76 1:75827d765e34 552
walter76 1:75827d765e34 553
walter76 1:75827d765e34 554