Russell Shomberg
/
shomberg_hw_6
HW6 for OCE560
Fork of shomberg_hw_5 by
main.cpp@4:098e3d869055, 2018-10-08 (annotated)
- Committer:
- rshomberg
- Date:
- Mon Oct 08 20:39:35 2018 +0000
- Revision:
- 4:098e3d869055
- Parent:
- 3:56972a65cd0a
- Child:
- 5:dbd163551a58
publish for submission
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rshomberg | 0:82635173a413 | 1 | /** |
rshomberg | 4:098e3d869055 | 2 | MBED Analug Output Triangle Wave and PWM Wave |
rshomberg | 0:82635173a413 | 3 | main.cpp |
rshomberg | 0:82635173a413 | 4 | |
rshomberg | 3:56972a65cd0a | 5 | Purpose: Output a triangle waveform |
rshomberg | 4:098e3d869055 | 6 | Output a PWM Signal of same frequency |
rshomberg | 4:098e3d869055 | 7 | Adjust LED1 brightness with PWM Signal |
rshomberg | 0:82635173a413 | 8 | Adjust frequency using variable resistor |
rshomberg | 0:82635173a413 | 9 | Adjust amplitude using switch |
rshomberg | 0:82635173a413 | 10 | |
rshomberg | 0:82635173a413 | 11 | @author Russell Shomberg |
rshomberg | 4:098e3d869055 | 12 | @version 1.0 2018-10-04 |
rshomberg | 0:82635173a413 | 13 | |
rshomberg | 2:312a0a9c4485 | 14 | Issues: |
rshomberg | 2:312a0a9c4485 | 15 | Frequency and amplitude only change at the start of the waveform by design |
rshomberg | 0:82635173a413 | 16 | |
rshomberg | 0:82635173a413 | 17 | |
rshomberg | 0:82635173a413 | 18 | */ |
rshomberg | 0:82635173a413 | 19 | |
rshomberg | 0:82635173a413 | 20 | // INCLUDES |
rshomberg | 0:82635173a413 | 21 | #include "mbed.h" |
rshomberg | 0:82635173a413 | 22 | |
rshomberg | 0:82635173a413 | 23 | // INPUTS |
rshomberg | 2:312a0a9c4485 | 24 | DigitalIn switchPosition(p7); // wire p7 to middle connection of 2 position switch between Vref and GND |
rshomberg | 2:312a0a9c4485 | 25 | AnalogIn Ain(p20); // wire p20 to a variable resister connected from Vref and GND |
rshomberg | 0:82635173a413 | 26 | |
rshomberg | 0:82635173a413 | 27 | // OUTPUTS |
rshomberg | 2:312a0a9c4485 | 28 | Serial pc(USBTX, USBRX); // for debugging |
rshomberg | 2:312a0a9c4485 | 29 | AnalogOut Aout(p18); // leave open lead on p18 for signal output |
rshomberg | 4:098e3d869055 | 30 | PwmOut myled(LED1); |
rshomberg | 4:098e3d869055 | 31 | PwmOut mypwm(p21); |
rshomberg | 0:82635173a413 | 32 | |
rshomberg | 0:82635173a413 | 33 | // VARIABLES |
rshomberg | 2:312a0a9c4485 | 34 | float period; // range between ~0 and 1 (seconds) |
rshomberg | 4:098e3d869055 | 35 | float pwmfreq; // switch between 1 and -.5 |
rshomberg | 2:312a0a9c4485 | 36 | float i; // index |
rshomberg | 0:82635173a413 | 37 | |
rshomberg | 0:82635173a413 | 38 | int main() { |
rshomberg | 4:098e3d869055 | 39 | |
rshomberg | 0:82635173a413 | 40 | while(1) { |
rshomberg | 2:312a0a9c4485 | 41 | // Check settings at start of loop which are changed with |
rshomberg | 1:cbee04784c60 | 42 | period = Ain; |
rshomberg | 4:098e3d869055 | 43 | if (switchPosition==1) {pwmfreq = 1*period;} |
rshomberg | 4:098e3d869055 | 44 | else {pwmfreq = .5*period;} |
rshomberg | 1:cbee04784c60 | 45 | |
rshomberg | 2:312a0a9c4485 | 46 | // Debugging code |
rshomberg | 4:098e3d869055 | 47 | //printf("PWM Frequency = %1.2f Hz\n\r", pwmfreq); |
rshomberg | 4:098e3d869055 | 48 | //printf("Analog Period = %1.2f seconds\n\r", period); |
rshomberg | 4:098e3d869055 | 49 | |
rshomberg | 4:098e3d869055 | 50 | //mypwm.period(period); |
rshomberg | 4:098e3d869055 | 51 | //mypwm = pwmfreq; |
rshomberg | 1:cbee04784c60 | 52 | |
rshomberg | 1:cbee04784c60 | 53 | for (i=0;i<1;i=i+.001){ |
rshomberg | 4:098e3d869055 | 54 | myled = 1-i; |
rshomberg | 4:098e3d869055 | 55 | mypwm = 1-i; |
rshomberg | 4:098e3d869055 | 56 | Aout = i; |
rshomberg | 1:cbee04784c60 | 57 | wait(0.001*period); |
rshomberg | 0:82635173a413 | 58 | } |
rshomberg | 1:cbee04784c60 | 59 | |
rshomberg | 1:cbee04784c60 | 60 | for (i=1;i>0;i=i-.001){ |
rshomberg | 4:098e3d869055 | 61 | Aout = i; |
rshomberg | 4:098e3d869055 | 62 | myled = 1-i; |
rshomberg | 4:098e3d869055 | 63 | mypwm = 1-i; |
rshomberg | 1:cbee04784c60 | 64 | wait(0.001*period); |
rshomberg | 0:82635173a413 | 65 | } |
rshomberg | 0:82635173a413 | 66 | } |
rshomberg | 0:82635173a413 | 67 | } |