Matt Shilling / Mbed 2 deprecated Waveform_Generator

Dependencies:   SLCD TSI mbed

Committer:
MattShilling
Date:
Thu Sep 29 22:24:25 2016 +0000
Revision:
1:36dabc12aa01
Parent:
0:fb56047b523f
Child:
2:15b40c0f648c
revised head disc;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MattShilling 0:fb56047b523f 1 //***************************************************************
MattShilling 0:fb56047b523f 2 // Function Generator - main entry and sole source file for an
MattShilling 0:fb56047b523f 3 // MBED based function generator. The program will output
MattShilling 1:36dabc12aa01 4 // waveforms sinewave, squarewave, sawtooth.
MattShilling 0:fb56047b523f 5 //
MattShilling 1:36dabc12aa01 6 // Author: Matt Shilling & Terry Richards
MattShilling 1:36dabc12aa01 7 // Date: 9/29/2016
MattShilling 0:fb56047b523f 8 //
MattShilling 0:fb56047b523f 9 #include "mbed.h"
MattShilling 0:fb56047b523f 10 #include "SLCD.h"
MattShilling 0:fb56047b523f 11 #include "TSISensor.h"
MattShilling 0:fb56047b523f 12
MattShilling 0:fb56047b523f 13 //Serial output for debug
MattShilling 0:fb56047b523f 14 //Serial pc(USBTX, USBRX);
MattShilling 0:fb56047b523f 15
MattShilling 0:fb56047b523f 16 SLCD lcd; //create lcd class
MattShilling 0:fb56047b523f 17 TSISensor tsi; // touch sensor
MattShilling 0:fb56047b523f 18 Serial pc(USBTX, USBRX); //init serial
MattShilling 0:fb56047b523f 19
MattShilling 0:fb56047b523f 20 AnalogOut Aout(PTE30); // waveform will be outputted through this pin
MattShilling 0:fb56047b523f 21 DigitalIn sw_right(SW1);
MattShilling 0:fb56047b523f 22 DigitalIn sw_left(SW3);
MattShilling 0:fb56047b523f 23
MattShilling 0:fb56047b523f 24
MattShilling 0:fb56047b523f 25 bool first = true;
MattShilling 0:fb56047b523f 26 bool just_pressed = false;
MattShilling 0:fb56047b523f 27 int select = 1;
MattShilling 0:fb56047b523f 28
MattShilling 0:fb56047b523f 29 // check_update - checks the buttons to see if the user wants
MattShilling 0:fb56047b523f 30 // to update either the vertical (in volts) or the horizontal
MattShilling 0:fb56047b523f 31 // (in uS) for the currently selected waveform. It also updates
MattShilling 0:fb56047b523f 32 // the LCD
MattShilling 0:fb56047b523f 33 // **** The joystick is used instead of the pots because *****
MattShilling 0:fb56047b523f 34 // **** I found that the pots were to unstable ***************
MattShilling 0:fb56047b523f 35 // Inputs - the vert and horiz from the calling waveform routine
MattShilling 0:fb56047b523f 36 // Outputs - the updated vert and horiz based on the button presses
MattShilling 0:fb56047b523f 37 //
MattShilling 0:fb56047b523f 38 #define SINE 1
MattShilling 0:fb56047b523f 39 #define SQUARE 2
MattShilling 0:fb56047b523f 40 #define SAW 3
MattShilling 0:fb56047b523f 41
MattShilling 0:fb56047b523f 42
MattShilling 0:fb56047b523f 43 void check_click_right()
MattShilling 0:fb56047b523f 44 {
MattShilling 0:fb56047b523f 45 if (sw_right == 0)
MattShilling 0:fb56047b523f 46 {
MattShilling 0:fb56047b523f 47 just_pressed = true;
MattShilling 0:fb56047b523f 48 }
MattShilling 0:fb56047b523f 49
MattShilling 0:fb56047b523f 50 if (sw_right == 1 && just_pressed == true)
MattShilling 0:fb56047b523f 51 {
MattShilling 0:fb56047b523f 52 select++;
MattShilling 0:fb56047b523f 53 if (select == 4) select = 1;
MattShilling 0:fb56047b523f 54 just_pressed = false;
MattShilling 0:fb56047b523f 55 pc.printf("selection %d \n", select);
MattShilling 0:fb56047b523f 56 }
MattShilling 0:fb56047b523f 57
MattShilling 0:fb56047b523f 58 }
MattShilling 0:fb56047b523f 59
MattShilling 0:fb56047b523f 60 void check_update( int type, //what type of waveform
MattShilling 0:fb56047b523f 61 double *vert, //vertical in volts
MattShilling 0:fb56047b523f 62 int *horiz, //horizontal in microseconds (uS)
MattShilling 0:fb56047b523f 63 float vertmax, //max vertical in volts
MattShilling 0:fb56047b523f 64 int horizmax, //max horizontal in us
MattShilling 0:fb56047b523f 65 bool disp_info ) //put initial vert and horiz
MattShilling 0:fb56047b523f 66 { //on LCD screen if set
MattShilling 0:fb56047b523f 67 if( type ) { //The header is static once the waveform is chosen
MattShilling 0:fb56047b523f 68 lcd.clear();
MattShilling 0:fb56047b523f 69 lcd.Home();
MattShilling 0:fb56047b523f 70 switch( type ) { //only done if type is set
MattShilling 0:fb56047b523f 71 case SINE:
MattShilling 0:fb56047b523f 72 lcd.printf("sin");
MattShilling 0:fb56047b523f 73 break;
MattShilling 0:fb56047b523f 74 case SQUARE:
MattShilling 0:fb56047b523f 75 lcd.printf("sqr");
MattShilling 0:fb56047b523f 76 break;
MattShilling 0:fb56047b523f 77 case SAW:
MattShilling 0:fb56047b523f 78 lcd.printf("saw");
MattShilling 0:fb56047b523f 79 }
MattShilling 0:fb56047b523f 80 }
MattShilling 0:fb56047b523f 81
MattShilling 0:fb56047b523f 82 if(tsi.readPercentage() != 0
MattShilling 0:fb56047b523f 83 && tsi.readPercentage() < (1.0 - 0.001)) {
MattShilling 0:fb56047b523f 84
MattShilling 0:fb56047b523f 85 if (sw_left == 1) {
MattShilling 0:fb56047b523f 86 *vert = tsi.readPercentage(); //update the vertical
MattShilling 0:fb56047b523f 87 pc.printf("Height Changed to: %f \n", *vert);
MattShilling 0:fb56047b523f 88 }
MattShilling 0:fb56047b523f 89
MattShilling 0:fb56047b523f 90 }
MattShilling 0:fb56047b523f 91
MattShilling 0:fb56047b523f 92
MattShilling 0:fb56047b523f 93 if(tsi.readPercentage() != 0
MattShilling 0:fb56047b523f 94 && sw_left == 0 ) { //updates the horizontal in us
MattShilling 0:fb56047b523f 95
MattShilling 0:fb56047b523f 96 *horiz = int(tsi.readPercentage() * 1000);
MattShilling 0:fb56047b523f 97 pc.printf("Period Changed to: %d \n", *horiz);
MattShilling 0:fb56047b523f 98 }
MattShilling 0:fb56047b523f 99
MattShilling 0:fb56047b523f 100 if(disp_info) { //only done if bool is true
MattShilling 0:fb56047b523f 101 //pc.printf("PW/Period: %d uS \n", *horiz);
MattShilling 0:fb56047b523f 102 //pc.printf("Height: %5.3f V \n\n", *vert);
MattShilling 0:fb56047b523f 103 }
MattShilling 0:fb56047b523f 104
MattShilling 0:fb56047b523f 105 check_click_right();
MattShilling 0:fb56047b523f 106
MattShilling 0:fb56047b523f 107 }
MattShilling 0:fb56047b523f 108
MattShilling 0:fb56047b523f 109 // SineWave - is initiated when the user selects sinewave output
MattShilling 0:fb56047b523f 110 // Sends a sine wave to the Aout pin.
MattShilling 0:fb56047b523f 111 // The Touch Slider controls amplitude
MattShilling 0:fb56047b523f 112 // The Touch Slider + left button controls the period
MattShilling 0:fb56047b523f 113 void sineWave()
MattShilling 0:fb56047b523f 114 {
MattShilling 0:fb56047b523f 115 int horiz=1000;
MattShilling 0:fb56047b523f 116 double vert=1.0, outval, i;
MattShilling 0:fb56047b523f 117 check_update( SINE, &vert, &horiz, 1.0, 10000, true );
MattShilling 0:fb56047b523f 118 while(select == SINE) { // thread loop
MattShilling 0:fb56047b523f 119 check_update( 0, &vert, &horiz, 1.0, 10000, false );
MattShilling 0:fb56047b523f 120 for (i=0; i<2; i=i+0.05) {
MattShilling 0:fb56047b523f 121 outval = 0.5 + 0.5*vert*sin(i*3.14159);
MattShilling 0:fb56047b523f 122 Aout.write(outval); // Compute the sine value, + half the range
MattShilling 0:fb56047b523f 123 wait_us(horiz); // Controls the sine wave period
MattShilling 0:fb56047b523f 124 }
MattShilling 0:fb56047b523f 125 }
MattShilling 0:fb56047b523f 126
MattShilling 0:fb56047b523f 127 first = true;
MattShilling 0:fb56047b523f 128 }
MattShilling 0:fb56047b523f 129
MattShilling 0:fb56047b523f 130 // squareWave - called if user selects squarewave. Sends the
MattShilling 0:fb56047b523f 131 // square wave to the Aout pin.
MattShilling 0:fb56047b523f 132 // The Touch Slider controls amplitude
MattShilling 0:fb56047b523f 133 // The Touch Slider + left button controls the period
MattShilling 0:fb56047b523f 134 void squareWave()
MattShilling 0:fb56047b523f 135 {
MattShilling 0:fb56047b523f 136 static double height = 1.0;
MattShilling 0:fb56047b523f 137 static int width = 20;
MattShilling 0:fb56047b523f 138 check_update( SQUARE, &height, &width, 1.0, 10000, true );
MattShilling 0:fb56047b523f 139 while(select == SQUARE) { // thread loop
MattShilling 0:fb56047b523f 140 check_update( 0, &height, &width, 1.0, 100000, false );
MattShilling 0:fb56047b523f 141 Aout.write(height);
MattShilling 0:fb56047b523f 142 wait_us(width);
MattShilling 0:fb56047b523f 143 Aout.write(0);
MattShilling 0:fb56047b523f 144 wait_us(width);
MattShilling 0:fb56047b523f 145 }
MattShilling 0:fb56047b523f 146
MattShilling 0:fb56047b523f 147 first = true;
MattShilling 0:fb56047b523f 148 }
MattShilling 0:fb56047b523f 149
MattShilling 0:fb56047b523f 150 // SawTooth - called if the user selects sawTooth. Sends the
MattShilling 0:fb56047b523f 151 // saw tooth waveform out to the Aout pin.
MattShilling 0:fb56047b523f 152 // The up and down buttons of the joystick control the amplitude
MattShilling 0:fb56047b523f 153 // The right and left buttons control the period
MattShilling 0:fb56047b523f 154 void sawTooth()
MattShilling 0:fb56047b523f 155 {
MattShilling 0:fb56047b523f 156 static double height = 1.0, inc;
MattShilling 0:fb56047b523f 157 static int width = 6000, i;
MattShilling 0:fb56047b523f 158 inc = height/width;
MattShilling 0:fb56047b523f 159 check_update( SAW, &height, &width, 1.0, 10000, true );
MattShilling 0:fb56047b523f 160 while(select == SAW) { // thread loop
MattShilling 0:fb56047b523f 161 check_update( 0, &height, &width, 1.0, 100000, false );
MattShilling 0:fb56047b523f 162 inc = height/width;
MattShilling 0:fb56047b523f 163 for( i=0; i<width; i++) {
MattShilling 0:fb56047b523f 164 Aout.write(i*inc);
MattShilling 0:fb56047b523f 165 //wait_us(1);
MattShilling 0:fb56047b523f 166 }
MattShilling 0:fb56047b523f 167 }
MattShilling 0:fb56047b523f 168
MattShilling 0:fb56047b523f 169 first = true;
MattShilling 0:fb56047b523f 170 }
MattShilling 0:fb56047b523f 171 // Banner - Output a welcome and select screen for
MattShilling 0:fb56047b523f 172 // the user and give them instructions for use
MattShilling 0:fb56047b523f 173 void banner(){
MattShilling 0:fb56047b523f 174
MattShilling 0:fb56047b523f 175 pc.printf("WELCOME TO THE FRDM-KL46Z WAVEFORM GENERATOR \n");
MattShilling 0:fb56047b523f 176 pc.printf("Writen by Matt Shilling based on code from Terry Richards \n\n");
MattShilling 0:fb56047b523f 177 pc.printf("---------------------- INSTRUCTIONS ----------------------\n");
MattShilling 0:fb56047b523f 178 pc.printf("1.) Click right button to select waveform\n");
MattShilling 0:fb56047b523f 179 pc.printf("2.) Use touch slider to control wave amplitude\n");
MattShilling 0:fb56047b523f 180 pc.printf("3.) Use touch slider + left putton to control wave period\n");
MattShilling 0:fb56047b523f 181
MattShilling 0:fb56047b523f 182 }
MattShilling 0:fb56047b523f 183
MattShilling 0:fb56047b523f 184 // main - main entry point to program and where the
MattShilling 0:fb56047b523f 185 // user selects the desired waveform.
MattShilling 0:fb56047b523f 186 int main()
MattShilling 0:fb56047b523f 187 {
MattShilling 0:fb56047b523f 188 //booleans to select waveform
MattShilling 0:fb56047b523f 189 bool do_sine=false, do_saw=false;
MattShilling 0:fb56047b523f 190 bool do_square=false;
MattShilling 0:fb56047b523f 191 //pc.baud(19200); //debug
MattShilling 0:fb56047b523f 192 banner(); //print instructions on serial
MattShilling 0:fb56047b523f 193 while(1) {
MattShilling 0:fb56047b523f 194 if( select == SAW ) { //is SAW selected?
MattShilling 0:fb56047b523f 195 do_saw=true; //select Sawtooth
MattShilling 0:fb56047b523f 196 do_sine=false; //ensure nothing else selected
MattShilling 0:fb56047b523f 197 do_square=false;
MattShilling 0:fb56047b523f 198 }
MattShilling 0:fb56047b523f 199
MattShilling 0:fb56047b523f 200 if( select == SQUARE ) { //is RIGHT pressed?
MattShilling 0:fb56047b523f 201 do_saw=false;
MattShilling 0:fb56047b523f 202 do_sine=false;
MattShilling 0:fb56047b523f 203 do_square=true; //user wants squarewave
MattShilling 0:fb56047b523f 204 }
MattShilling 0:fb56047b523f 205
MattShilling 0:fb56047b523f 206 if( select == SINE ) { //is LEFT pressed?
MattShilling 0:fb56047b523f 207 do_saw=false;
MattShilling 0:fb56047b523f 208 do_sine=true; //user wants sinewave
MattShilling 0:fb56047b523f 209 do_square=false;
MattShilling 0:fb56047b523f 210 }
MattShilling 0:fb56047b523f 211
MattShilling 0:fb56047b523f 212 if(first ) { //wave has ended
MattShilling 0:fb56047b523f 213
MattShilling 0:fb56047b523f 214 if( do_saw ) {
MattShilling 0:fb56047b523f 215 //pc.printf("I'm doing saw\r\n");
MattShilling 0:fb56047b523f 216 sawTooth();
MattShilling 0:fb56047b523f 217 }
MattShilling 0:fb56047b523f 218 else if( do_square ) {
MattShilling 0:fb56047b523f 219 //pc.printf("I'm doing square\r\n");
MattShilling 0:fb56047b523f 220 squareWave();
MattShilling 0:fb56047b523f 221 }
MattShilling 0:fb56047b523f 222 else if( do_sine ) {
MattShilling 0:fb56047b523f 223 //pc.printf("I'm doing sine\r\n");
MattShilling 0:fb56047b523f 224 sineWave();
MattShilling 0:fb56047b523f 225 }
MattShilling 0:fb56047b523f 226 else { //Default if no button pushed
MattShilling 0:fb56047b523f 227 //pc.printf("I'm doing default (sine)\r\n");
MattShilling 0:fb56047b523f 228 sineWave();
MattShilling 0:fb56047b523f 229 }
MattShilling 0:fb56047b523f 230 //banner(); //we came back, ask user what next
MattShilling 0:fb56047b523f 231 }
MattShilling 0:fb56047b523f 232 }
MattShilling 0:fb56047b523f 233 }