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:
Tue Aug 28 14:29:03 2018 -0700
Revision:
3:23b0185c7e7b
Parent:
2:ac0ed3d84d44
Child:
4:b17c6556cf1f
This is a trivial change from Sec_Beat.attach to Sec_Beat.attach_us to test out my development environment.

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
walter76 1:75827d765e34 76 bool Stopped = 1; // 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
walter76 0:6d1742703713 152
walter76 1:75827d765e34 153 if(ADC_val<0.1) // RESET pushbutton pressed
walter76 1:75827d765e34 154 {
walter76 1:75827d765e34 155 Count1 = 0; // clear counters
walter76 1:75827d765e34 156 Count2 = 0;
walter76 1:75827d765e34 157 }
walter76 1:75827d765e34 158
walter76 1:75827d765e34 159 if((ADC_val>0.4)&&(ADC_val<0.6)) // START/STOP pushbutton pressed
walter76 1:75827d765e34 160 Stopped=!Stopped; // toggle status
walter76 1:75827d765e34 161
walter76 1:75827d765e34 162 if(Stopped)
walter76 1:75827d765e34 163 {
walter76 1:75827d765e34 164 // disable interrupts on TRIG1 and TRIG2
walter76 1:75827d765e34 165 TRIG1.rise(NULL);
walter76 1:75827d765e34 166 TRIG2.rise(NULL);
walter76 0:6d1742703713 167
walter76 1:75827d765e34 168 // show zero gate time
walter76 1:75827d765e34 169 gate = 0;
walter76 1:75827d765e34 170 Display_2D_write(gate);
walter76 0:6d1742703713 171
walter76 1:75827d765e34 172 // show selected content on main display
walter76 1:75827d765e34 173 value = (int)(Count1/TGATE);
walter76 1:75827d765e34 174 Display_6D_write(value); // refresh the main display
walter76 1:75827d765e34 175 }
walter76 0:6d1742703713 176
walter76 0:6d1742703713 177 else
walter76 0:6d1742703713 178 {
walter76 1:75827d765e34 179 // Enable interrupts on rising edge of digital inputs TRIG1 & TRIG2
walter76 1:75827d765e34 180 TRIG1.rise(&Count1_up);
walter76 1:75827d765e34 181 TRIG2.rise(&Count2_up);
walter76 1:75827d765e34 182
walter76 1:75827d765e34 183 if(gate==0) // show the counter value at the end of the gate time
walter76 1:75827d765e34 184 {
walter76 1:75827d765e34 185 value = (int)(Count1/TGATE);
walter76 1:75827d765e34 186 //value = int(Wheel.getPulses());
walter76 0:6d1742703713 187
walter76 1:75827d765e34 188 Display_6D_write(value); // refresh the main display
walter76 1:75827d765e34 189
walter76 1:75827d765e34 190 Count1 = 0; // clear both counters
walter76 1:75827d765e34 191 Count2 = 0;
walter76 1:75827d765e34 192 gate = TGATE;// and reload the gate time
walter76 1:75827d765e34 193
walter76 1:75827d765e34 194 }
walter76 1:75827d765e34 195
walter76 1:75827d765e34 196 Display_2D_write(gate); // show gate time countdown
walter76 1:75827d765e34 197 gate--;
walter76 1:75827d765e34 198
walter76 1:75827d765e34 199 // Timestamp to PC (debug)
walter76 1:75827d765e34 200 seconds = time(NULL); // get current time
walter76 1:75827d765e34 201 //strftime(Text, 50, "%d-%b-%Y %H:%M:%S", localtime(&seconds));
walter76 1:75827d765e34 202 strftime(Text, 50, "%H:%M:%S", localtime(&seconds));
walter76 1:75827d765e34 203 PC.printf("RTC: %s, CNT1: %7d CNT2: %7d\n",Text, Count1, Count2);
walter76 1:75827d765e34 204
walter76 0:6d1742703713 205 }
walter76 1:75827d765e34 206
walter76 0:6d1742703713 207
walter76 0:6d1742703713 208 return;
walter76 0:6d1742703713 209 }
walter76 0:6d1742703713 210
walter76 0:6d1742703713 211 //---------------------------------------------------------------------------
walter76 1:75827d765e34 212 // Increment CNT1 every time a rising edge is detected on TRIG1 (interrupt)
walter76 1:75827d765e34 213
walter76 1:75827d765e34 214 void Count1_up(void)
walter76 1:75827d765e34 215 {
walter76 1:75827d765e34 216 Count1++;
walter76 1:75827d765e34 217 return;
walter76 1:75827d765e34 218 }
walter76 1:75827d765e34 219
walter76 1:75827d765e34 220
walter76 1:75827d765e34 221 //---------------------------------------------------------------------------
walter76 1:75827d765e34 222 // Increment CNT1 every time a rising edge is detected on TRIG2 (interrupt)
walter76 1:75827d765e34 223
walter76 1:75827d765e34 224 void Count2_up(void)
walter76 1:75827d765e34 225 {
walter76 1:75827d765e34 226 Count2++;
walter76 1:75827d765e34 227 return;
walter76 1:75827d765e34 228 }
walter76 1:75827d765e34 229
walter76 1:75827d765e34 230
walter76 1:75827d765e34 231 //---------------------------------------------------------------------------
walter76 1:75827d765e34 232 //Generates a short beep via BUZZ
walter76 1:75827d765e34 233
walter76 1:75827d765e34 234 void Beep(void)
walter76 1:75827d765e34 235 {
walter76 1:75827d765e34 236 BUZZ = 1; // turn-on the buzzer
walter76 1:75827d765e34 237 wait(0.3); // wait
walter76 1:75827d765e34 238 BUZZ = 0; // turn-off the buzzer
walter76 0:6d1742703713 239 return;
walter76 0:6d1742703713 240 }
walter76 0:6d1742703713 241
walter76 0:6d1742703713 242
walter76 0:6d1742703713 243 //---------------------------------------------------------------------------
walter76 1:75827d765e34 244 //Write to 74HC595 (LEDs) - Take care to avoid conflict with MAX7219
walter76 1:75827d765e34 245
walter76 1:75827d765e34 246 void LEDs_write(unsigned short data_val)
walter76 1:75827d765e34 247 {
walter76 1:75827d765e34 248 // Update 74HC595 shift registers
walter76 1:75827d765e34 249 unsigned short mask;
walter76 1:75827d765e34 250
walter76 1:75827d765e34 251 SCK = 0;
walter76 1:75827d765e34 252 wait_us(DT);
walter76 1:75827d765e34 253 CS2 = 0;
walter76 1:75827d765e34 254
walter76 1:75827d765e34 255 for(mask = 0x80; mask!= 0; mask>>= 1)
walter76 1:75827d765e34 256 {
walter76 1:75827d765e34 257 wait_us(DT);
walter76 1:75827d765e34 258 SCK = 0;
walter76 1:75827d765e34 259 if(mask & data_val)
walter76 1:75827d765e34 260 MOSI = 0;
walter76 1:75827d765e34 261 else
walter76 1:75827d765e34 262 MOSI = 1;
walter76 1:75827d765e34 263 wait_us(DT);
walter76 1:75827d765e34 264 SCK = 1;
walter76 1:75827d765e34 265 }
walter76 1:75827d765e34 266
walter76 1:75827d765e34 267 SCK = 0;
walter76 1:75827d765e34 268 wait_us(DT);
walter76 1:75827d765e34 269 CS2 = 1;
walter76 1:75827d765e34 270
walter76 0:6d1742703713 271 return;
walter76 0:6d1742703713 272 }
walter76 0:6d1742703713 273
walter76 0:6d1742703713 274
walter76 1:75827d765e34 275 //---------------------------------------------------------------------------
walter76 1:75827d765e34 276 // Initialize the MAX7219
walter76 1:75827d765e34 277
walter76 1:75827d765e34 278 void Display_init(void)
walter76 1:75827d765e34 279 {
walter76 1:75827d765e34 280 uint8_t i;
walter76 1:75827d765e34 281 uint16_t mask;
walter76 1:75827d765e34 282 uint16_t data_to_send[6] = {SHUTDOWN, TURN_ON, DEC_MODE, BRIGHTNESS, SCAN_LIM, TEST};
walter76 1:75827d765e34 283 //{SHUTDOWN, TURN_ON, DEC_MODE, BRIGHTNESS, SCAN_LIM, TEST};
walter76 1:75827d765e34 284 for(i = 0; i <6; i++)
walter76 1:75827d765e34 285 {
walter76 1:75827d765e34 286 CS1 = 0;
walter76 1:75827d765e34 287
walter76 1:75827d765e34 288 for(mask = 0x8000; mask!= 0; mask>>= 1)
walter76 1:75827d765e34 289 {
walter76 1:75827d765e34 290 wait_us(DT);
walter76 1:75827d765e34 291 SCK = 0;
walter76 1:75827d765e34 292 if(mask & data_to_send[i])
walter76 1:75827d765e34 293 MOSI = 1;
walter76 1:75827d765e34 294 else
walter76 1:75827d765e34 295 MOSI = 0;
walter76 1:75827d765e34 296 wait_us(DT);
walter76 1:75827d765e34 297 SCK = 1;
walter76 1:75827d765e34 298 }
walter76 1:75827d765e34 299
walter76 1:75827d765e34 300 wait_us(DT);
walter76 1:75827d765e34 301 SCK = 0;
walter76 1:75827d765e34 302 wait_us(DT);
walter76 1:75827d765e34 303 CS1 = 1;
walter76 1:75827d765e34 304 }
walter76 1:75827d765e34 305
walter76 1:75827d765e34 306 return;
walter76 1:75827d765e34 307 }
walter76 1:75827d765e34 308
walter76 1:75827d765e34 309
walter76 1:75827d765e34 310 //---------------------------------------------------------------------------
walter76 1:75827d765e34 311 // Refresh the 6 digits of the main display
walter76 1:75827d765e34 312
walter76 1:75827d765e34 313 void Display_6D_write(uint8_t value)
walter76 1:75827d765e34 314 {
walter76 1:75827d765e34 315
walter76 1:75827d765e34 316 uint8_t digit;
walter76 1:75827d765e34 317 uint16_t mask, data_to_send;
walter76 1:75827d765e34 318 char TextString[6];
walter76 1:75827d765e34 319
walter76 1:75827d765e34 320 // int to string, then string to digits
walter76 1:75827d765e34 321
walter76 1:75827d765e34 322 sprintf(TextString, "%6d", value); // int to string
walter76 1:75827d765e34 323
walter76 1:75827d765e34 324 for(uint8_t i=0; i<6; i++)
walter76 1:75827d765e34 325 {
walter76 1:75827d765e34 326 if(TextString[i] == ' ') // blank empty digits
walter76 1:75827d765e34 327 Disp_Digit[i] = 0xFF;
walter76 1:75827d765e34 328 else
walter76 1:75827d765e34 329 Disp_Digit[i] = TextString[i]-'0';
walter76 1:75827d765e34 330 }
walter76 1:75827d765e34 331
walter76 1:75827d765e34 332 // write to chip
walter76 1:75827d765e34 333
walter76 1:75827d765e34 334 SCK = 0;
walter76 1:75827d765e34 335 wait_us(DT);
walter76 1:75827d765e34 336
walter76 1:75827d765e34 337 for(digit = 1; digit <7; digit++)
walter76 1:75827d765e34 338 {
walter76 1:75827d765e34 339 // each stream consists of digit address and data to show
walter76 1:75827d765e34 340 data_to_send = 7-digit;
walter76 1:75827d765e34 341 data_to_send<<=8;
walter76 1:75827d765e34 342 data_to_send = data_to_send | Disp_Digit[digit-1];
walter76 1:75827d765e34 343
walter76 1:75827d765e34 344 CS1 = 0;
walter76 1:75827d765e34 345
walter76 1:75827d765e34 346 for(mask = 0x8000; mask!= 0; mask>>= 1)
walter76 1:75827d765e34 347 {
walter76 1:75827d765e34 348 wait_us(DT);
walter76 1:75827d765e34 349 SCK = 0;
walter76 1:75827d765e34 350 if(mask & data_to_send)
walter76 1:75827d765e34 351 MOSI = 1;
walter76 1:75827d765e34 352 else
walter76 1:75827d765e34 353 MOSI = 0;
walter76 1:75827d765e34 354
walter76 1:75827d765e34 355 wait_us(DT);
walter76 1:75827d765e34 356 SCK = 1;
walter76 1:75827d765e34 357 }
walter76 1:75827d765e34 358
walter76 1:75827d765e34 359 wait_us(DT);
walter76 1:75827d765e34 360 SCK = 0;
walter76 1:75827d765e34 361 wait_us(DT);
walter76 1:75827d765e34 362 CS1 = 1;
walter76 1:75827d765e34 363 }
walter76 1:75827d765e34 364
walter76 1:75827d765e34 365 return;
walter76 1:75827d765e34 366 }
walter76 1:75827d765e34 367
walter76 1:75827d765e34 368
walter76 1:75827d765e34 369 //---------------------------------------------------------------------------
walter76 1:75827d765e34 370 // Refresh the 2 digits of the gate display
walter76 1:75827d765e34 371
walter76 1:75827d765e34 372 void Display_2D_write(unsigned short value)
walter76 1:75827d765e34 373 {
walter76 1:75827d765e34 374
walter76 1:75827d765e34 375 uint8_t digit;
walter76 1:75827d765e34 376 uint16_t mask, data_to_send;
walter76 1:75827d765e34 377 char TextString[2];
walter76 1:75827d765e34 378
walter76 1:75827d765e34 379 // int to string, then string to digits
walter76 1:75827d765e34 380
walter76 1:75827d765e34 381 sprintf(TextString, "%2d", value); // int to string
walter76 1:75827d765e34 382
walter76 1:75827d765e34 383 if(TextString[0] == ' ') // blank empty digits
walter76 1:75827d765e34 384 Disp_Digit[7] = 0xFF;
walter76 1:75827d765e34 385 else
walter76 1:75827d765e34 386 Disp_Digit[7] = TextString[0] - '0';
walter76 1:75827d765e34 387
walter76 1:75827d765e34 388 Disp_Digit[6] = TextString[1] - '0';
walter76 1:75827d765e34 389
walter76 1:75827d765e34 390 // write to chip
walter76 1:75827d765e34 391
walter76 1:75827d765e34 392 SCK = 0;
walter76 1:75827d765e34 393 wait_us(DT);
walter76 1:75827d765e34 394
walter76 1:75827d765e34 395 for(digit = 7; digit <9; digit++)
walter76 1:75827d765e34 396 {
walter76 1:75827d765e34 397 // each stream consists of digit address and data to show
walter76 1:75827d765e34 398 data_to_send = digit;
walter76 1:75827d765e34 399 data_to_send<<=8;
walter76 1:75827d765e34 400 data_to_send = data_to_send | Disp_Digit[digit-1];
walter76 1:75827d765e34 401
walter76 1:75827d765e34 402 CS1 = 0;
walter76 1:75827d765e34 403
walter76 1:75827d765e34 404 for(mask = 0x8000; mask!= 0; mask>>= 1)
walter76 1:75827d765e34 405 {
walter76 1:75827d765e34 406 wait_us(DT);
walter76 1:75827d765e34 407 SCK = 0;
walter76 1:75827d765e34 408
walter76 1:75827d765e34 409 if(mask & data_to_send)
walter76 1:75827d765e34 410 MOSI = 1;
walter76 1:75827d765e34 411 else
walter76 1:75827d765e34 412 MOSI = 0;
walter76 1:75827d765e34 413
walter76 1:75827d765e34 414 wait_us(DT);
walter76 1:75827d765e34 415 SCK = 1;
walter76 1:75827d765e34 416 }
walter76 1:75827d765e34 417
walter76 1:75827d765e34 418 wait_us(DT);
walter76 1:75827d765e34 419 SCK = 0;
walter76 1:75827d765e34 420 wait_us(DT);
walter76 1:75827d765e34 421 CS1 = 1;
walter76 1:75827d765e34 422 }
walter76 1:75827d765e34 423
walter76 1:75827d765e34 424 return;
walter76 1:75827d765e34 425 }
walter76 1:75827d765e34 426
walter76 1:75827d765e34 427 //-------- END OF FILE --------------
walter76 1:75827d765e34 428 //==============================================================================
walter76 1:75827d765e34 429
walter76 1:75827d765e34 430
walter76 1:75827d765e34 431