HW4

Dependencies:   mbed

Revision:
6:8cfa0216554f
Parent:
5:dbd163551a58
Child:
7:334ad83c7596
--- a/main.cpp	Thu Oct 11 13:19:25 2018 +0000
+++ b/main.cpp	Tue Oct 16 14:52:21 2018 +0000
@@ -1,19 +1,17 @@
 /**
-    MBED Analug Output Triangle Wave and PWM Wave
+    Temperature Sensor
     main.cpp
 
-    Purpose:    Output a triangle waveform
-                Output a PWM Signal of same frequency
-                Adjust LED1 brightness with PWM Signal
-                Adjust frequency using variable resistor
-                Adjust amplitude using switch
-
+    Purpose:    Read signal from TMP36 connected to pin20
+                Display output voltage to terminal
+                Display difference in mV of output voltage to starting voltage on 2x 7-Segment Displays
+                    See outputs for pin configuration
+                Toggle switch connected to pin07 to convert displays to degC
+    
     @author Russell Shomberg
-    @version 1.0 2018-10-04
+    @version 1.0 2018-10-15
     
-    Issues: 
-        Frequency and amplitude only change at the start of the waveform by design
-            
+    Issues: No Decimal point for temperature    
 
 */ 
 
@@ -26,42 +24,83 @@
 
 // OUTPUTS
 Serial pc(USBTX, USBRX);        // for debugging
-AnalogOut Aout(p18);            // leave open lead on p18 for signal output
-PwmOut myled(LED1);
-PwmOut mypwm(p21);
+
+//// Pin Map for 7-Seg starts bottom left, goes CCW
+BusOut Seg1(p12,p13,p14,p15,p16,p17,p18,p19); //01 02 03 04 05 06 07 08 09 10
+BusOut Seg2(p21,p22,p23,p24,p25,p26,p27,p28); //E  D  CC C  DP B  A  CC F  G
+
+// FUNCTIONS
+char SegConvert(int SegValue);
 
 // VARIABLES
-float period;       // range between ~0 and 1 (seconds)
-float pwmfreq;    // switch between 1 and -.5
-float i;            // index
+int outputT= 0;
+float v0;
+float deltav;
+float temp0;
+float deltatemp;
+float val;
+int ones;
+int tens;
+
 
 int main() {
-         
+    // read starting voltage from temperature sensor
+    v0 = Ain*3.5*1000;
+    
     while(1) {
-        // Check settings at start of loop which are changed with 
-        period = Ain;
-        if (switchPosition==1) {pwmfreq = 1*period;}
-        else {pwmfreq = .5*period;}
+        // Read Switch if on output temp else output mV
+        outputT = switchPosition;
+        
+        // Read temperature sensor
+        deltav = Ain*3.5*1000-v0;
         
-        // Debugging code
-        //printf("PWM Frequency = %1.2f Hz\n\r", pwmfreq);
-        //printf("Analog Period = %1.2f seconds\n\r", period);
+        // Output to terminal
+        if (outputT) {
+            // Convert to temp
+            deltatemp = deltav/10;
+            printf("Temperature Difference = %1.2f degC\n\r", deltatemp);
+            val = deltatemp;
+        }
+
+        else {
+            printf("Voltage Difference = %1.2f mV\n\r", deltav);
+            val = deltav;
+        }
         
-        //mypwm.period(period);
-        //mypwm = pwmfreq;
+        // Convert val to ones and tens char
+        ones = fmod(rint(val),10);
+        tens = fmod(rint(val) / 10, 10);
         
-        for (i=0;i<1;i=i+.001){
-                myled = 1-i;
-                mypwm = 1-i;
-                Aout = i;
-                wait(0.001*period);
-            }
-            
-        for (i=1;i>0;i=i-.001){
-                Aout = i;
-                myled = 1-i;
-                mypwm = 1-i;
-                wait(0.001*period);
-            }
+
+        Seg1 = ~SegConvert(ones);
+        Seg2 = ~SegConvert(tens);
+        
+        
+        wait(1);
+        
     }
+    
 }
+
+
+//ones: 12 13 14 15 16 17 18 19
+//      A  B  C  D  E  F  G  P 
+//tens: 21 22 23 24 25 26 27 28
+//
+
+char SegConvert(int SegValue) {         // function 'SegConvert'
+    char SegByte=0x00;
+    switch (abs(SegValue)) {           // ABCDEFGP
+        case 0 : SegByte= 0x3F;break;  // 11111100 binary
+        case 1 : SegByte= 0x06;break;  // 01100000 binary
+        case 2 : SegByte= 0x5B;break;  // 11110110 binary
+        case 3 : SegByte= 0x4F;break;  // 10011110 binary
+        case 4 : SegByte= 0x66;break;  // 11001100 binary
+        case 5 : SegByte= 0x6D;break;  // 11011010 binary
+        case 6 : SegByte= 0x7D;break;  // 11111010 binary
+        case 7 : SegByte= 0x07;break;  // 00001110 binary
+        case 8 : SegByte= 0x7F;break;  // 11111110 binary
+        case 9 : SegByte= 0x6F;break;  // 11011110 binary
+    }
+    return SegByte;
+}
\ No newline at end of file