The project is a fast lock in amplifier (LIA) which can update its output at rate of 1000 measurements/s. It performs digital dual mixing and filtering to obtain a DC value proportional to the AC input signal.
Diff: main.cpp
- Revision:
- 2:c9b24787d5e1
- Parent:
- 1:bf693859586c
- Child:
- 3:dd4eb355f8d9
--- a/main.cpp Mon Aug 21 11:43:03 2017 +0000 +++ b/main.cpp Fri Aug 25 10:22:51 2017 +0000 @@ -3,8 +3,12 @@ int main() { pc.baud(115200); + lcd_intro(); + calibrateJoystick(); ///calibrate joystick + settings_menu(); dref.rise(&voltageRise); /// set interrupt to calculate reference frequency setupK64Fclocks(); + //settings_menu(); /// initialise DAC output dac0_out while (ref_freq < 1e2) { sleep(); @@ -65,7 +69,7 @@ double mavg_filter(int filt_points) { double avg = 0, signal = 0; - double delay = 0.9/(1*ref_freq*filter_points); + //double delay = 0.9/(1*ref_freq*filter_points); for (int i = 0; i < filter_points; i++) { signal = ain.read(); avg = avg + signal; @@ -158,3 +162,87 @@ DAC0->C1 = 0; //reset DAC state DAC0->C0 = DAC_C0_DACEN_MASK | DAC_C0_DACSWTRG_MASK| DAC_C0_DACRFS_MASK; } +void lcd_intro() +{ + lcd.init(); + lcd.setBrightness(0.7); // put LED backlight on 50% + lcd.printString("THE CIRCUIT IS",1,1); + lcd.printString("A FAST LIA!",7,3); + lcd.refresh(); + Timeout timeout; + timeout.attach(&timeout_isr,3); + sleep(); + lcd.clear(); +} + +void settings_menu() +{ + lcd.setBrightness(0.7); // put LED backlight on 50% + menu_ticker.attach(&menu_isr,0.2); + + while (exit_menu == 0) { + if (g_menu_flag == 1) { + g_menu_flag = 0; + + updateJoystick(); + lcd.clear(); + + lcd.printString("Settings:",0,0); + + char gain_char[20]; ///create an array of chars + sprintf(gain_char,"Gain:%.2f",var_gain); ///create string + + char speed_char[20]; ///create an array of chars + sprintf(speed_char,"Speed:%.2f",var_speed); ///create string + + lcd.printString(gain_char,0,2); + lcd.printString(speed_char,0,3); + lcd.refresh(); + } + sleep(); + } +} + +void menu_isr() { + g_menu_flag = 1; +} + +void timeout_isr() {} + +void calibrateJoystick() +{ + // must not move during calibration + joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) + joystick.y0 = yPot; +} + +void updateJoystick() +{ + // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) + joystick.x = xPot - joystick.x0; + joystick.y = yPot - joystick.y0; + // read button state + joystick.button = joy_button; + + // calculate direction depending on x,y values + // tolerance allows a little lee-way in case joystick not exactly in the stated direction + if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { + joystick.direction = CENTRE; + } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { + joystick.direction = UP; + } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { + joystick.direction = DOWN; + } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { + joystick.direction = RIGHT; + } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { + joystick.direction = LEFT; + } /*else if (joystick.y > DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) { + joystick.direction = UP_LEFT; + } else if (joystick.y > DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) { + joystick.direction = UP_RIGHT; + } else if (joystick.y < DIRECTION_TOLERANCE && joystick.x < DIRECTION_TOLERANCE) { + joystick.direction = DOWN_LEFT; + } else if (joystick.y < DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) { + joystick.direction = DOWN_RIGHT; + }*/ +}