From 1ec0f63696c78c77e62f682a6ca242e01295aa26 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Thu, 3 Apr 2025 11:30:58 -0400 Subject: [PATCH] voltage scaling --- main.c | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index e2e2f12..1a8e09b 100644 --- a/main.c +++ b/main.c @@ -35,6 +35,21 @@ volatile uint8_t drawRequested = 1; #define WIDTH LCD_HORIZONTAL_MAX #define PIXELS_PER_DIV 20 +#define VIN_RANGE 3.3 // volts +#define ADC_BITS 12 +#define ADC_OFFSET 30 + +#define VOLTAGE_SCALES 5 +const char * const gVoltageScaleStr[VOLTAGE_SCALES] = { +"100 mV", "200 mV", "500 mV", " 1 V", " 2 V" +}; + +const float gVoltageScale[VOLTAGE_SCALES] = { + 0.1, 0.2, 0.5, 1., 2. +}; + +int RisingTrigger(void); + // start a pwm test signal void start_signal() { // configure M0PWM2, at GPIO PF2, BoosterPack 1 header C1 pin 2 @@ -87,14 +102,22 @@ int main(void) { ButtonInit(); IntMasterEnable(); + uint8_t voltage_scale = 0; + while (true) { // handle buttons Button button = (Button) 0; while (fifo_get(&button)) { switch (button) { - case S2: + case S2: // draw drawRequested = 1; break; + case Up: // next scale + voltage_scale = (voltage_scale + 1) % VOLTAGE_SCALES; + break; + case Down: // previous scale + voltage_scale = (voltage_scale + VOLTAGE_SCALES - 1) % VOLTAGE_SCALES; + break; } } @@ -130,9 +153,15 @@ int main(void) { drawRequested = 0; } + GrContextForegroundSet(&sContext, ClrWheat); + GrStringDraw(&sContext, gVoltageScaleStr[voltage_scale], /*length*/ -1, /*x*/ 0, /*y*/ 0, /*opaque*/ false); + + float fVoltsPerDiv = gVoltageScale[voltage_scale]; + float fScale = (VIN_RANGE * PIXELS_PER_DIV)/((1 << ADC_BITS) * fVoltsPerDiv); + GrContextForegroundSet(&sContext, ClrPink); for(j=0; j> 6) // reduce from twelve to six bits, then flip and center + #define TRANSPOSE(x) (HEIGHT/2) - (int)roundf(fScale * ((int)x - ADC_OFFSET)); uint32_t upper,lower, current, last; if (j==0) { @@ -191,7 +220,6 @@ int main(void) { int RisingTrigger(void) // search for rising edge trigger { - #define ADC_OFFSET 20 int x = gADCBufferIndex - (WIDTH / 2); // half screen width int x_stop = x - ADC_BUFFER_SIZE/2; for (; x > x_stop; x--) {