Function Generator and Oscilloscope

Dependencies:   C12832_lcd DebounceIn mbed

Committer:
trichards1138
Date:
Sat May 24 22:45:38 2014 +0000
Revision:
2:218abf68fe93
Parent:
1:e31325194990
Child:
3:920cc6573be0
second;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
trichards1138 0:dfc39b05ea05 1 #include "mbed.h"
trichards1138 0:dfc39b05ea05 2 #include "C12832_lcd.h"
trichards1138 0:dfc39b05ea05 3 #include "DebounceIn.h"
trichards1138 2:218abf68fe93 4 //#include "rtos.h"
trichards1138 0:dfc39b05ea05 5
trichards1138 0:dfc39b05ea05 6 Serial pc(USBTX, USBRX);
trichards1138 0:dfc39b05ea05 7 C12832_LCD LCD;
trichards1138 0:dfc39b05ea05 8 AnalogIn Ain(p17);
trichards1138 0:dfc39b05ea05 9 AnalogOut Aout(p18);
trichards1138 0:dfc39b05ea05 10 AnalogIn pot1(p19);
trichards1138 0:dfc39b05ea05 11 AnalogIn pot2(p20);
trichards1138 0:dfc39b05ea05 12 DigitalIn up(p15);
trichards1138 0:dfc39b05ea05 13 DigitalIn down(p12);
trichards1138 0:dfc39b05ea05 14 DigitalIn left(p13);
trichards1138 0:dfc39b05ea05 15 DigitalIn right(p16);
trichards1138 0:dfc39b05ea05 16 BusIn joy(p15,p12,p13,p16);
trichards1138 0:dfc39b05ea05 17 DigitalIn OnOff(p14);
trichards1138 0:dfc39b05ea05 18 PwmOut testout(p21);
trichards1138 0:dfc39b05ea05 19 PwmOut tled(LED1);
trichards1138 0:dfc39b05ea05 20 /*
trichards1138 0:dfc39b05ea05 21 DebounceIn OnOff(p14);
trichards1138 0:dfc39b05ea05 22 DebounceIn up(p15);
trichards1138 0:dfc39b05ea05 23 DebounceIn down(p12);
trichards1138 0:dfc39b05ea05 24 DebounceIn left(p13);
trichards1138 0:dfc39b05ea05 25 DebounceIn right(p16);
trichards1138 0:dfc39b05ea05 26 */
trichards1138 0:dfc39b05ea05 27
trichards1138 0:dfc39b05ea05 28
trichards1138 0:dfc39b05ea05 29 // SineWave thread - is initiated when the user selects sinewave output
trichards1138 0:dfc39b05ea05 30 // print a sin function in a small window
trichards1138 0:dfc39b05ea05 31 // the value of pot 1 changes the period of the sine wave
trichards1138 0:dfc39b05ea05 32 // The value of pot 2 changes the height of the sine wave
trichards1138 1:e31325194990 33 void sineWave(void)
trichards1138 0:dfc39b05ea05 34 {
trichards1138 0:dfc39b05ea05 35 double i, vert, horiz, outval;
trichards1138 1:e31325194990 36 while(!OnOff) { // thread loop
trichards1138 1:e31325194990 37 horiz = pot1.read(); // get value of pot 1
trichards1138 1:e31325194990 38 vert = pot2.read(); // scale the height of wave
trichards1138 1:e31325194990 39 for (i=0; i<2; i=i+0.05) {
trichards1138 1:e31325194990 40 outval = 0.5 + 0.5*vert*sin(i*3.14159);
trichards1138 1:e31325194990 41 Aout.write(outval); // Compute the sine value, + half the range
trichards1138 1:e31325194990 42 wait_ms(0.0002*horiz); // Controls the sine wave period
trichards1138 1:e31325194990 43 }
trichards1138 1:e31325194990 44 }
trichards1138 0:dfc39b05ea05 45 }
trichards1138 0:dfc39b05ea05 46
trichards1138 0:dfc39b05ea05 47 // Thread squareWave - called if user selects squarewave
trichards1138 0:dfc39b05ea05 48 // pot1 controls the width of pulse
trichards1138 0:dfc39b05ea05 49 // pot2 controls the height of pulse
trichards1138 1:e31325194990 50 void squareWave(void)
trichards1138 0:dfc39b05ea05 51 {
trichards1138 1:e31325194990 52 float height=0;
trichards1138 1:e31325194990 53 unsigned int width=0, cnt=0;
trichards1138 1:e31325194990 54 while(!OnOff) { // thread loop
trichards1138 1:e31325194990 55 if( cnt == 1000 ) {
trichards1138 1:e31325194990 56 width = pot1.read_u16(); // get value of
trichards1138 1:e31325194990 57 height = pot2.read(); // scale the height of wave
trichards1138 1:e31325194990 58 cnt = 0;
trichards1138 1:e31325194990 59 }
trichards1138 1:e31325194990 60 Aout.write(height);
trichards1138 1:e31325194990 61 wait_ms(0.1*width);
trichards1138 1:e31325194990 62 Aout.write(0);
trichards1138 1:e31325194990 63 wait_ms(0.1*width);
trichards1138 1:e31325194990 64 cnt++;
trichards1138 0:dfc39b05ea05 65 }
trichards1138 0:dfc39b05ea05 66 }
trichards1138 0:dfc39b05ea05 67
trichards1138 0:dfc39b05ea05 68 // Thread flatdc - called if user selects dc sig
trichards1138 0:dfc39b05ea05 69 // pot2 controls the height of dc signal
trichards1138 1:e31325194990 70 void flatdc(void)
trichards1138 0:dfc39b05ea05 71 {
trichards1138 1:e31325194990 72 while(!OnOff) { // thread loop
trichards1138 1:e31325194990 73 Aout.write(pot2.read()); // scale the height of wave
trichards1138 1:e31325194990 74 wait_ms(10);
trichards1138 0:dfc39b05ea05 75 }
trichards1138 0:dfc39b05ea05 76 }
trichards1138 1:e31325194990 77
trichards1138 1:e31325194990 78 void sawTooth(void)
trichards1138 0:dfc39b05ea05 79 {
trichards1138 0:dfc39b05ea05 80 }
trichards1138 0:dfc39b05ea05 81
trichards1138 0:dfc39b05ea05 82 int main()
trichards1138 0:dfc39b05ea05 83 {
trichards1138 1:e31325194990 84 bool do_sine=false, do_saw=false;
trichards1138 1:e31325194990 85 bool do_dc=false, do_square=false;
trichards1138 0:dfc39b05ea05 86 pc.baud(19200);
trichards1138 0:dfc39b05ea05 87 while(1) {
trichards1138 1:e31325194990 88 if( up ) {
trichards1138 1:e31325194990 89 do_saw=true;
trichards1138 1:e31325194990 90 do_sine=false;
trichards1138 1:e31325194990 91 do_dc=false;
trichards1138 1:e31325194990 92 do_square=false;
trichards1138 1:e31325194990 93 }
trichards1138 1:e31325194990 94 if( down ) {
trichards1138 1:e31325194990 95 do_saw=false;
trichards1138 1:e31325194990 96 do_sine=false;
trichards1138 1:e31325194990 97 do_dc=true;
trichards1138 1:e31325194990 98 do_square=false;
trichards1138 1:e31325194990 99 }
trichards1138 1:e31325194990 100 if( right ) {
trichards1138 1:e31325194990 101 do_saw=false;
trichards1138 1:e31325194990 102 do_sine=false;
trichards1138 1:e31325194990 103 do_dc=false;
trichards1138 1:e31325194990 104 do_square=true;
trichards1138 1:e31325194990 105 }
trichards1138 1:e31325194990 106 if( left ) {
trichards1138 1:e31325194990 107 do_saw=false;
trichards1138 1:e31325194990 108 do_sine=true;
trichards1138 1:e31325194990 109 do_dc=false;
trichards1138 1:e31325194990 110 do_square=false;
trichards1138 1:e31325194990 111 }
trichards1138 1:e31325194990 112
trichards1138 1:e31325194990 113 if( OnOff ) {
trichards1138 1:e31325194990 114 if( do_saw )
trichards1138 1:e31325194990 115 sawTooth();
trichards1138 1:e31325194990 116 else if( do_dc )
trichards1138 1:e31325194990 117 flatdc();
trichards1138 1:e31325194990 118 else if( do_square )
trichards1138 1:e31325194990 119 squareWave();
trichards1138 1:e31325194990 120 else if( do_sine )
trichards1138 1:e31325194990 121 sineWave();
trichards1138 1:e31325194990 122 else
trichards1138 1:e31325194990 123 sineWave();
trichards1138 1:e31325194990 124 }
trichards1138 1:e31325194990 125 }
trichards1138 0:dfc39b05ea05 126 }
trichards1138 0:dfc39b05ea05 127