Ian McElroy / Mbed 2 deprecated mcelroy_hw_5_1

Dependencies:   mbed

Committer:
imcelroy
Date:
Tue Oct 16 13:46:54 2018 +0000
Revision:
1:d7b669bcfdd7
Parent:
0:ab22c15897d9
Child:
2:635e61271822
Displays the start, current, and difference voltage in millivolts on a serial output to the user's laptop, and two 7-segment LED displays. Uses switch to change to degrees C.

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