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