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@0:6d1742703713, 2018-02-13 (annotated)
- Committer:
- walter76
- Date:
- Tue Feb 13 13:30:08 2018 +0000
- Revision:
- 0:6d1742703713
- Child:
- 1:75827d765e34
GEO-COUNTER test version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
walter76 | 0:6d1742703713 | 1 | #include "mbed.h" |
walter76 | 0:6d1742703713 | 2 | #include <string> |
walter76 | 0:6d1742703713 | 3 | #include "max7219.h" |
walter76 | 0:6d1742703713 | 4 | #include "QEI.h" |
walter76 | 0:6d1742703713 | 5 | |
walter76 | 0:6d1742703713 | 6 | #define TGATE 1 |
walter76 | 0:6d1742703713 | 7 | #define MAX_VAL 999999 |
walter76 | 0:6d1742703713 | 8 | |
walter76 | 0:6d1742703713 | 9 | InterruptIn TRIG1 (D3); // Counter 1 trigger |
walter76 | 0:6d1742703713 | 10 | InterruptIn TRIG2 (D6); // Counter 2 trigger |
walter76 | 0:6d1742703713 | 11 | PwmOut PWM (D10); // PWM output |
walter76 | 0:6d1742703713 | 12 | DigitalOut BUZZ (D2); // Buzzer |
walter76 | 0:6d1742703713 | 13 | DigitalOut AUX (D2); // AUX control for GPS module |
walter76 | 0:6d1742703713 | 14 | DigitalIn QEIPB (D9); // Quadrature encoder pushbutton |
walter76 | 0:6d1742703713 | 15 | |
walter76 | 0:6d1742703713 | 16 | QEI Wheel(D11, D12, NC, 16); // Quadrature encoder |
walter76 | 0:6d1742703713 | 17 | I2C i2c(D4, D5); // I2C port |
walter76 | 0:6d1742703713 | 18 | Ticker Sec_Beat; |
walter76 | 0:6d1742703713 | 19 | Max7219 Display(A6, A5, A4, A3); //LED diplay driver (MOSI, MISO, SCK, SS) |
walter76 | 0:6d1742703713 | 20 | Serial PC(USBTX, USBRX); // Virtual COM via USB |
walter76 | 0:6d1742703713 | 21 | Serial GPS(D1, D0); |
walter76 | 0:6d1742703713 | 22 | |
walter76 | 0:6d1742703713 | 23 | // Global variables |
walter76 | 0:6d1742703713 | 24 | uint8_t Digit_Disp[8]; |
walter76 | 0:6d1742703713 | 25 | uint16_t Stream; |
walter76 | 0:6d1742703713 | 26 | |
walter76 | 0:6d1742703713 | 27 | time_t seconds; // RTC timestamp |
walter76 | 0:6d1742703713 | 28 | |
walter76 | 0:6d1742703713 | 29 | unsigned int value = 0; |
walter76 | 0:6d1742703713 | 30 | uint32_t CNT1, CNT2; // pulse counters |
walter76 | 0:6d1742703713 | 31 | uint8_t gate; |
walter76 | 0:6d1742703713 | 32 | char Text[40]=""; |
walter76 | 0:6d1742703713 | 33 | |
walter76 | 0:6d1742703713 | 34 | // ------------------- Prototypes ----------------------- |
walter76 | 0:6d1742703713 | 35 | void RefreshDisplay(void); |
walter76 | 0:6d1742703713 | 36 | void Update(void); |
walter76 | 0:6d1742703713 | 37 | void CNT1_count(void); |
walter76 | 0:6d1742703713 | 38 | void CNT2_count(void); |
walter76 | 0:6d1742703713 | 39 | |
walter76 | 0:6d1742703713 | 40 | |
walter76 | 0:6d1742703713 | 41 | //============================================================================== |
walter76 | 0:6d1742703713 | 42 | //============================================================================== |
walter76 | 0:6d1742703713 | 43 | |
walter76 | 0:6d1742703713 | 44 | int main() |
walter76 | 0:6d1742703713 | 45 | { |
walter76 | 0:6d1742703713 | 46 | |
walter76 | 0:6d1742703713 | 47 | PC.baud(115200); |
walter76 | 0:6d1742703713 | 48 | PC.printf("Connected...\n"); |
walter76 | 0:6d1742703713 | 49 | GPS.baud(9600); |
walter76 | 0:6d1742703713 | 50 | |
walter76 | 0:6d1742703713 | 51 | max7219_configuration_t cfg = { |
walter76 | 0:6d1742703713 | 52 | .device_number = 1, |
walter76 | 0:6d1742703713 | 53 | .decode_mode = 0xFF, |
walter76 | 0:6d1742703713 | 54 | .intensity = Max7219::MAX7219_INTENSITY_F, // max brightness |
walter76 | 0:6d1742703713 | 55 | .scan_limit = Max7219::MAX7219_SCAN_6 // 6 digits |
walter76 | 0:6d1742703713 | 56 | }; |
walter76 | 0:6d1742703713 | 57 | |
walter76 | 0:6d1742703713 | 58 | Display.init_device(cfg); |
walter76 | 0:6d1742703713 | 59 | Display.enable_device(1); |
walter76 | 0:6d1742703713 | 60 | Display.device_all_on(1); |
walter76 | 0:6d1742703713 | 61 | |
walter76 | 0:6d1742703713 | 62 | set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37 |
walter76 | 0:6d1742703713 | 63 | |
walter76 | 0:6d1742703713 | 64 | //RTC::attach(&Update, RTC::Second); |
walter76 | 0:6d1742703713 | 65 | //RTC::detach(RTC::Second); |
walter76 | 0:6d1742703713 | 66 | |
walter76 | 0:6d1742703713 | 67 | Wheel.reset(); // clear encoder |
walter76 | 0:6d1742703713 | 68 | |
walter76 | 0:6d1742703713 | 69 | PWM.period_ms(3); // set LCD backlight PWM |
walter76 | 0:6d1742703713 | 70 | PWM.write(0.8); |
walter76 | 0:6d1742703713 | 71 | /* |
walter76 | 0:6d1742703713 | 72 | BUZZ.period_ms(30); // set LCD backlight PWM |
walter76 | 0:6d1742703713 | 73 | BUZZ.write(0.2); |
walter76 | 0:6d1742703713 | 74 | */ |
walter76 | 0:6d1742703713 | 75 | Sec_Beat.attach(&Update, 1); |
walter76 | 0:6d1742703713 | 76 | |
walter76 | 0:6d1742703713 | 77 | // enable & attach interrupts on rising edge of digital inputs |
walter76 | 0:6d1742703713 | 78 | TRIG1.rise(&CNT1_count); |
walter76 | 0:6d1742703713 | 79 | TRIG2.rise(&CNT2_count); |
walter76 | 0:6d1742703713 | 80 | |
walter76 | 0:6d1742703713 | 81 | |
walter76 | 0:6d1742703713 | 82 | while(1) |
walter76 | 0:6d1742703713 | 83 | { |
walter76 | 0:6d1742703713 | 84 | // do nothing |
walter76 | 0:6d1742703713 | 85 | |
walter76 | 0:6d1742703713 | 86 | } |
walter76 | 0:6d1742703713 | 87 | } |
walter76 | 0:6d1742703713 | 88 | |
walter76 | 0:6d1742703713 | 89 | |
walter76 | 0:6d1742703713 | 90 | // ------------- Refresh Display ------------------------ |
walter76 | 0:6d1742703713 | 91 | |
walter76 | 0:6d1742703713 | 92 | void RefreshDisplay(void) |
walter76 | 0:6d1742703713 | 93 | { |
walter76 | 0:6d1742703713 | 94 | Display.write_digit(1, 1, Digit_Disp[6]); // device, digit, data |
walter76 | 0:6d1742703713 | 95 | Display.write_digit(1, 2, Digit_Disp[5]); // device, digit, data |
walter76 | 0:6d1742703713 | 96 | Display.write_digit(1, 3, Digit_Disp[4]); // device, digit, data |
walter76 | 0:6d1742703713 | 97 | Display.write_digit(1, 4, Digit_Disp[3]); // device, digit, data |
walter76 | 0:6d1742703713 | 98 | Display.write_digit(1, 5, Digit_Disp[2]); // device, digit, data |
walter76 | 0:6d1742703713 | 99 | Display.write_digit(1, 6, Digit_Disp[1]); // device, digit, data |
walter76 | 0:6d1742703713 | 100 | Display.write_digit(1, 7, Digit_Disp[0]); // device, digit, data |
walter76 | 0:6d1742703713 | 101 | |
walter76 | 0:6d1742703713 | 102 | return; |
walter76 | 0:6d1742703713 | 103 | } |
walter76 | 0:6d1742703713 | 104 | |
walter76 | 0:6d1742703713 | 105 | |
walter76 | 0:6d1742703713 | 106 | |
walter76 | 0:6d1742703713 | 107 | //============================================================================== |
walter76 | 0:6d1742703713 | 108 | |
walter76 | 0:6d1742703713 | 109 | void Update() // refresh display data |
walter76 | 0:6d1742703713 | 110 | { |
walter76 | 0:6d1742703713 | 111 | char TextString[9]; |
walter76 | 0:6d1742703713 | 112 | gate++; |
walter76 | 0:6d1742703713 | 113 | |
walter76 | 0:6d1742703713 | 114 | if(QEIPB) |
walter76 | 0:6d1742703713 | 115 | AUX = 1; |
walter76 | 0:6d1742703713 | 116 | else |
walter76 | 0:6d1742703713 | 117 | AUX = 0; |
walter76 | 0:6d1742703713 | 118 | |
walter76 | 0:6d1742703713 | 119 | if(gate==TGATE) |
walter76 | 0:6d1742703713 | 120 | { |
walter76 | 0:6d1742703713 | 121 | //value = (int)(CNT1/TGATE); |
walter76 | 0:6d1742703713 | 122 | value = int(Wheel.getPulses()); |
walter76 | 0:6d1742703713 | 123 | |
walter76 | 0:6d1742703713 | 124 | if(value<MAX_VAL) |
walter76 | 0:6d1742703713 | 125 | { |
walter76 | 0:6d1742703713 | 126 | snprintf(TextString, 8, "%7d", value); // int to string |
walter76 | 0:6d1742703713 | 127 | seconds = time(NULL); |
walter76 | 0:6d1742703713 | 128 | strftime(Text, 50, "%d-%b-%Y %H:%M:%S", localtime(&seconds)); |
walter76 | 0:6d1742703713 | 129 | PC.printf("RTC: %s, CNT1: %7d CNT2: %7d\n",Text, CNT1, CNT2); |
walter76 | 0:6d1742703713 | 130 | |
walter76 | 0:6d1742703713 | 131 | for(uint8_t i=0; i<7; i++) |
walter76 | 0:6d1742703713 | 132 | { |
walter76 | 0:6d1742703713 | 133 | if(TextString[i]==' ') // blanking empty digits |
walter76 | 0:6d1742703713 | 134 | TextString[i]= 0xFF; |
walter76 | 0:6d1742703713 | 135 | else |
walter76 | 0:6d1742703713 | 136 | TextString[i]=TextString[i]-'0'; |
walter76 | 0:6d1742703713 | 137 | |
walter76 | 0:6d1742703713 | 138 | Digit_Disp[i] = TextString[i]; |
walter76 | 0:6d1742703713 | 139 | |
walter76 | 0:6d1742703713 | 140 | } |
walter76 | 0:6d1742703713 | 141 | } |
walter76 | 0:6d1742703713 | 142 | |
walter76 | 0:6d1742703713 | 143 | else |
walter76 | 0:6d1742703713 | 144 | { |
walter76 | 0:6d1742703713 | 145 | for(uint8_t i=0; i<7; i++) // all minus |
walter76 | 0:6d1742703713 | 146 | Digit_Disp[i] = 10; |
walter76 | 0:6d1742703713 | 147 | } |
walter76 | 0:6d1742703713 | 148 | |
walter76 | 0:6d1742703713 | 149 | RefreshDisplay(); |
walter76 | 0:6d1742703713 | 150 | |
walter76 | 0:6d1742703713 | 151 | CNT1=0; |
walter76 | 0:6d1742703713 | 152 | CNT2=0; |
walter76 | 0:6d1742703713 | 153 | gate = 0; |
walter76 | 0:6d1742703713 | 154 | } |
walter76 | 0:6d1742703713 | 155 | else |
walter76 | 0:6d1742703713 | 156 | { |
walter76 | 0:6d1742703713 | 157 | value = TGATE - gate; |
walter76 | 0:6d1742703713 | 158 | snprintf(TextString, 8, "%7d", value); |
walter76 | 0:6d1742703713 | 159 | for(uint8_t i=6; i<8; i++) |
walter76 | 0:6d1742703713 | 160 | { |
walter76 | 0:6d1742703713 | 161 | if(TextString[i]==' ') // blanking empty digits |
walter76 | 0:6d1742703713 | 162 | TextString[i]= 0xFF; |
walter76 | 0:6d1742703713 | 163 | else |
walter76 | 0:6d1742703713 | 164 | TextString[i]=TextString[i]-'0'; |
walter76 | 0:6d1742703713 | 165 | } |
walter76 | 0:6d1742703713 | 166 | |
walter76 | 0:6d1742703713 | 167 | //Digit_Disp[1] = TextString[6]; |
walter76 | 0:6d1742703713 | 168 | //Digit_Disp[0] = TextString[7]; |
walter76 | 0:6d1742703713 | 169 | |
walter76 | 0:6d1742703713 | 170 | RefreshDisplay(); |
walter76 | 0:6d1742703713 | 171 | } |
walter76 | 0:6d1742703713 | 172 | |
walter76 | 0:6d1742703713 | 173 | |
walter76 | 0:6d1742703713 | 174 | return; |
walter76 | 0:6d1742703713 | 175 | } |
walter76 | 0:6d1742703713 | 176 | |
walter76 | 0:6d1742703713 | 177 | //--------------------------------------------------------------------------- |
walter76 | 0:6d1742703713 | 178 | void CNT1_count(void) |
walter76 | 0:6d1742703713 | 179 | { //function to call upon interrupt |
walter76 | 0:6d1742703713 | 180 | CNT1++; //increment counter object |
walter76 | 0:6d1742703713 | 181 | return; |
walter76 | 0:6d1742703713 | 182 | } |
walter76 | 0:6d1742703713 | 183 | |
walter76 | 0:6d1742703713 | 184 | |
walter76 | 0:6d1742703713 | 185 | //--------------------------------------------------------------------------- |
walter76 | 0:6d1742703713 | 186 | void CNT2_count(void) |
walter76 | 0:6d1742703713 | 187 | { //function to call upon interrupt |
walter76 | 0:6d1742703713 | 188 | CNT2++; //increment counter object |
walter76 | 0:6d1742703713 | 189 | return; |
walter76 | 0:6d1742703713 | 190 | } |
walter76 | 0:6d1742703713 | 191 | |
walter76 | 0:6d1742703713 | 192 |