HW6 for OCE560

Dependencies:   mbed

Fork of shomberg_hw_5 by Russell Shomberg

Committer:
rshomberg
Date:
Thu Oct 11 13:19:25 2018 +0000
Revision:
5:dbd163551a58
Parent:
4:098e3d869055
Child:
6:8cfa0216554f
Minor change to publish

Who changed what in which revision?

UserRevisionLine numberNew 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 5:dbd163551a58 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 }