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