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:
- 3:dd4eb355f8d9
- Parent:
- 2:c9b24787d5e1
- Child:
- 4:2264789de7b2
--- a/main.cpp Fri Aug 25 10:22:51 2017 +0000 +++ b/main.cpp Wed Aug 30 15:09:56 2017 +0000 @@ -2,10 +2,10 @@ int main() { - pc.baud(115200); lcd_intro(); calibrateJoystick(); ///calibrate joystick settings_menu(); + lcd.clear(); dref.rise(&voltageRise); /// set interrupt to calculate reference frequency setupK64Fclocks(); //settings_menu(); @@ -13,10 +13,29 @@ while (ref_freq < 1e2) { sleep(); } + + double freq_check = ref_freq*0.001; + double out_speed = 2*var_speed; + + if (freq_check < out_speed) { + lcd.printString("RefFreq Low",0,0); + lcd.printString("OR: ",0,1); + lcd.printString("AnOut too Fast",0,2); + + while (freq_check < out_speed) { + sleep(); + } + } /// make sure frequency is read before we go to the program /// cancel event-triggered rise interrupt, not to interfere with program dref.rise(NULL); pc.printf("Ref_Freq is:%.2f kHz\n\r",ref_freq*0.001); + lcd.clear(); + char refFreq_char[20]; ///create an array of chars + sprintf(refFreq_char,"%.3f",ref_freq); ///create string + + lcd.printString("Ref Freq: ",0,0); + lcd.printString(refFreq_char,35,1); /// constant 6 for correct sampling time /// compensates for delay caused by computations sample_freq = 6*samples16*ref_freq; @@ -25,9 +44,17 @@ initDAC(); delay_freq = ref_freq*amp_points; amplitude_delay = 1/delay_freq; + + wait(1); + lcd.clear(); + lcd.printString("LIA ",0,0); + lcd.printString("In process...",0,1); + lcd.refresh(); /// find the offset of the signal offset_ticker.attach(&offset_isr,0.001); - + + double update_value = 0.0; + while (offset == 0) { if (g_offset_flag == 1) { g_offset_flag = 0; @@ -37,8 +64,9 @@ } offset_ticker.detach(); /// once the offset is calculated detach the offset ticker - /// attach the output ticker to update every 1 ms - output_ticker.attach(&output_isr,0.00099); + /// attach the output ticker to update every x ms + update_value = var_speed*0.001; + output_ticker.attach(&output_isr,update_value); while (true) { // gpo = !gpo; @@ -48,7 +76,7 @@ if (g_output_flag == 1) { g_output_flag = 0; //aout = max(samples16); - aout = 2*max(samples16); + aout = var_gain*max(samples16); /// DC output by taking the maximum value of the mixed signal (R) } } @@ -164,6 +192,11 @@ } void lcd_intro() { + pc.baud(115200); + joyButton.rise(&joyButton_isr); ///assign rise with ISR + joyButton.mode(PullDown); ///use PullDown mode + swButton.rise(&swButton_isr); ///assign rise with ISR + swButton.mode(PullDown); ///use PullDown mode lcd.init(); lcd.setBrightness(0.7); // put LED backlight on 50% lcd.printString("THE CIRCUIT IS",1,1); @@ -185,20 +218,138 @@ g_menu_flag = 0; updateJoystick(); - lcd.clear(); + init_LIA_menu(); + setup_selector(); + + lcd.refresh(); + + + } + sleep(); + } + menu_ticker.detach(); +} + +void init_LIA_menu() { + 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 + + sprintf(gain_char,"%.2f",var_gain); ///create string char speed_char[20]; ///create an array of chars - sprintf(speed_char,"Speed:%.2f",var_speed); ///create string + sprintf(speed_char,"%.2f",var_speed); ///create string - lcd.printString(gain_char,0,2); - lcd.printString(speed_char,0,3); - lcd.refresh(); + lcd.printString("Gain: ",0,1); + lcd.printString(gain_char,35,2); + lcd.printString("Speed (ms)",0,3); + lcd.printString(speed_char,35,4); + lcd.printString("Confirm",0,5); +} + +void setup_selector() +{ + switch (joystick.direction) { ///check the direction of joystick + case UP: + menu_option--; + break; + case DOWN: + menu_option++; + break; + case RIGHT: + switch (menu_option) { + case 0: + var_gain += 0.1; + if (var_gain > 3) + var_gain = 0.1; + break; + case 1: + var_speed++; + if (var_speed > 10) + var_speed = 1; + break; + } + break; + case LEFT: + switch (menu_option) { + case 0: + var_gain -= 0.1; + if (var_gain < 0.1) + var_gain = 3; + break; + case 1: + var_speed--; + if (var_speed < 1) + var_speed = 10; + break; + } + break; + } + if (menu_option < 0) { /// if the last (down) option is set for selection and user presses joystick down, selector moves at the top + menu_option = 2; + } + if (menu_option > 2) { /// if the first (up) option is set for selection and user presses joystick up, selector moves at the bottom + menu_option = 0; + } + + if (menu_option == 0) { ///selection in menu depends on the value of int option + lcd.drawCircle(75,10,3,1); + } else if (menu_option == 1) { + lcd.drawCircle(75,27,3,1); + } else if (menu_option == 2) { + lcd.drawCircle(55,43,3,1); + if (g_joyButton_flag == 1) { + g_joyButton_flag = 0; + confirmationJoyButton(); } + } + g_joyButton_flag = 0; +} + +void confirmationJoyButton () { + + bool confirm = 1; + while (confirm) { + lcd.clear(); + lcd.printString("Continue ?",0,0); + lcd.printString("Yes",0,2); + lcd.printString("No",0,3); + updateJoystick(); + switch (joystick.direction) { ///check the direction of joystick + case UP: + save_option--; + break; + case DOWN: + save_option++; + break; + } + + if (save_option < 0) { /// if the last (down) option is set for selection and user presses joystick down, selector moves at the top + save_option = 1; + } + if (save_option > 1) { /// if the first (up) option is set for selection and user presses joystick up, selector moves at the bottom + save_option = 0; + } + + if (save_option == 0) { + lcd.drawCircle(30,19,3,1); + } + else if (save_option == 1) { + lcd.drawCircle(30,28,3,1); + } + if (g_swButton_flag == 1) { + g_swButton_flag = 0; + if (save_option == 0) { + confirm = 0; // exit continue + exit_menu = 1; // exit menu + } + else if (save_option == 1) { + confirm = 0; // just exit continue + } + } + lcd.refresh(); sleep(); } } @@ -209,6 +360,15 @@ void timeout_isr() {} +void joyButton_isr() { + g_joyButton_flag = 1; +} + + +void swButton_isr() { + g_swButton_flag = 1; +} + void calibrateJoystick() { // must not move during calibration @@ -222,7 +382,7 @@ joystick.x = xPot - joystick.x0; joystick.y = yPot - joystick.y0; // read button state - joystick.button = joy_button; + joystick.button = joyButton; // calculate direction depending on x,y values // tolerance allows a little lee-way in case joystick not exactly in the stated direction @@ -233,10 +393,11 @@ } 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; + joystick.direction = LEFT; } 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 = RIGHT; + }/* + 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; @@ -244,5 +405,6 @@ joystick.direction = DOWN_LEFT; } else if (joystick.y < DIRECTION_TOLERANCE && joystick.x > DIRECTION_TOLERANCE) { joystick.direction = DOWN_RIGHT; - }*/ + } + */ }