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