![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Function Generator and Oscilloscope
Dependencies: C12832_lcd DebounceIn mbed
main.cpp@0:dfc39b05ea05, 2014-05-22 (annotated)
- Committer:
- trichards1138
- Date:
- Thu May 22 18:43:41 2014 +0000
- Revision:
- 0:dfc39b05ea05
- Child:
- 1:e31325194990
First commit
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:dfc39b05ea05 | 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 | bool en_saw=false, en_dc=false, en_square=false, en_sine=false, en_display=false; |
trichards1138 | 0:dfc39b05ea05 | 29 | |
trichards1138 | 0:dfc39b05ea05 | 30 | // SineWave thread - is initiated when the user selects sinewave output |
trichards1138 | 0:dfc39b05ea05 | 31 | // print a sin function in a small window |
trichards1138 | 0:dfc39b05ea05 | 32 | // the value of pot 1 changes the period of the sine wave |
trichards1138 | 0:dfc39b05ea05 | 33 | // The value of pot 2 changes the height of the sine wave |
trichards1138 | 0:dfc39b05ea05 | 34 | void sineWave(const void *arg) |
trichards1138 | 0:dfc39b05ea05 | 35 | { |
trichards1138 | 0:dfc39b05ea05 | 36 | double i, vert, horiz, outval; |
trichards1138 | 0:dfc39b05ea05 | 37 | while(true) { // thread loop |
trichards1138 | 0:dfc39b05ea05 | 38 | while( en_sine ) { |
trichards1138 | 0:dfc39b05ea05 | 39 | horiz = pot1.read(); // get value of pot 1 |
trichards1138 | 0:dfc39b05ea05 | 40 | vert = pot2.read(); // scale the height of wave |
trichards1138 | 0:dfc39b05ea05 | 41 | // pc.printf("horiz=%f, vert=%f\r\n", horiz, vert); |
trichards1138 | 0:dfc39b05ea05 | 42 | for (i=0; i<2; i=i+0.05) { |
trichards1138 | 0:dfc39b05ea05 | 43 | outval = 0.5 + 0.5*vert*sin(i*3.14159); |
trichards1138 | 0:dfc39b05ea05 | 44 | Aout.write(outval); // Compute the sine value, + half the range |
trichards1138 | 0:dfc39b05ea05 | 45 | //pc.printf("Output is: %f\r\n", outval); |
trichards1138 | 0:dfc39b05ea05 | 46 | Thread::wait(0.00005); // Controls the sine wave period |
trichards1138 | 0:dfc39b05ea05 | 47 | } |
trichards1138 | 0:dfc39b05ea05 | 48 | } |
trichards1138 | 0:dfc39b05ea05 | 49 | Thread::wait(200); |
trichards1138 | 0:dfc39b05ea05 | 50 | } |
trichards1138 | 0:dfc39b05ea05 | 51 | } |
trichards1138 | 0:dfc39b05ea05 | 52 | |
trichards1138 | 0:dfc39b05ea05 | 53 | // Thread squareWave - called if user selects squarewave |
trichards1138 | 0:dfc39b05ea05 | 54 | // pot1 controls the width of pulse |
trichards1138 | 0:dfc39b05ea05 | 55 | // pot2 controls the height of pulse |
trichards1138 | 0:dfc39b05ea05 | 56 | void squareWave(const void *arg) |
trichards1138 | 0:dfc39b05ea05 | 57 | { |
trichards1138 | 0:dfc39b05ea05 | 58 | unsigned int i; |
trichards1138 | 0:dfc39b05ea05 | 59 | float width, height, w; |
trichards1138 | 0:dfc39b05ea05 | 60 | while(true) { // thread loop |
trichards1138 | 0:dfc39b05ea05 | 61 | while( en_square ) { |
trichards1138 | 0:dfc39b05ea05 | 62 | width = pot1.read_u16(); // get value of |
trichards1138 | 0:dfc39b05ea05 | 63 | w = width*2; |
trichards1138 | 0:dfc39b05ea05 | 64 | height = pot2.read(); // scale the height of wave |
trichards1138 | 0:dfc39b05ea05 | 65 | for (i=0; i<w; i++) { |
trichards1138 | 0:dfc39b05ea05 | 66 | if( i >= width ) |
trichards1138 | 0:dfc39b05ea05 | 67 | Aout.write(0); |
trichards1138 | 0:dfc39b05ea05 | 68 | else |
trichards1138 | 0:dfc39b05ea05 | 69 | Aout.write(height); |
trichards1138 | 0:dfc39b05ea05 | 70 | } |
trichards1138 | 0:dfc39b05ea05 | 71 | Thread::wait(5); |
trichards1138 | 0:dfc39b05ea05 | 72 | } |
trichards1138 | 0:dfc39b05ea05 | 73 | Thread::wait(200); |
trichards1138 | 0:dfc39b05ea05 | 74 | } |
trichards1138 | 0:dfc39b05ea05 | 75 | } |
trichards1138 | 0:dfc39b05ea05 | 76 | |
trichards1138 | 0:dfc39b05ea05 | 77 | // Thread flatdc - called if user selects dc sig |
trichards1138 | 0:dfc39b05ea05 | 78 | // pot2 controls the height of dc signal |
trichards1138 | 0:dfc39b05ea05 | 79 | void flatdc(const void *arg) |
trichards1138 | 0:dfc39b05ea05 | 80 | { |
trichards1138 | 0:dfc39b05ea05 | 81 | while(true) { // thread loop |
trichards1138 | 0:dfc39b05ea05 | 82 | while( en_dc ) { |
trichards1138 | 0:dfc39b05ea05 | 83 | Aout.write(pot2.read()); // scale the height of wave |
trichards1138 | 0:dfc39b05ea05 | 84 | Thread::wait(10); |
trichards1138 | 0:dfc39b05ea05 | 85 | } |
trichards1138 | 0:dfc39b05ea05 | 86 | Thread::wait(200); |
trichards1138 | 0:dfc39b05ea05 | 87 | } |
trichards1138 | 0:dfc39b05ea05 | 88 | } |
trichards1138 | 0:dfc39b05ea05 | 89 | |
trichards1138 | 0:dfc39b05ea05 | 90 | void displayWave(const void *arg) |
trichards1138 | 0:dfc39b05ea05 | 91 | { |
trichards1138 | 0:dfc39b05ea05 | 92 | unsigned int i; |
trichards1138 | 0:dfc39b05ea05 | 93 | unsigned int h = LCD.height(), w = LCD.width(), hhh; |
trichards1138 | 0:dfc39b05ea05 | 94 | unsigned int time_base = 0.5; |
trichards1138 | 0:dfc39b05ea05 | 95 | float trigger, curstate; |
trichards1138 | 0:dfc39b05ea05 | 96 | while(true) { // thread loop |
trichards1138 | 0:dfc39b05ea05 | 97 | while( en_display ) { |
trichards1138 | 0:dfc39b05ea05 | 98 | for (i=0; i<w; i++) { |
trichards1138 | 0:dfc39b05ea05 | 99 | hhh = (int)((h-(h*Ain.read()))+4); |
trichards1138 | 0:dfc39b05ea05 | 100 | LCD.pixel(i, hhh ,1); // print pixel |
trichards1138 | 0:dfc39b05ea05 | 101 | Thread::wait(time_base); |
trichards1138 | 0:dfc39b05ea05 | 102 | } |
trichards1138 | 0:dfc39b05ea05 | 103 | LCD.copy_to_lcd(); // LCD.pixel does not update the lcd |
trichards1138 | 0:dfc39b05ea05 | 104 | Thread::wait(20); |
trichards1138 | 0:dfc39b05ea05 | 105 | LCD.cls(); |
trichards1138 | 0:dfc39b05ea05 | 106 | LCD.rect(0,0,w,h,1); |
trichards1138 | 0:dfc39b05ea05 | 107 | trigger = pot1.read(); |
trichards1138 | 0:dfc39b05ea05 | 108 | while( Ain.read() < trigger ) Thread::wait(0.5); |
trichards1138 | 0:dfc39b05ea05 | 109 | if( false ) { //en_square ) { |
trichards1138 | 0:dfc39b05ea05 | 110 | while( Ain.read() > 0.2 ) Thread::wait(0.5); |
trichards1138 | 0:dfc39b05ea05 | 111 | while( Ain.read() < trigger ) Thread::wait(0.5); |
trichards1138 | 0:dfc39b05ea05 | 112 | } |
trichards1138 | 0:dfc39b05ea05 | 113 | } |
trichards1138 | 0:dfc39b05ea05 | 114 | LCD.cls(); |
trichards1138 | 0:dfc39b05ea05 | 115 | } |
trichards1138 | 0:dfc39b05ea05 | 116 | } |
trichards1138 | 0:dfc39b05ea05 | 117 | |
trichards1138 | 0:dfc39b05ea05 | 118 | |
trichards1138 | 0:dfc39b05ea05 | 119 | int main() |
trichards1138 | 0:dfc39b05ea05 | 120 | { |
trichards1138 | 0:dfc39b05ea05 | 121 | bool do_sinewave, do_squarewave, do_sawtooth, do_dc; |
trichards1138 | 0:dfc39b05ea05 | 122 | pc.baud(19200); |
trichards1138 | 0:dfc39b05ea05 | 123 | //testout.period_ms(6); |
trichards1138 | 0:dfc39b05ea05 | 124 | //testout.write(0.5); |
trichards1138 | 0:dfc39b05ea05 | 125 | testout.period_ms(127); |
trichards1138 | 0:dfc39b05ea05 | 126 | testout.pulsewidth_ms(127*pot2.read()); |
trichards1138 | 0:dfc39b05ea05 | 127 | en_display = true; |
trichards1138 | 0:dfc39b05ea05 | 128 | Thread display(displayWave); |
trichards1138 | 0:dfc39b05ea05 | 129 | // Thread square(squareWave); |
trichards1138 | 0:dfc39b05ea05 | 130 | //Thread saw(sawtooth); |
trichards1138 | 0:dfc39b05ea05 | 131 | // Thread dc(flatdc); |
trichards1138 | 0:dfc39b05ea05 | 132 | // Thread sine(sineWave); |
trichards1138 | 0:dfc39b05ea05 | 133 | while(1) { |
trichards1138 | 0:dfc39b05ea05 | 134 | //tled = testout; |
trichards1138 | 0:dfc39b05ea05 | 135 | testout.pulsewidth_ms(127*pot2.read()); |
trichards1138 | 0:dfc39b05ea05 | 136 | Thread::wait(500); |
trichards1138 | 0:dfc39b05ea05 | 137 | } |
trichards1138 | 0:dfc39b05ea05 | 138 | while(1) { |
trichards1138 | 0:dfc39b05ea05 | 139 | while( !OnOff ) { |
trichards1138 | 0:dfc39b05ea05 | 140 | //pc.printf("havent selected yet\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 141 | if( up ) { |
trichards1138 | 0:dfc39b05ea05 | 142 | do_sinewave = false; |
trichards1138 | 0:dfc39b05ea05 | 143 | do_squarewave = false; |
trichards1138 | 0:dfc39b05ea05 | 144 | do_sawtooth = true; |
trichards1138 | 0:dfc39b05ea05 | 145 | do_dc = false; |
trichards1138 | 0:dfc39b05ea05 | 146 | } |
trichards1138 | 0:dfc39b05ea05 | 147 | if( down ) { |
trichards1138 | 0:dfc39b05ea05 | 148 | do_sinewave = false; |
trichards1138 | 0:dfc39b05ea05 | 149 | do_squarewave = false; |
trichards1138 | 0:dfc39b05ea05 | 150 | do_sawtooth = false; |
trichards1138 | 0:dfc39b05ea05 | 151 | do_dc = true; |
trichards1138 | 0:dfc39b05ea05 | 152 | } |
trichards1138 | 0:dfc39b05ea05 | 153 | if( left ) { |
trichards1138 | 0:dfc39b05ea05 | 154 | do_sinewave = true; |
trichards1138 | 0:dfc39b05ea05 | 155 | do_squarewave = false; |
trichards1138 | 0:dfc39b05ea05 | 156 | do_sawtooth = false; |
trichards1138 | 0:dfc39b05ea05 | 157 | do_dc = false; |
trichards1138 | 0:dfc39b05ea05 | 158 | } |
trichards1138 | 0:dfc39b05ea05 | 159 | if( right ) { |
trichards1138 | 0:dfc39b05ea05 | 160 | do_sinewave = false; |
trichards1138 | 0:dfc39b05ea05 | 161 | do_squarewave = true; |
trichards1138 | 0:dfc39b05ea05 | 162 | do_sawtooth = false; |
trichards1138 | 0:dfc39b05ea05 | 163 | do_dc = false; |
trichards1138 | 0:dfc39b05ea05 | 164 | } |
trichards1138 | 0:dfc39b05ea05 | 165 | Thread::wait(20); |
trichards1138 | 0:dfc39b05ea05 | 166 | } |
trichards1138 | 0:dfc39b05ea05 | 167 | if( do_squarewave ) { |
trichards1138 | 0:dfc39b05ea05 | 168 | pc.printf("I selected square\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 169 | en_square = true; |
trichards1138 | 0:dfc39b05ea05 | 170 | } |
trichards1138 | 0:dfc39b05ea05 | 171 | else if( do_sawtooth ) { |
trichards1138 | 0:dfc39b05ea05 | 172 | pc.printf("I selected saw\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 173 | en_saw = true; |
trichards1138 | 0:dfc39b05ea05 | 174 | } |
trichards1138 | 0:dfc39b05ea05 | 175 | else if( do_dc ) { |
trichards1138 | 0:dfc39b05ea05 | 176 | pc.printf("I selected DC\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 177 | en_dc = true; |
trichards1138 | 0:dfc39b05ea05 | 178 | } |
trichards1138 | 0:dfc39b05ea05 | 179 | else { |
trichards1138 | 0:dfc39b05ea05 | 180 | pc.printf("I selected sine\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 181 | en_sine = true; |
trichards1138 | 0:dfc39b05ea05 | 182 | } |
trichards1138 | 0:dfc39b05ea05 | 183 | en_display = true; |
trichards1138 | 0:dfc39b05ea05 | 184 | while( OnOff ) { |
trichards1138 | 0:dfc39b05ea05 | 185 | // pc.printf("In first\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 186 | Thread::wait(200); |
trichards1138 | 0:dfc39b05ea05 | 187 | } |
trichards1138 | 0:dfc39b05ea05 | 188 | while( !OnOff ) { |
trichards1138 | 0:dfc39b05ea05 | 189 | // pc.printf("In Second\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 190 | Thread::wait(200); |
trichards1138 | 0:dfc39b05ea05 | 191 | } |
trichards1138 | 0:dfc39b05ea05 | 192 | while( OnOff ) { |
trichards1138 | 0:dfc39b05ea05 | 193 | // pc.printf("In Third\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 194 | Thread::wait(100); |
trichards1138 | 0:dfc39b05ea05 | 195 | } |
trichards1138 | 0:dfc39b05ea05 | 196 | if( do_squarewave ) { |
trichards1138 | 0:dfc39b05ea05 | 197 | pc.printf("I terminated square\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 198 | en_square = false; |
trichards1138 | 0:dfc39b05ea05 | 199 | } |
trichards1138 | 0:dfc39b05ea05 | 200 | else if( do_sawtooth ) { |
trichards1138 | 0:dfc39b05ea05 | 201 | pc.printf("I terminated saw\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 202 | en_saw = false; |
trichards1138 | 0:dfc39b05ea05 | 203 | } |
trichards1138 | 0:dfc39b05ea05 | 204 | else if( do_dc ) { |
trichards1138 | 0:dfc39b05ea05 | 205 | pc.printf("I terminated DC\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 206 | en_dc = false; |
trichards1138 | 0:dfc39b05ea05 | 207 | } |
trichards1138 | 0:dfc39b05ea05 | 208 | else { |
trichards1138 | 0:dfc39b05ea05 | 209 | pc.printf("I terminated sine\r\n"); |
trichards1138 | 0:dfc39b05ea05 | 210 | en_sine = false; |
trichards1138 | 0:dfc39b05ea05 | 211 | } |
trichards1138 | 0:dfc39b05ea05 | 212 | en_display=false; |
trichards1138 | 0:dfc39b05ea05 | 213 | LCD.cls(); |
trichards1138 | 0:dfc39b05ea05 | 214 | } |
trichards1138 | 0:dfc39b05ea05 | 215 | } |
trichards1138 | 0:dfc39b05ea05 | 216 |