Charles Young's development fork. Going forward I only want to push mature code to main repository.
Fork of GEO_COUNTER_L432KC by
Diff: main.cpp
- Revision:
- 0:6d1742703713
- Child:
- 1:75827d765e34
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Feb 13 13:30:08 2018 +0000 @@ -0,0 +1,192 @@ +#include "mbed.h" +#include <string> +#include "max7219.h" +#include "QEI.h" + +#define TGATE 1 +#define MAX_VAL 999999 + +InterruptIn TRIG1 (D3); // Counter 1 trigger +InterruptIn TRIG2 (D6); // Counter 2 trigger +PwmOut PWM (D10); // PWM output +DigitalOut BUZZ (D2); // Buzzer +DigitalOut AUX (D2); // AUX control for GPS module +DigitalIn QEIPB (D9); // Quadrature encoder pushbutton + +QEI Wheel(D11, D12, NC, 16); // Quadrature encoder +I2C i2c(D4, D5); // I2C port +Ticker Sec_Beat; +Max7219 Display(A6, A5, A4, A3); //LED diplay driver (MOSI, MISO, SCK, SS) +Serial PC(USBTX, USBRX); // Virtual COM via USB +Serial GPS(D1, D0); + +// Global variables +uint8_t Digit_Disp[8]; +uint16_t Stream; + +time_t seconds; // RTC timestamp + +unsigned int value = 0; +uint32_t CNT1, CNT2; // pulse counters +uint8_t gate; +char Text[40]=""; + +// ------------------- Prototypes ----------------------- +void RefreshDisplay(void); +void Update(void); +void CNT1_count(void); +void CNT2_count(void); + + +//============================================================================== +//============================================================================== + +int main() +{ + + PC.baud(115200); + PC.printf("Connected...\n"); + GPS.baud(9600); + + max7219_configuration_t cfg = { + .device_number = 1, + .decode_mode = 0xFF, + .intensity = Max7219::MAX7219_INTENSITY_F, // max brightness + .scan_limit = Max7219::MAX7219_SCAN_6 // 6 digits + }; + + Display.init_device(cfg); + Display.enable_device(1); + Display.device_all_on(1); + + set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37 + + //RTC::attach(&Update, RTC::Second); + //RTC::detach(RTC::Second); + + Wheel.reset(); // clear encoder + + PWM.period_ms(3); // set LCD backlight PWM + PWM.write(0.8); + /* + BUZZ.period_ms(30); // set LCD backlight PWM + BUZZ.write(0.2); + */ + Sec_Beat.attach(&Update, 1); + + // enable & attach interrupts on rising edge of digital inputs + TRIG1.rise(&CNT1_count); + TRIG2.rise(&CNT2_count); + + + while(1) + { + // do nothing + + } +} + + +// ------------- Refresh Display ------------------------ + +void RefreshDisplay(void) +{ + Display.write_digit(1, 1, Digit_Disp[6]); // device, digit, data + Display.write_digit(1, 2, Digit_Disp[5]); // device, digit, data + Display.write_digit(1, 3, Digit_Disp[4]); // device, digit, data + Display.write_digit(1, 4, Digit_Disp[3]); // device, digit, data + Display.write_digit(1, 5, Digit_Disp[2]); // device, digit, data + Display.write_digit(1, 6, Digit_Disp[1]); // device, digit, data + Display.write_digit(1, 7, Digit_Disp[0]); // device, digit, data + + return; +} + + + +//============================================================================== + +void Update() // refresh display data +{ + char TextString[9]; + gate++; + + if(QEIPB) + AUX = 1; + else + AUX = 0; + + if(gate==TGATE) + { + //value = (int)(CNT1/TGATE); + value = int(Wheel.getPulses()); + + if(value<MAX_VAL) + { + snprintf(TextString, 8, "%7d", value); // int to string + seconds = time(NULL); + strftime(Text, 50, "%d-%b-%Y %H:%M:%S", localtime(&seconds)); + PC.printf("RTC: %s, CNT1: %7d CNT2: %7d\n",Text, CNT1, CNT2); + + for(uint8_t i=0; i<7; i++) + { + if(TextString[i]==' ') // blanking empty digits + TextString[i]= 0xFF; + else + TextString[i]=TextString[i]-'0'; + + Digit_Disp[i] = TextString[i]; + + } + } + + else + { + for(uint8_t i=0; i<7; i++) // all minus + Digit_Disp[i] = 10; + } + + RefreshDisplay(); + + CNT1=0; + CNT2=0; + gate = 0; + } + else + { + value = TGATE - gate; + snprintf(TextString, 8, "%7d", value); + for(uint8_t i=6; i<8; i++) + { + if(TextString[i]==' ') // blanking empty digits + TextString[i]= 0xFF; + else + TextString[i]=TextString[i]-'0'; + } + + //Digit_Disp[1] = TextString[6]; + //Digit_Disp[0] = TextString[7]; + + RefreshDisplay(); + } + + + return; +} + +//--------------------------------------------------------------------------- +void CNT1_count(void) +{ //function to call upon interrupt + CNT1++; //increment counter object + return; +} + + +//--------------------------------------------------------------------------- +void CNT2_count(void) +{ //function to call upon interrupt + CNT2++; //increment counter object + return; +} + +