7 Segment display for tm36 temperature sensor
Fork of shomberg_hw_4 by
main.cpp@7:1a43ff8ccc08, 2018-10-23 (annotated)
- Committer:
- rshomberg
- Date:
- Tue Oct 23 13:58:16 2018 +0000
- Revision:
- 7:1a43ff8ccc08
- Parent:
- 6:8cfa0216554f
Minor change to commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rshomberg | 0:82635173a413 | 1 | /** |
rshomberg | 6:8cfa0216554f | 2 | Temperature Sensor |
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 | 0:82635173a413 | 11 | @author Russell Shomberg |
rshomberg | 6:8cfa0216554f | 12 | @version 1.0 2018-10-15 |
rshomberg | 0:82635173a413 | 13 | |
rshomberg | 6:8cfa0216554f | 14 | Issues: No Decimal point for temperature |
rshomberg | 7:1a43ff8ccc08 | 15 | |
rshomberg | 5:dbd163551a58 | 16 | */ |
rshomberg | 0:82635173a413 | 17 | |
rshomberg | 0:82635173a413 | 18 | // INCLUDES |
rshomberg | 0:82635173a413 | 19 | #include "mbed.h" |
rshomberg | 0:82635173a413 | 20 | |
rshomberg | 0:82635173a413 | 21 | // INPUTS |
rshomberg | 2:312a0a9c4485 | 22 | DigitalIn switchPosition(p7); // wire p7 to middle connection of 2 position switch between Vref and GND |
rshomberg | 2:312a0a9c4485 | 23 | AnalogIn Ain(p20); // wire p20 to a variable resister connected from Vref and GND |
rshomberg | 0:82635173a413 | 24 | |
rshomberg | 0:82635173a413 | 25 | // OUTPUTS |
rshomberg | 2:312a0a9c4485 | 26 | Serial pc(USBTX, USBRX); // for debugging |
rshomberg | 6:8cfa0216554f | 27 | |
rshomberg | 6:8cfa0216554f | 28 | //// Pin Map for 7-Seg starts bottom left, goes CCW |
rshomberg | 6:8cfa0216554f | 29 | BusOut Seg1(p12,p13,p14,p15,p16,p17,p18,p19); //01 02 03 04 05 06 07 08 09 10 |
rshomberg | 6:8cfa0216554f | 30 | BusOut Seg2(p21,p22,p23,p24,p25,p26,p27,p28); //E D CC C DP B A CC F G |
rshomberg | 6:8cfa0216554f | 31 | |
rshomberg | 6:8cfa0216554f | 32 | // FUNCTIONS |
rshomberg | 6:8cfa0216554f | 33 | char SegConvert(int SegValue); |
rshomberg | 0:82635173a413 | 34 | |
rshomberg | 0:82635173a413 | 35 | // VARIABLES |
rshomberg | 6:8cfa0216554f | 36 | int outputT= 0; |
rshomberg | 6:8cfa0216554f | 37 | float v0; |
rshomberg | 6:8cfa0216554f | 38 | float deltav; |
rshomberg | 6:8cfa0216554f | 39 | float temp0; |
rshomberg | 6:8cfa0216554f | 40 | float deltatemp; |
rshomberg | 6:8cfa0216554f | 41 | float val; |
rshomberg | 6:8cfa0216554f | 42 | int ones; |
rshomberg | 6:8cfa0216554f | 43 | int tens; |
rshomberg | 6:8cfa0216554f | 44 | |
rshomberg | 0:82635173a413 | 45 | |
rshomberg | 0:82635173a413 | 46 | int main() { |
rshomberg | 6:8cfa0216554f | 47 | // read starting voltage from temperature sensor |
rshomberg | 6:8cfa0216554f | 48 | v0 = Ain*3.5*1000; |
rshomberg | 6:8cfa0216554f | 49 | |
rshomberg | 0:82635173a413 | 50 | while(1) { |
rshomberg | 6:8cfa0216554f | 51 | // Read Switch if on output temp else output mV |
rshomberg | 6:8cfa0216554f | 52 | outputT = switchPosition; |
rshomberg | 6:8cfa0216554f | 53 | |
rshomberg | 6:8cfa0216554f | 54 | // Read temperature sensor |
rshomberg | 6:8cfa0216554f | 55 | deltav = Ain*3.5*1000-v0; |
rshomberg | 1:cbee04784c60 | 56 | |
rshomberg | 6:8cfa0216554f | 57 | // Output to terminal |
rshomberg | 6:8cfa0216554f | 58 | if (outputT) { |
rshomberg | 6:8cfa0216554f | 59 | // Convert to temp |
rshomberg | 6:8cfa0216554f | 60 | deltatemp = deltav/10; |
rshomberg | 6:8cfa0216554f | 61 | printf("Temperature Difference = %1.2f degC\n\r", deltatemp); |
rshomberg | 6:8cfa0216554f | 62 | val = deltatemp; |
rshomberg | 6:8cfa0216554f | 63 | } |
rshomberg | 6:8cfa0216554f | 64 | |
rshomberg | 6:8cfa0216554f | 65 | else { |
rshomberg | 6:8cfa0216554f | 66 | printf("Voltage Difference = %1.2f mV\n\r", deltav); |
rshomberg | 6:8cfa0216554f | 67 | val = deltav; |
rshomberg | 6:8cfa0216554f | 68 | } |
rshomberg | 4:098e3d869055 | 69 | |
rshomberg | 6:8cfa0216554f | 70 | // Convert val to ones and tens char |
rshomberg | 6:8cfa0216554f | 71 | ones = fmod(rint(val),10); |
rshomberg | 6:8cfa0216554f | 72 | tens = fmod(rint(val) / 10, 10); |
rshomberg | 1:cbee04784c60 | 73 | |
rshomberg | 6:8cfa0216554f | 74 | |
rshomberg | 6:8cfa0216554f | 75 | Seg1 = ~SegConvert(ones); |
rshomberg | 6:8cfa0216554f | 76 | Seg2 = ~SegConvert(tens); |
rshomberg | 6:8cfa0216554f | 77 | |
rshomberg | 6:8cfa0216554f | 78 | |
rshomberg | 6:8cfa0216554f | 79 | wait(1); |
rshomberg | 6:8cfa0216554f | 80 | |
rshomberg | 0:82635173a413 | 81 | } |
rshomberg | 6:8cfa0216554f | 82 | |
rshomberg | 0:82635173a413 | 83 | } |
rshomberg | 6:8cfa0216554f | 84 | |
rshomberg | 6:8cfa0216554f | 85 | |
rshomberg | 6:8cfa0216554f | 86 | //ones: 12 13 14 15 16 17 18 19 |
rshomberg | 6:8cfa0216554f | 87 | // A B C D E F G P |
rshomberg | 6:8cfa0216554f | 88 | //tens: 21 22 23 24 25 26 27 28 |
rshomberg | 6:8cfa0216554f | 89 | // |
rshomberg | 6:8cfa0216554f | 90 | |
rshomberg | 6:8cfa0216554f | 91 | char SegConvert(int SegValue) { // function 'SegConvert' |
rshomberg | 6:8cfa0216554f | 92 | char SegByte=0x00; |
rshomberg | 6:8cfa0216554f | 93 | switch (abs(SegValue)) { // ABCDEFGP |
rshomberg | 6:8cfa0216554f | 94 | case 0 : SegByte= 0x3F;break; // 11111100 binary |
rshomberg | 6:8cfa0216554f | 95 | case 1 : SegByte= 0x06;break; // 01100000 binary |
rshomberg | 6:8cfa0216554f | 96 | case 2 : SegByte= 0x5B;break; // 11110110 binary |
rshomberg | 6:8cfa0216554f | 97 | case 3 : SegByte= 0x4F;break; // 10011110 binary |
rshomberg | 6:8cfa0216554f | 98 | case 4 : SegByte= 0x66;break; // 11001100 binary |
rshomberg | 6:8cfa0216554f | 99 | case 5 : SegByte= 0x6D;break; // 11011010 binary |
rshomberg | 6:8cfa0216554f | 100 | case 6 : SegByte= 0x7D;break; // 11111010 binary |
rshomberg | 6:8cfa0216554f | 101 | case 7 : SegByte= 0x07;break; // 00001110 binary |
rshomberg | 6:8cfa0216554f | 102 | case 8 : SegByte= 0x7F;break; // 11111110 binary |
rshomberg | 6:8cfa0216554f | 103 | case 9 : SegByte= 0x6F;break; // 11011110 binary |
rshomberg | 6:8cfa0216554f | 104 | } |
rshomberg | 6:8cfa0216554f | 105 | return SegByte; |
rshomberg | 6:8cfa0216554f | 106 | } |