Ian McElroy / Mbed 2 deprecated mcelroy_hw_5_1

Dependencies:   mbed

Committer:
imcelroy
Date:
Tue Oct 16 13:49:36 2018 +0000
Revision:
3:56c8ab2df3aa
Parent:
2:635e61271822
clean up code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
imcelroy 1:d7b669bcfdd7 1 #include "mbed.h"
imcelroy 0:ab22c15897d9 2
imcelroy 0:ab22c15897d9 3 /*
imcelroy 0:ab22c15897d9 4 Ian McElroy
imcelroy 0:ab22c15897d9 5 10/16/18
imcelroy 0:ab22c15897d9 6
imcelroy 2:635e61271822 7 Displays the start, current, and difference voltage in millivolts on a serial
imcelroy 2:635e61271822 8 output to the user's laptop, and two 7-segment LED displays.
imcelroy 2:635e61271822 9 Uses switch to change to degrees C.
imcelroy 0:ab22c15897d9 10 */
imcelroy 0:ab22c15897d9 11
imcelroy 0:ab22c15897d9 12 BusOut Ones(p12,p13,p14,p15,p16,p17,p18,p19);// A,B,C,D,E,F,G,DP
imcelroy 0:ab22c15897d9 13 BusOut Tens(p21,p22,p23,p24,p25,p26,p27,p28);// A,B,C,D,E,F,G,DP
imcelroy 0:ab22c15897d9 14 AnalogIn tempR(p20);
imcelroy 0:ab22c15897d9 15
imcelroy 0:ab22c15897d9 16 DigitalIn Switch(p7);
imcelroy 0:ab22c15897d9 17
imcelroy 0:ab22c15897d9 18 float ADCdata;
imcelroy 0:ab22c15897d9 19 float mv_to_temp = 10; // mv/C
imcelroy 0:ab22c15897d9 20 int v_to_mV = 1000; //convert to mV
imcelroy 0:ab22c15897d9 21
imcelroy 0:ab22c15897d9 22 char SegConvert(char SegValue);
imcelroy 0:ab22c15897d9 23 void displayOnesTens(int number);
imcelroy 0:ab22c15897d9 24 Serial pc(USBTX,USBRX);
imcelroy 0:ab22c15897d9 25
imcelroy 0:ab22c15897d9 26 int main() {
imcelroy 0:ab22c15897d9 27
imcelroy 0:ab22c15897d9 28 //float v_ref = 3.3;
imcelroy 0:ab22c15897d9 29
imcelroy 0:ab22c15897d9 30 float mv_start = tempR*v_to_mV; //get start voltage in mV
imcelroy 0:ab22c15897d9 31
imcelroy 0:ab22c15897d9 32 float start_tempC = tempR*v_to_mV/mv_to_temp; //convert sensor to temp C
imcelroy 0:ab22c15897d9 33
imcelroy 0:ab22c15897d9 34 while (1) {
imcelroy 0:ab22c15897d9 35
imcelroy 0:ab22c15897d9 36 float mv = tempR*v_to_mV; //get voltage in mV
imcelroy 0:ab22c15897d9 37 float tempC = tempR*v_to_mV/mv_to_temp; //convert sensor to temp C
imcelroy 0:ab22c15897d9 38 int diff = abs(rint(mv_start - mv));
imcelroy 0:ab22c15897d9 39
imcelroy 0:ab22c15897d9 40 if(Switch){ //if switch on, print voltage
imcelroy 0:ab22c15897d9 41
imcelroy 0:ab22c15897d9 42 pc.printf("Start mv: %.3f, Current mV: %.3f, difference from start mV: %.3f \n\r",mv_start, mv, mv_start-mv);
imcelroy 0:ab22c15897d9 43 displayOnesTens(abs(rint(mv_start - mv)));
imcelroy 0:ab22c15897d9 44 }
imcelroy 0:ab22c15897d9 45 else{//if switch off, print degrees C
imcelroy 0:ab22c15897d9 46 pc.printf("Start temp C: %.3f, Current temp C: %.3f, difference from start temp C: %.3f \n\r",start_tempC, tempC, start_tempC - tempC);
imcelroy 0:ab22c15897d9 47 displayOnesTens(abs(rint(start_tempC - tempC)));
imcelroy 0:ab22c15897d9 48 }
imcelroy 0:ab22c15897d9 49 wait(1);
imcelroy 0:ab22c15897d9 50
imcelroy 0:ab22c15897d9 51 }
imcelroy 0:ab22c15897d9 52 }
imcelroy 0:ab22c15897d9 53
imcelroy 0:ab22c15897d9 54 char SegConvert(char SegValue) {
imcelroy 0:ab22c15897d9 55 char SegByte=0x00;
imcelroy 0:ab22c15897d9 56 switch (SegValue) {
imcelroy 0:ab22c15897d9 57 case 0 : SegByte = 0x3F;break; // 00111111 binary
imcelroy 0:ab22c15897d9 58 case 1 : SegByte = 0x06;break; // 00000110 binary
imcelroy 0:ab22c15897d9 59 case 2 : SegByte = 0x5B;break; // 01011011 binary
imcelroy 0:ab22c15897d9 60 case 3 : SegByte = 0x4F;break; // 01001111 binary
imcelroy 0:ab22c15897d9 61 case 4 : SegByte = 0x66;break; // 01100110 binary
imcelroy 0:ab22c15897d9 62 case 5 : SegByte = 0x6D;break; // 01101101 binary
imcelroy 0:ab22c15897d9 63 case 6 : SegByte = 0x7D;break; // 01111101 binary
imcelroy 0:ab22c15897d9 64 case 7 : SegByte = 0x07;break; // 00000111 binary
imcelroy 0:ab22c15897d9 65 case 8 : SegByte = 0x7F;break; // 01111111 binary
imcelroy 0:ab22c15897d9 66 case 9 : SegByte = 0x6F;break; // 01101111 binary
imcelroy 0:ab22c15897d9 67 }
imcelroy 0:ab22c15897d9 68 char flip = ~SegByte; //flip since led takes low pin voltage instead of high
imcelroy 0:ab22c15897d9 69 return flip;
imcelroy 0:ab22c15897d9 70 }
imcelroy 0:ab22c15897d9 71
imcelroy 0:ab22c15897d9 72 void displayOnesTens(int number) {
imcelroy 0:ab22c15897d9 73 //converts number to two seperate hexnumbers for the leds
imcelroy 3:56c8ab2df3aa 74 int ones = number % 10; //get ones digit
imcelroy 3:56c8ab2df3aa 75 int tens = number / 10 % 10; //get tens digit
imcelroy 3:56c8ab2df3aa 76
imcelroy 3:56c8ab2df3aa 77 //send to respective displays
imcelroy 0:ab22c15897d9 78 Ones = SegConvert(ones);
imcelroy 0:ab22c15897d9 79 Tens = SegConvert(tens);
imcelroy 0:ab22c15897d9 80
imcelroy 0:ab22c15897d9 81 }