Function Generator and Oscilloscope
Dependencies: C12832_lcd DebounceIn mbed
Diff: main.cpp
- Revision:
- 1:e31325194990
- Parent:
- 0:dfc39b05ea05
- Child:
- 2:218abf68fe93
diff -r dfc39b05ea05 -r e31325194990 main.cpp --- a/main.cpp Thu May 22 18:43:41 2014 +0000 +++ b/main.cpp Sat May 24 04:52:20 2014 +0000 @@ -25,192 +25,103 @@ DebounceIn right(p16); */ -bool en_saw=false, en_dc=false, en_square=false, en_sine=false, en_display=false; // SineWave thread - is initiated when the user selects sinewave output // print a sin function in a small window // the value of pot 1 changes the period of the sine wave // The value of pot 2 changes the height of the sine wave -void sineWave(const void *arg) +void sineWave(void) { double i, vert, horiz, outval; - while(true) { // thread loop - while( en_sine ) { - horiz = pot1.read(); // get value of pot 1 - vert = pot2.read(); // scale the height of wave - // pc.printf("horiz=%f, vert=%f\r\n", horiz, vert); - for (i=0; i<2; i=i+0.05) { - outval = 0.5 + 0.5*vert*sin(i*3.14159); - Aout.write(outval); // Compute the sine value, + half the range - //pc.printf("Output is: %f\r\n", outval); - Thread::wait(0.00005); // Controls the sine wave period - } - } - Thread::wait(200); - } + while(!OnOff) { // thread loop + horiz = pot1.read(); // get value of pot 1 + vert = pot2.read(); // scale the height of wave + for (i=0; i<2; i=i+0.05) { + outval = 0.5 + 0.5*vert*sin(i*3.14159); + Aout.write(outval); // Compute the sine value, + half the range + wait_ms(0.0002*horiz); // Controls the sine wave period + } + } } // Thread squareWave - called if user selects squarewave // pot1 controls the width of pulse // pot2 controls the height of pulse -void squareWave(const void *arg) +void squareWave(void) { - unsigned int i; - float width, height, w; - while(true) { // thread loop - while( en_square ) { - width = pot1.read_u16(); // get value of - w = width*2; - height = pot2.read(); // scale the height of wave - for (i=0; i<w; i++) { - if( i >= width ) - Aout.write(0); - else - Aout.write(height); - } - Thread::wait(5); - } - Thread::wait(200); + float height=0; + unsigned int width=0, cnt=0; + while(!OnOff) { // thread loop + if( cnt == 1000 ) { + width = pot1.read_u16(); // get value of + height = pot2.read(); // scale the height of wave + cnt = 0; + } + Aout.write(height); + wait_ms(0.1*width); + Aout.write(0); + wait_ms(0.1*width); + cnt++; } } // Thread flatdc - called if user selects dc sig // pot2 controls the height of dc signal -void flatdc(const void *arg) +void flatdc(void) { - while(true) { // thread loop - while( en_dc ) { - Aout.write(pot2.read()); // scale the height of wave - Thread::wait(10); - } - Thread::wait(200); + while(!OnOff) { // thread loop + Aout.write(pot2.read()); // scale the height of wave + wait_ms(10); } } - -void displayWave(const void *arg) + +void sawTooth(void) { - unsigned int i; - unsigned int h = LCD.height(), w = LCD.width(), hhh; - unsigned int time_base = 0.5; - float trigger, curstate; - while(true) { // thread loop - while( en_display ) { - for (i=0; i<w; i++) { - hhh = (int)((h-(h*Ain.read()))+4); - LCD.pixel(i, hhh ,1); // print pixel - Thread::wait(time_base); - } - LCD.copy_to_lcd(); // LCD.pixel does not update the lcd - Thread::wait(20); - LCD.cls(); - LCD.rect(0,0,w,h,1); - trigger = pot1.read(); - while( Ain.read() < trigger ) Thread::wait(0.5); - if( false ) { //en_square ) { - while( Ain.read() > 0.2 ) Thread::wait(0.5); - while( Ain.read() < trigger ) Thread::wait(0.5); - } - } - LCD.cls(); - } } - int main() { - bool do_sinewave, do_squarewave, do_sawtooth, do_dc; + bool do_sine=false, do_saw=false; + bool do_dc=false, do_square=false; pc.baud(19200); - //testout.period_ms(6); - //testout.write(0.5); - testout.period_ms(127); - testout.pulsewidth_ms(127*pot2.read()); - en_display = true; - Thread display(displayWave); - // Thread square(squareWave); - //Thread saw(sawtooth); - // Thread dc(flatdc); - // Thread sine(sineWave); - while(1) { - //tled = testout; - testout.pulsewidth_ms(127*pot2.read()); - Thread::wait(500); - } while(1) { - while( !OnOff ) { - //pc.printf("havent selected yet\r\n"); - if( up ) { - do_sinewave = false; - do_squarewave = false; - do_sawtooth = true; - do_dc = false; - } - if( down ) { - do_sinewave = false; - do_squarewave = false; - do_sawtooth = false; - do_dc = true; - } - if( left ) { - do_sinewave = true; - do_squarewave = false; - do_sawtooth = false; - do_dc = false; - } - if( right ) { - do_sinewave = false; - do_squarewave = true; - do_sawtooth = false; - do_dc = false; - } - Thread::wait(20); - } - if( do_squarewave ) { - pc.printf("I selected square\r\n"); - en_square = true; - } - else if( do_sawtooth ) { - pc.printf("I selected saw\r\n"); - en_saw = true; - } - else if( do_dc ) { - pc.printf("I selected DC\r\n"); - en_dc = true; - } - else { - pc.printf("I selected sine\r\n"); - en_sine = true; - } - en_display = true; - while( OnOff ) { - // pc.printf("In first\r\n"); - Thread::wait(200); - } - while( !OnOff ) { - // pc.printf("In Second\r\n"); - Thread::wait(200); - } - while( OnOff ) { - // pc.printf("In Third\r\n"); - Thread::wait(100); - } - if( do_squarewave ) { - pc.printf("I terminated square\r\n"); - en_square = false; - } - else if( do_sawtooth ) { - pc.printf("I terminated saw\r\n"); - en_saw = false; - } - else if( do_dc ) { - pc.printf("I terminated DC\r\n"); - en_dc = false; - } - else { - pc.printf("I terminated sine\r\n"); - en_sine = false; - } - en_display=false; - LCD.cls(); - } + if( up ) { + do_saw=true; + do_sine=false; + do_dc=false; + do_square=false; + } + if( down ) { + do_saw=false; + do_sine=false; + do_dc=true; + do_square=false; + } + if( right ) { + do_saw=false; + do_sine=false; + do_dc=false; + do_square=true; + } + if( left ) { + do_saw=false; + do_sine=true; + do_dc=false; + do_square=false; + } + + if( OnOff ) { + if( do_saw ) + sawTooth(); + else if( do_dc ) + flatdc(); + else if( do_square ) + squareWave(); + else if( do_sine ) + sineWave(); + else + sineWave(); + } + } }