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:
kd5byb
Date:
Sun Sep 02 01:42:59 2018 +0000
Revision:
4:b17c6556cf1f
Parent:
3:23b0185c7e7b
Child:
5:4f90b458dbdf
Modified per messages in the forum.  Basically starts with Stopped = 0 so counter auto starts, then tweaks ADC values for an AREF of 2.5 VDC.

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
walter76 1:75827d765e34 41
walter76 1:75827d765e34 42 // definitions of the input/outputs (pins)
walter76 1:75827d765e34 43 DigitalOut AUX (D2); // AUX control for GPS module
walter76 0:6d1742703713 44 InterruptIn TRIG1 (D3); // Counter 1 trigger
walter76 0:6d1742703713 45 InterruptIn TRIG2 (D6); // Counter 2 trigger
walter76 1:75827d765e34 46 DigitalIn QEIPB (D9); // Quadrature encoder pushbutton
walter76 0:6d1742703713 47 PwmOut PWM (D10); // PWM output
walter76 1:75827d765e34 48 DigitalOut BUZZ (D13); // Buzzer
walter76 0:6d1742703713 49
walter76 1:75827d765e34 50 AnalogIn AIN0 (A0); // ADC input 0 (High Voltage)
walter76 1:75827d765e34 51 AnalogIn AIN1 (A1); // ADC input 1 (aux)
walter76 1:75827d765e34 52 DigitalOut CS2 (A2); // 74HC595 RCLK (pin 12)
walter76 1:75827d765e34 53 DigitalOut CS1 (A3); // MAX7219 CS (pin 12)
walter76 1:75827d765e34 54 DigitalOut SCK (A4); // 74HC595 SRCLK (pin 11) & MAX7219 SCK (pin 13)
walter76 1:75827d765e34 55 AnalogIn KEYB (A5); // Keyboard input (SW2 & SW3)
walter76 1:75827d765e34 56 DigitalOut MOSI (A6); // 74HC595 SER (pin 14) & MAX7219 DIN (pin 1)
walter76 1:75827d765e34 57 DigitalIn UN (A7); // Unused (in V1 PCB A5 and A7 must be connected)
walter76 1:75827d765e34 58
walter76 1:75827d765e34 59
walter76 1:75827d765e34 60 // definitions of peripherals and devices
walter76 1:75827d765e34 61 QEI Wheel(D12, D11, NC, 16); // Quadrature encoder
walter76 1:75827d765e34 62 I2C i2c(D4, D5); // I2C port
walter76 1:75827d765e34 63 Ticker Sec_Beat; // 1 second ticker
walter76 1:75827d765e34 64 Serial PC(USBTX, USBRX); // Virtual COM via USB (PC connection)
walter76 1:75827d765e34 65 Serial GPS(D1, D0); // Serial port for GPS module
walter76 0:6d1742703713 66
walter76 0:6d1742703713 67 // Global variables
walter76 1:75827d765e34 68 uint8_t Disp_Digit[8]; // used to manage 8-digits through MAX7219
walter76 1:75827d765e34 69 uint16_t Stream; // used to stream out serial data to MAX7219
walter76 1:75827d765e34 70 time_t seconds; // Real-Time Clock (RTC) timestamp
walter76 1:75827d765e34 71 unsigned int value = 0; // displayed value on the 6-digits of the display
walter76 1:75827d765e34 72 uint8_t gate = TGATE; // displayed value on the 2-digits display
walter76 1:75827d765e34 73 uint32_t Count1, Count2; // pulse counters (32-bit)
walter76 1:75827d765e34 74 char Text[40]=""; // used to send messages over the serial port
walter76 1:75827d765e34 75 uint8_t Disp_mode = 0x01, Disp_unit = 0xA0; // status of 1st row and 2nd rows of LEDs
kd5byb 4:b17c6556cf1f 76 bool Stopped = 0; // status of counting activity
walter76 1:75827d765e34 77 double ADC_val; // used to read ADC value
walter76 0:6d1742703713 78
walter76 1:75827d765e34 79 // ----- Prototypes of routines (defined below the main) -------------------
walter76 1:75827d765e34 80 void Update(void); // periodically called by the ticker
walter76 1:75827d765e34 81 void Count1_up(void); // called every time an edge is detected on TRIG1 pin
walter76 1:75827d765e34 82 void Count2_up(void); // called every time an edge is detected on TRIG2 pin
walter76 1:75827d765e34 83 void Beep(void); // used to generate a short beep (buzzer)
walter76 1:75827d765e34 84 void LEDs_write(unsigned short); // write to 74HC595 (8x LEDs)
walter76 1:75827d765e34 85 void Display_init(void); // initialize MAX7219
walter76 1:75827d765e34 86 void Display_6D_write(uint8_t); // write to MAX7219 (Main 6-digits display)
walter76 1:75827d765e34 87 void Display_2D_write(unsigned short); // write to MAX7219 (Gate 2-digits display)
walter76 0:6d1742703713 88
walter76 0:6d1742703713 89 //==============================================================================
walter76 0:6d1742703713 90 //==============================================================================
walter76 0:6d1742703713 91
walter76 0:6d1742703713 92 int main()
walter76 0:6d1742703713 93 {
walter76 0:6d1742703713 94
walter76 1:75827d765e34 95 PC.baud(115200); // set baud-rate of virtual COM port (PC connection)
walter76 1:75827d765e34 96 PC.printf("GEO COUNTER V1 2108\n");
walter76 1:75827d765e34 97 PC.printf(__DATE__);
walter76 1:75827d765e34 98 PC.printf(" ");
walter76 1:75827d765e34 99 PC.printf(__TIME__);
walter76 1:75827d765e34 100 PC.printf("\nReady...\n");
walter76 1:75827d765e34 101
walter76 2:ac0ed3d84d44 102 GPS.baud(9600); // set the baud-rate of the serial port dedicated to the GPS
walter76 0:6d1742703713 103
walter76 1:75827d765e34 104 CS1 = 1; // presets CS of MAX7219
walter76 1:75827d765e34 105 CS2 = 1; // preset CS of 74HC595
walter76 1:75827d765e34 106
walter76 1:75827d765e34 107 Display_6D_write(0);
walter76 1:75827d765e34 108 Display_2D_write(TGATE);
walter76 1:75827d765e34 109 Display_init(); // initialize MAX7219
walter76 0:6d1742703713 110
walter76 1:75827d765e34 111 // RTC is supposed to be loose time at power down (no backup battery)
walter76 1:75827d765e34 112 // An initialization is performed anyway
walter76 1:75827d765e34 113 set_time(0); // Set time
walter76 0:6d1742703713 114
walter76 1:75827d765e34 115 Wheel.reset(); // clear the variable associated to the encoder
walter76 1:75827d765e34 116
walter76 1:75827d765e34 117 PWM.period_ms(3); // set the PWM period
walter76 1:75827d765e34 118 PWM.write(0.8); // set the PWM duty-cycle
walter76 1:75827d765e34 119
walter76 1:75827d765e34 120 LEDs_write(0x00); // initialize LEDs (CPM and CNT1 on)
walter76 1:75827d765e34 121 Beep(); // initial beep
walter76 1:75827d765e34 122
walter76 1:75827d765e34 123 uint8_t LED_status = CNT1 | CPS;
walter76 1:75827d765e34 124 LEDs_write(LED_status);
walter76 1:75827d765e34 125
walter76 1:75827d765e34 126 // set the 1 sec ticker to periodically call the Update() routine
walter76 1:75827d765e34 127 // NOTE: this is also the 1-sec time base for counters. A better approach
walter76 1:75827d765e34 128 // would replace the ticker with an interrupt from the RTC (to be implemented)
Charles David Young 3:23b0185c7e7b 129 Sec_Beat.attach_us(&Update, 1000000);
walter76 0:6d1742703713 130 //RTC::attach(&Update, RTC::Second);
walter76 1:75827d765e34 131 //RTC::detach(RTC::Second);
walter76 1:75827d765e34 132
walter76 1:75827d765e34 133 // main loop does nothing as all activities are interrupt driven
walter76 0:6d1742703713 134 while(1)
walter76 0:6d1742703713 135 {
walter76 2:ac0ed3d84d44 136 // dance (or drink a beer)
walter76 0:6d1742703713 137 }
walter76 0:6d1742703713 138 }
walter76 0:6d1742703713 139
walter76 0:6d1742703713 140
walter76 1:75827d765e34 141 //-------- END OF MAIN --------------
walter76 0:6d1742703713 142 //==============================================================================
walter76 0:6d1742703713 143
walter76 1:75827d765e34 144 // Definition of routines
walter76 1:75827d765e34 145
walter76 1:75827d765e34 146 //---------------------------------------------------------------------------
walter76 1:75827d765e34 147 // Update values to be displayed
walter76 1:75827d765e34 148
walter76 1:75827d765e34 149 void Update()
walter76 1:75827d765e34 150 {
walter76 1:75827d765e34 151 ADC_val = KEYB.read(); // read voltage from keyboard
kd5byb 4:b17c6556cf1f 152 PC.printf("%lf\n", ADC_val);
walter76 1:75827d765e34 153 if(ADC_val<0.1) // RESET pushbutton pressed
walter76 1:75827d765e34 154 {
kd5byb 4:b17c6556cf1f 155 PC.printf("reset pushed");
walter76 1:75827d765e34 156 Count1 = 0; // clear counters
walter76 1:75827d765e34 157 Count2 = 0;
walter76 1:75827d765e34 158 }
walter76 1:75827d765e34 159
kd5byb 4:b17c6556cf1f 160 if((ADC_val>0.6)&&(ADC_val<0.7)) // START/STOP pushbutton pressed
kd5byb 4:b17c6556cf1f 161 {
walter76 1:75827d765e34 162 Stopped=!Stopped; // toggle status
kd5byb 4:b17c6556cf1f 163 PC.printf("ADC > 0.6 but < 0.7");
kd5byb 4:b17c6556cf1f 164 }
walter76 1:75827d765e34 165 if(Stopped)
walter76 1:75827d765e34 166 {
walter76 1:75827d765e34 167 // disable interrupts on TRIG1 and TRIG2
kd5byb 4:b17c6556cf1f 168
kd5byb 4:b17c6556cf1f 169 PC.printf("stopped");
walter76 1:75827d765e34 170 TRIG1.rise(NULL);
walter76 1:75827d765e34 171 TRIG2.rise(NULL);
walter76 0:6d1742703713 172
walter76 1:75827d765e34 173 // show zero gate time
walter76 1:75827d765e34 174 gate = 0;
walter76 1:75827d765e34 175 Display_2D_write(gate);
walter76 0:6d1742703713 176
walter76 1:75827d765e34 177 // show selected content on main display
walter76 1:75827d765e34 178 value = (int)(Count1/TGATE);
walter76 1:75827d765e34 179 Display_6D_write(value); // refresh the main display
walter76 1:75827d765e34 180 }
walter76 0:6d1742703713 181
walter76 0:6d1742703713 182 else
walter76 0:6d1742703713 183 {
walter76 1:75827d765e34 184 // Enable interrupts on rising edge of digital inputs TRIG1 & TRIG2
kd5byb 4:b17c6556cf1f 185
kd5byb 4:b17c6556cf1f 186 PC.printf("started");
walter76 1:75827d765e34 187 TRIG1.rise(&Count1_up);
walter76 1:75827d765e34 188 TRIG2.rise(&Count2_up);
walter76 1:75827d765e34 189
walter76 1:75827d765e34 190 if(gate==0) // show the counter value at the end of the gate time
walter76 1:75827d765e34 191 {
walter76 1:75827d765e34 192 value = (int)(Count1/TGATE);
walter76 1:75827d765e34 193 //value = int(Wheel.getPulses());
walter76 0:6d1742703713 194
walter76 1:75827d765e34 195 Display_6D_write(value); // refresh the main display
walter76 1:75827d765e34 196
walter76 1:75827d765e34 197 Count1 = 0; // clear both counters
walter76 1:75827d765e34 198 Count2 = 0;
walter76 1:75827d765e34 199 gate = TGATE;// and reload the gate time
walter76 1:75827d765e34 200
walter76 1:75827d765e34 201 }
walter76 1:75827d765e34 202
walter76 1:75827d765e34 203 Display_2D_write(gate); // show gate time countdown
walter76 1:75827d765e34 204 gate--;
walter76 1:75827d765e34 205
walter76 1:75827d765e34 206 // Timestamp to PC (debug)
walter76 1:75827d765e34 207 seconds = time(NULL); // get current time
walter76 1:75827d765e34 208 //strftime(Text, 50, "%d-%b-%Y %H:%M:%S", localtime(&seconds));
walter76 1:75827d765e34 209 strftime(Text, 50, "%H:%M:%S", localtime(&seconds));
walter76 1:75827d765e34 210 PC.printf("RTC: %s, CNT1: %7d CNT2: %7d\n",Text, Count1, Count2);
walter76 1:75827d765e34 211
walter76 0:6d1742703713 212 }
walter76 1:75827d765e34 213
walter76 0:6d1742703713 214
walter76 0:6d1742703713 215 return;
walter76 0:6d1742703713 216 }
walter76 0:6d1742703713 217
walter76 0:6d1742703713 218 //---------------------------------------------------------------------------
walter76 1:75827d765e34 219 // Increment CNT1 every time a rising edge is detected on TRIG1 (interrupt)
walter76 1:75827d765e34 220
walter76 1:75827d765e34 221 void Count1_up(void)
walter76 1:75827d765e34 222 {
walter76 1:75827d765e34 223 Count1++;
walter76 1:75827d765e34 224 return;
walter76 1:75827d765e34 225 }
walter76 1:75827d765e34 226
walter76 1:75827d765e34 227
walter76 1:75827d765e34 228 //---------------------------------------------------------------------------
walter76 1:75827d765e34 229 // Increment CNT1 every time a rising edge is detected on TRIG2 (interrupt)
walter76 1:75827d765e34 230
walter76 1:75827d765e34 231 void Count2_up(void)
walter76 1:75827d765e34 232 {
walter76 1:75827d765e34 233 Count2++;
walter76 1:75827d765e34 234 return;
walter76 1:75827d765e34 235 }
walter76 1:75827d765e34 236
walter76 1:75827d765e34 237
walter76 1:75827d765e34 238 //---------------------------------------------------------------------------
walter76 1:75827d765e34 239 //Generates a short beep via BUZZ
walter76 1:75827d765e34 240
walter76 1:75827d765e34 241 void Beep(void)
walter76 1:75827d765e34 242 {
walter76 1:75827d765e34 243 BUZZ = 1; // turn-on the buzzer
walter76 1:75827d765e34 244 wait(0.3); // wait
walter76 1:75827d765e34 245 BUZZ = 0; // turn-off the buzzer
walter76 0:6d1742703713 246 return;
walter76 0:6d1742703713 247 }
walter76 0:6d1742703713 248
walter76 0:6d1742703713 249
walter76 0:6d1742703713 250 //---------------------------------------------------------------------------
walter76 1:75827d765e34 251 //Write to 74HC595 (LEDs) - Take care to avoid conflict with MAX7219
walter76 1:75827d765e34 252
walter76 1:75827d765e34 253 void LEDs_write(unsigned short data_val)
walter76 1:75827d765e34 254 {
walter76 1:75827d765e34 255 // Update 74HC595 shift registers
walter76 1:75827d765e34 256 unsigned short mask;
walter76 1:75827d765e34 257
walter76 1:75827d765e34 258 SCK = 0;
walter76 1:75827d765e34 259 wait_us(DT);
walter76 1:75827d765e34 260 CS2 = 0;
walter76 1:75827d765e34 261
walter76 1:75827d765e34 262 for(mask = 0x80; mask!= 0; mask>>= 1)
walter76 1:75827d765e34 263 {
walter76 1:75827d765e34 264 wait_us(DT);
walter76 1:75827d765e34 265 SCK = 0;
walter76 1:75827d765e34 266 if(mask & data_val)
walter76 1:75827d765e34 267 MOSI = 0;
walter76 1:75827d765e34 268 else
walter76 1:75827d765e34 269 MOSI = 1;
walter76 1:75827d765e34 270 wait_us(DT);
walter76 1:75827d765e34 271 SCK = 1;
walter76 1:75827d765e34 272 }
walter76 1:75827d765e34 273
walter76 1:75827d765e34 274 SCK = 0;
walter76 1:75827d765e34 275 wait_us(DT);
walter76 1:75827d765e34 276 CS2 = 1;
walter76 1:75827d765e34 277
walter76 0:6d1742703713 278 return;
walter76 0:6d1742703713 279 }
walter76 0:6d1742703713 280
walter76 0:6d1742703713 281
walter76 1:75827d765e34 282 //---------------------------------------------------------------------------
walter76 1:75827d765e34 283 // Initialize the MAX7219
walter76 1:75827d765e34 284
walter76 1:75827d765e34 285 void Display_init(void)
walter76 1:75827d765e34 286 {
walter76 1:75827d765e34 287 uint8_t i;
walter76 1:75827d765e34 288 uint16_t mask;
walter76 1:75827d765e34 289 uint16_t data_to_send[6] = {SHUTDOWN, TURN_ON, DEC_MODE, BRIGHTNESS, SCAN_LIM, TEST};
walter76 1:75827d765e34 290 //{SHUTDOWN, TURN_ON, DEC_MODE, BRIGHTNESS, SCAN_LIM, TEST};
walter76 1:75827d765e34 291 for(i = 0; i <6; i++)
walter76 1:75827d765e34 292 {
walter76 1:75827d765e34 293 CS1 = 0;
walter76 1:75827d765e34 294
walter76 1:75827d765e34 295 for(mask = 0x8000; mask!= 0; mask>>= 1)
walter76 1:75827d765e34 296 {
walter76 1:75827d765e34 297 wait_us(DT);
walter76 1:75827d765e34 298 SCK = 0;
walter76 1:75827d765e34 299 if(mask & data_to_send[i])
walter76 1:75827d765e34 300 MOSI = 1;
walter76 1:75827d765e34 301 else
walter76 1:75827d765e34 302 MOSI = 0;
walter76 1:75827d765e34 303 wait_us(DT);
walter76 1:75827d765e34 304 SCK = 1;
walter76 1:75827d765e34 305 }
walter76 1:75827d765e34 306
walter76 1:75827d765e34 307 wait_us(DT);
walter76 1:75827d765e34 308 SCK = 0;
walter76 1:75827d765e34 309 wait_us(DT);
walter76 1:75827d765e34 310 CS1 = 1;
walter76 1:75827d765e34 311 }
walter76 1:75827d765e34 312
walter76 1:75827d765e34 313 return;
walter76 1:75827d765e34 314 }
walter76 1:75827d765e34 315
walter76 1:75827d765e34 316
walter76 1:75827d765e34 317 //---------------------------------------------------------------------------
walter76 1:75827d765e34 318 // Refresh the 6 digits of the main display
walter76 1:75827d765e34 319
walter76 1:75827d765e34 320 void Display_6D_write(uint8_t value)
walter76 1:75827d765e34 321 {
walter76 1:75827d765e34 322
walter76 1:75827d765e34 323 uint8_t digit;
walter76 1:75827d765e34 324 uint16_t mask, data_to_send;
walter76 1:75827d765e34 325 char TextString[6];
walter76 1:75827d765e34 326
walter76 1:75827d765e34 327 // int to string, then string to digits
walter76 1:75827d765e34 328
walter76 1:75827d765e34 329 sprintf(TextString, "%6d", value); // int to string
walter76 1:75827d765e34 330
walter76 1:75827d765e34 331 for(uint8_t i=0; i<6; i++)
walter76 1:75827d765e34 332 {
walter76 1:75827d765e34 333 if(TextString[i] == ' ') // blank empty digits
walter76 1:75827d765e34 334 Disp_Digit[i] = 0xFF;
walter76 1:75827d765e34 335 else
walter76 1:75827d765e34 336 Disp_Digit[i] = TextString[i]-'0';
walter76 1:75827d765e34 337 }
walter76 1:75827d765e34 338
walter76 1:75827d765e34 339 // write to chip
walter76 1:75827d765e34 340
walter76 1:75827d765e34 341 SCK = 0;
walter76 1:75827d765e34 342 wait_us(DT);
walter76 1:75827d765e34 343
walter76 1:75827d765e34 344 for(digit = 1; digit <7; digit++)
walter76 1:75827d765e34 345 {
walter76 1:75827d765e34 346 // each stream consists of digit address and data to show
walter76 1:75827d765e34 347 data_to_send = 7-digit;
walter76 1:75827d765e34 348 data_to_send<<=8;
walter76 1:75827d765e34 349 data_to_send = data_to_send | Disp_Digit[digit-1];
walter76 1:75827d765e34 350
walter76 1:75827d765e34 351 CS1 = 0;
walter76 1:75827d765e34 352
walter76 1:75827d765e34 353 for(mask = 0x8000; mask!= 0; mask>>= 1)
walter76 1:75827d765e34 354 {
walter76 1:75827d765e34 355 wait_us(DT);
walter76 1:75827d765e34 356 SCK = 0;
walter76 1:75827d765e34 357 if(mask & data_to_send)
walter76 1:75827d765e34 358 MOSI = 1;
walter76 1:75827d765e34 359 else
walter76 1:75827d765e34 360 MOSI = 0;
walter76 1:75827d765e34 361
walter76 1:75827d765e34 362 wait_us(DT);
walter76 1:75827d765e34 363 SCK = 1;
walter76 1:75827d765e34 364 }
walter76 1:75827d765e34 365
walter76 1:75827d765e34 366 wait_us(DT);
walter76 1:75827d765e34 367 SCK = 0;
walter76 1:75827d765e34 368 wait_us(DT);
walter76 1:75827d765e34 369 CS1 = 1;
walter76 1:75827d765e34 370 }
walter76 1:75827d765e34 371
walter76 1:75827d765e34 372 return;
walter76 1:75827d765e34 373 }
walter76 1:75827d765e34 374
walter76 1:75827d765e34 375
walter76 1:75827d765e34 376 //---------------------------------------------------------------------------
walter76 1:75827d765e34 377 // Refresh the 2 digits of the gate display
walter76 1:75827d765e34 378
walter76 1:75827d765e34 379 void Display_2D_write(unsigned short value)
walter76 1:75827d765e34 380 {
walter76 1:75827d765e34 381
walter76 1:75827d765e34 382 uint8_t digit;
walter76 1:75827d765e34 383 uint16_t mask, data_to_send;
walter76 1:75827d765e34 384 char TextString[2];
walter76 1:75827d765e34 385
walter76 1:75827d765e34 386 // int to string, then string to digits
walter76 1:75827d765e34 387
walter76 1:75827d765e34 388 sprintf(TextString, "%2d", value); // int to string
walter76 1:75827d765e34 389
walter76 1:75827d765e34 390 if(TextString[0] == ' ') // blank empty digits
walter76 1:75827d765e34 391 Disp_Digit[7] = 0xFF;
walter76 1:75827d765e34 392 else
walter76 1:75827d765e34 393 Disp_Digit[7] = TextString[0] - '0';
walter76 1:75827d765e34 394
walter76 1:75827d765e34 395 Disp_Digit[6] = TextString[1] - '0';
walter76 1:75827d765e34 396
walter76 1:75827d765e34 397 // write to chip
walter76 1:75827d765e34 398
walter76 1:75827d765e34 399 SCK = 0;
walter76 1:75827d765e34 400 wait_us(DT);
walter76 1:75827d765e34 401
walter76 1:75827d765e34 402 for(digit = 7; digit <9; digit++)
walter76 1:75827d765e34 403 {
walter76 1:75827d765e34 404 // each stream consists of digit address and data to show
walter76 1:75827d765e34 405 data_to_send = digit;
walter76 1:75827d765e34 406 data_to_send<<=8;
walter76 1:75827d765e34 407 data_to_send = data_to_send | Disp_Digit[digit-1];
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
walter76 1:75827d765e34 416 if(mask & data_to_send)
walter76 1:75827d765e34 417 MOSI = 1;
walter76 1:75827d765e34 418 else
walter76 1:75827d765e34 419 MOSI = 0;
walter76 1:75827d765e34 420
walter76 1:75827d765e34 421 wait_us(DT);
walter76 1:75827d765e34 422 SCK = 1;
walter76 1:75827d765e34 423 }
walter76 1:75827d765e34 424
walter76 1:75827d765e34 425 wait_us(DT);
walter76 1:75827d765e34 426 SCK = 0;
walter76 1:75827d765e34 427 wait_us(DT);
walter76 1:75827d765e34 428 CS1 = 1;
walter76 1:75827d765e34 429 }
walter76 1:75827d765e34 430
walter76 1:75827d765e34 431 return;
walter76 1:75827d765e34 432 }
walter76 1:75827d765e34 433
walter76 1:75827d765e34 434 //-------- END OF FILE --------------
walter76 1:75827d765e34 435 //==============================================================================
walter76 1:75827d765e34 436
walter76 1:75827d765e34 437
walter76 1:75827d765e34 438