HW4

Dependencies:   mbed

Committer:
rshomberg
Date:
Tue Oct 23 14:00:23 2018 +0000
Revision:
7:334ad83c7596
Parent:
6:8cfa0216554f
Reverted to previously overwritten copy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rshomberg 0:82635173a413 1 /**
rshomberg 7:334ad83c7596 2 MBED Analug Output Triangle Wave and PWM Wave
rshomberg 0:82635173a413 3 main.cpp
rshomberg 0:82635173a413 4
rshomberg 7:334ad83c7596 5 Purpose: Output a triangle waveform
rshomberg 7:334ad83c7596 6 Output a PWM Signal of same frequency
rshomberg 7:334ad83c7596 7 Adjust LED1 brightness with PWM Signal
rshomberg 7:334ad83c7596 8 Adjust frequency using variable resistor
rshomberg 7:334ad83c7596 9 Adjust amplitude using switch
rshomberg 7:334ad83c7596 10
rshomberg 7:334ad83c7596 11 @author Russell Shomberg
rshomberg 7:334ad83c7596 12 @version 1.0 2018-10-04
rshomberg 6:8cfa0216554f 13
rshomberg 7:334ad83c7596 14 Issues:
rshomberg 7:334ad83c7596 15 Frequency and amplitude only change at the start of the waveform by design
rshomberg 7:334ad83c7596 16
rshomberg 0:82635173a413 17
rshomberg 7:334ad83c7596 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 7:334ad83c7596 29 AnalogOut Aout(p18); // leave open lead on p18 for signal output
rshomberg 7:334ad83c7596 30 PwmOut myled(LED1);
rshomberg 7:334ad83c7596 31 PwmOut mypwm(p21);
rshomberg 0:82635173a413 32
rshomberg 0:82635173a413 33 // VARIABLES
rshomberg 7:334ad83c7596 34 float period; // range between ~0 and 1 (seconds)
rshomberg 7:334ad83c7596 35 float pwmfreq; // switch between 1 and -.5
rshomberg 7:334ad83c7596 36 float i; // index
rshomberg 0:82635173a413 37
rshomberg 0:82635173a413 38 int main() {
rshomberg 7:334ad83c7596 39
rshomberg 0:82635173a413 40 while(1) {
rshomberg 7:334ad83c7596 41 // Check settings at start of loop which are changed with
rshomberg 7:334ad83c7596 42 period = Ain;
rshomberg 7:334ad83c7596 43 if (switchPosition==1) {pwmfreq = 1*period;}
rshomberg 7:334ad83c7596 44 else {pwmfreq = .5*period;}
rshomberg 1:cbee04784c60 45
rshomberg 7:334ad83c7596 46 // Debugging code
rshomberg 7:334ad83c7596 47 //printf("PWM Frequency = %1.2f Hz\n\r", pwmfreq);
rshomberg 7:334ad83c7596 48 //printf("Analog Period = %1.2f seconds\n\r", period);
rshomberg 4:098e3d869055 49
rshomberg 7:334ad83c7596 50 //mypwm.period(period);
rshomberg 7:334ad83c7596 51 //mypwm = pwmfreq;
rshomberg 1:cbee04784c60 52
rshomberg 7:334ad83c7596 53 for (i=0;i<1;i=i+.001){
rshomberg 7:334ad83c7596 54 myled = 1-i;
rshomberg 7:334ad83c7596 55 mypwm = 1-i;
rshomberg 7:334ad83c7596 56 Aout = i;
rshomberg 7:334ad83c7596 57 wait(0.001*period);
rshomberg 7:334ad83c7596 58 }
rshomberg 7:334ad83c7596 59
rshomberg 7:334ad83c7596 60 for (i=1;i>0;i=i-.001){
rshomberg 7:334ad83c7596 61 Aout = i;
rshomberg 7:334ad83c7596 62 myled = 1-i;
rshomberg 7:334ad83c7596 63 mypwm = 1-i;
rshomberg 7:334ad83c7596 64 wait(0.001*period);
rshomberg 7:334ad83c7596 65 }
rshomberg 0:82635173a413 66 }
rshomberg 0:82635173a413 67 }