Sawtooth wave form on oscilloscope with variable amplitude and frequency

Dependencies:   mbed

Committer:
ccschneider
Date:
Thu Sep 27 14:24:55 2018 +0000
Revision:
0:ce916b042621
Sawtooth waveform on oscilloscope with variable amplitude and frequency

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ccschneider 0:ce916b042621 1 // Cecilia Schneider, OCE 360, September 24,2018
ccschneider 0:ce916b042621 2 // HW #3, exercise 4, Analog I/O
ccschneider 0:ce916b042621 3 // Sawtooth waveform on DAC output to view on oscilloscope
ccschneider 0:ce916b042621 4 #include "mbed.h"
ccschneider 0:ce916b042621 5 AnalogOut Aout(p18); //oscilliscope on pin 18
ccschneider 0:ce916b042621 6 AnalogIn Ain(p20); //potentiometer on pin 20
ccschneider 0:ce916b042621 7 DigitalIn Sin(p7); //switch on pin 7
ccschneider 0:ce916b042621 8 float ADCin;
ccschneider 0:ce916b042621 9 float Switchin;
ccschneider 0:ce916b042621 10 float i;
ccschneider 0:ce916b042621 11 float j;
ccschneider 0:ce916b042621 12
ccschneider 0:ce916b042621 13 int main() {
ccschneider 0:ce916b042621 14 while(1) {
ccschneider 0:ce916b042621 15 ADCin = Ain; //
ccschneider 0:ce916b042621 16 if (Sin == 1) {
ccschneider 0:ce916b042621 17 Switchin = 1; //when the switch is on, the amplitude will be 1
ccschneider 0:ce916b042621 18 }
ccschneider 0:ce916b042621 19 else if (Sin == 0) {
ccschneider 0:ce916b042621 20 Switchin = 0.5; //when the switch is off, the amplitude will be 0.5
ccschneider 0:ce916b042621 21 }
ccschneider 0:ce916b042621 22 for (i=0;i<Switchin;i=i+0.001) {
ccschneider 0:ce916b042621 23 Aout=i;
ccschneider 0:ce916b042621 24 wait(0.001*ADCin); //allows user to control frequency with potentiometer
ccschneider 0:ce916b042621 25 }
ccschneider 0:ce916b042621 26 for (j=Switchin;j>0;j=j-0.001) {
ccschneider 0:ce916b042621 27 Aout=j;
ccschneider 0:ce916b042621 28 wait(0.001*ADCin);
ccschneider 0:ce916b042621 29 }
ccschneider 0:ce916b042621 30 }
ccschneider 0:ce916b042621 31 }