HW6 for OCE560

Dependencies:   mbed

Fork of shomberg_hw_5 by Russell Shomberg

Committer:
rshomberg
Date:
Tue Oct 23 14:04:01 2018 +0000
Revision:
8:d6560caeda9a
Parent:
7:1a43ff8ccc08
Child:
9:da0b72918880
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rshomberg 0:82635173a413 1 /**
rshomberg 8:d6560caeda9a 2 Temperature Sensor for hw6
rshomberg 0:82635173a413 3 main.cpp
rshomberg 0:82635173a413 4
rshomberg 6:8cfa0216554f 5 Purpose: Read signal from TMP36 connected to pin20
rshomberg 6:8cfa0216554f 6 Display output voltage to terminal
rshomberg 6:8cfa0216554f 7 Display difference in mV of output voltage to starting voltage on 2x 7-Segment Displays
rshomberg 6:8cfa0216554f 8 See outputs for pin configuration
rshomberg 6:8cfa0216554f 9 Toggle switch connected to pin07 to convert displays to degC
rshomberg 6:8cfa0216554f 10
rshomberg 8:d6560caeda9a 11 @author Russell Shomberg
rshomberg 8:d6560caeda9a 12 @created 2018-10-23
rshomberg 8:d6560caeda9a 13 @revised 2018-10-23
rshomberg 8:d6560caeda9a 14 @version 0.0
rshomberg 0:82635173a413 15
rshomberg 6:8cfa0216554f 16 Issues: No Decimal point for temperature
rshomberg 7:1a43ff8ccc08 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 6:8cfa0216554f 29
rshomberg 6:8cfa0216554f 30 //// Pin Map for 7-Seg starts bottom left, goes CCW
rshomberg 6:8cfa0216554f 31 BusOut Seg1(p12,p13,p14,p15,p16,p17,p18,p19); //01 02 03 04 05 06 07 08 09 10
rshomberg 6:8cfa0216554f 32 BusOut Seg2(p21,p22,p23,p24,p25,p26,p27,p28); //E D CC C DP B A CC F G
rshomberg 6:8cfa0216554f 33
rshomberg 6:8cfa0216554f 34 // FUNCTIONS
rshomberg 6:8cfa0216554f 35 char SegConvert(int SegValue);
rshomberg 0:82635173a413 36
rshomberg 0:82635173a413 37 // VARIABLES
rshomberg 6:8cfa0216554f 38 int outputT= 0;
rshomberg 6:8cfa0216554f 39 float v0;
rshomberg 6:8cfa0216554f 40 float deltav;
rshomberg 6:8cfa0216554f 41 float temp0;
rshomberg 6:8cfa0216554f 42 float deltatemp;
rshomberg 6:8cfa0216554f 43 float val;
rshomberg 6:8cfa0216554f 44 int ones;
rshomberg 6:8cfa0216554f 45 int tens;
rshomberg 6:8cfa0216554f 46
rshomberg 0:82635173a413 47
rshomberg 0:82635173a413 48 int main() {
rshomberg 6:8cfa0216554f 49 // read starting voltage from temperature sensor
rshomberg 6:8cfa0216554f 50 v0 = Ain*3.5*1000;
rshomberg 6:8cfa0216554f 51
rshomberg 0:82635173a413 52 while(1) {
rshomberg 6:8cfa0216554f 53 // Read Switch if on output temp else output mV
rshomberg 6:8cfa0216554f 54 outputT = switchPosition;
rshomberg 6:8cfa0216554f 55
rshomberg 6:8cfa0216554f 56 // Read temperature sensor
rshomberg 6:8cfa0216554f 57 deltav = Ain*3.5*1000-v0;
rshomberg 1:cbee04784c60 58
rshomberg 6:8cfa0216554f 59 // Output to terminal
rshomberg 6:8cfa0216554f 60 if (outputT) {
rshomberg 6:8cfa0216554f 61 // Convert to temp
rshomberg 6:8cfa0216554f 62 deltatemp = deltav/10;
rshomberg 6:8cfa0216554f 63 printf("Temperature Difference = %1.2f degC\n\r", deltatemp);
rshomberg 6:8cfa0216554f 64 val = deltatemp;
rshomberg 6:8cfa0216554f 65 }
rshomberg 6:8cfa0216554f 66
rshomberg 6:8cfa0216554f 67 else {
rshomberg 6:8cfa0216554f 68 printf("Voltage Difference = %1.2f mV\n\r", deltav);
rshomberg 6:8cfa0216554f 69 val = deltav;
rshomberg 6:8cfa0216554f 70 }
rshomberg 4:098e3d869055 71
rshomberg 6:8cfa0216554f 72 // Convert val to ones and tens char
rshomberg 6:8cfa0216554f 73 ones = fmod(rint(val),10);
rshomberg 6:8cfa0216554f 74 tens = fmod(rint(val) / 10, 10);
rshomberg 1:cbee04784c60 75
rshomberg 6:8cfa0216554f 76
rshomberg 6:8cfa0216554f 77 Seg1 = ~SegConvert(ones);
rshomberg 6:8cfa0216554f 78 Seg2 = ~SegConvert(tens);
rshomberg 6:8cfa0216554f 79
rshomberg 6:8cfa0216554f 80
rshomberg 6:8cfa0216554f 81 wait(1);
rshomberg 6:8cfa0216554f 82
rshomberg 0:82635173a413 83 }
rshomberg 6:8cfa0216554f 84
rshomberg 0:82635173a413 85 }
rshomberg 6:8cfa0216554f 86
rshomberg 6:8cfa0216554f 87
rshomberg 6:8cfa0216554f 88 //ones: 12 13 14 15 16 17 18 19
rshomberg 6:8cfa0216554f 89 // A B C D E F G P
rshomberg 6:8cfa0216554f 90 //tens: 21 22 23 24 25 26 27 28
rshomberg 6:8cfa0216554f 91 //
rshomberg 6:8cfa0216554f 92
rshomberg 6:8cfa0216554f 93 char SegConvert(int SegValue) { // function 'SegConvert'
rshomberg 6:8cfa0216554f 94 char SegByte=0x00;
rshomberg 6:8cfa0216554f 95 switch (abs(SegValue)) { // ABCDEFGP
rshomberg 6:8cfa0216554f 96 case 0 : SegByte= 0x3F;break; // 11111100 binary
rshomberg 6:8cfa0216554f 97 case 1 : SegByte= 0x06;break; // 01100000 binary
rshomberg 6:8cfa0216554f 98 case 2 : SegByte= 0x5B;break; // 11110110 binary
rshomberg 6:8cfa0216554f 99 case 3 : SegByte= 0x4F;break; // 10011110 binary
rshomberg 6:8cfa0216554f 100 case 4 : SegByte= 0x66;break; // 11001100 binary
rshomberg 6:8cfa0216554f 101 case 5 : SegByte= 0x6D;break; // 11011010 binary
rshomberg 6:8cfa0216554f 102 case 6 : SegByte= 0x7D;break; // 11111010 binary
rshomberg 6:8cfa0216554f 103 case 7 : SegByte= 0x07;break; // 00001110 binary
rshomberg 6:8cfa0216554f 104 case 8 : SegByte= 0x7F;break; // 11111110 binary
rshomberg 6:8cfa0216554f 105 case 9 : SegByte= 0x6F;break; // 11011110 binary
rshomberg 6:8cfa0216554f 106 }
rshomberg 6:8cfa0216554f 107 return SegByte;
rshomberg 6:8cfa0216554f 108 }