Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:ab22c15897d9, 2018-10-16 (annotated)
- Committer:
- imcelroy
- Date:
- Tue Oct 16 13:42:06 2018 +0000
- Revision:
- 0:ab22c15897d9
- Child:
- 1:d7b669bcfdd7
complete hw 5.1
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| imcelroy | 0:ab22c15897d9 | 1 | #include "mbed.h" |
| imcelroy | 0:ab22c15897d9 | 2 | #include<string> |
| imcelroy | 0:ab22c15897d9 | 3 | #include "stdio.h" |
| imcelroy | 0:ab22c15897d9 | 4 | #include <sstream> |
| imcelroy | 0:ab22c15897d9 | 5 | |
| imcelroy | 0:ab22c15897d9 | 6 | /* |
| imcelroy | 0:ab22c15897d9 | 7 | Ian McElroy |
| imcelroy | 0:ab22c15897d9 | 8 | 10/16/18 |
| imcelroy | 0:ab22c15897d9 | 9 | |
| 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 | //Ones = SegConvert(abs(rint(mv_start - mv))); |
| imcelroy | 0:ab22c15897d9 | 45 | } |
| imcelroy | 0:ab22c15897d9 | 46 | else{//if switch off, print degrees C |
| imcelroy | 0:ab22c15897d9 | 47 | 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 | 48 | displayOnesTens(abs(rint(start_tempC - tempC))); |
| imcelroy | 0:ab22c15897d9 | 49 | } |
| imcelroy | 0:ab22c15897d9 | 50 | wait(1); |
| imcelroy | 0:ab22c15897d9 | 51 | |
| imcelroy | 0:ab22c15897d9 | 52 | } |
| imcelroy | 0:ab22c15897d9 | 53 | } |
| imcelroy | 0:ab22c15897d9 | 54 | |
| imcelroy | 0:ab22c15897d9 | 55 | char SegConvert(char SegValue) { |
| imcelroy | 0:ab22c15897d9 | 56 | char SegByte=0x00; |
| imcelroy | 0:ab22c15897d9 | 57 | switch (SegValue) { |
| imcelroy | 0:ab22c15897d9 | 58 | case 0 : SegByte = 0x3F;break; // 00111111 binary |
| imcelroy | 0:ab22c15897d9 | 59 | case 1 : SegByte = 0x06;break; // 00000110 binary |
| imcelroy | 0:ab22c15897d9 | 60 | case 2 : SegByte = 0x5B;break; // 01011011 binary |
| imcelroy | 0:ab22c15897d9 | 61 | case 3 : SegByte = 0x4F;break; // 01001111 binary |
| imcelroy | 0:ab22c15897d9 | 62 | case 4 : SegByte = 0x66;break; // 01100110 binary |
| imcelroy | 0:ab22c15897d9 | 63 | case 5 : SegByte = 0x6D;break; // 01101101 binary |
| imcelroy | 0:ab22c15897d9 | 64 | case 6 : SegByte = 0x7D;break; // 01111101 binary |
| imcelroy | 0:ab22c15897d9 | 65 | case 7 : SegByte = 0x07;break; // 00000111 binary |
| imcelroy | 0:ab22c15897d9 | 66 | case 8 : SegByte = 0x7F;break; // 01111111 binary |
| imcelroy | 0:ab22c15897d9 | 67 | case 9 : SegByte = 0x6F;break; // 01101111 binary |
| imcelroy | 0:ab22c15897d9 | 68 | } |
| imcelroy | 0:ab22c15897d9 | 69 | char flip = ~SegByte; //flip since led takes low pin voltage instead of high |
| imcelroy | 0:ab22c15897d9 | 70 | return flip; |
| imcelroy | 0:ab22c15897d9 | 71 | } |
| imcelroy | 0:ab22c15897d9 | 72 | |
| imcelroy | 0:ab22c15897d9 | 73 | void displayOnesTens(int number) { |
| imcelroy | 0:ab22c15897d9 | 74 | //converts number to two seperate hexnumbers for the leds |
| imcelroy | 0:ab22c15897d9 | 75 | int ones = number % 10; |
| imcelroy | 0:ab22c15897d9 | 76 | int tens = number / 10 % 10; |
| imcelroy | 0:ab22c15897d9 | 77 | |
| imcelroy | 0:ab22c15897d9 | 78 | Ones = SegConvert(ones); |
| imcelroy | 0:ab22c15897d9 | 79 | Tens = SegConvert(tens); |
| imcelroy | 0:ab22c15897d9 | 80 | |
| imcelroy | 0:ab22c15897d9 | 81 | } |