Program reads temperature from internal thermo meter and voltage on A0. Both values are displayed on LCD
Dependencies: Freetronics_16x2_LCD mbed
Fork of Freetronics_16x2_LCD by
main.cpp@3:8ed2cd310fa7, 2015-01-31 (annotated)
- Committer:
- jirik09
- Date:
- Sat Jan 31 15:53:05 2015 +0000
- Revision:
- 3:8ed2cd310fa7
- Parent:
- 2:4e0ed7dc6420
Just test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 2:4e0ed7dc6420 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
screamer | 2:4e0ed7dc6420 | 2 | * |
screamer | 2:4e0ed7dc6420 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
screamer | 2:4e0ed7dc6420 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
screamer | 2:4e0ed7dc6420 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
screamer | 2:4e0ed7dc6420 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
screamer | 2:4e0ed7dc6420 | 7 | * Software is furnished to do so, subject to the following conditions: |
screamer | 2:4e0ed7dc6420 | 8 | * |
screamer | 2:4e0ed7dc6420 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
screamer | 2:4e0ed7dc6420 | 10 | * substantial portions of the Software. |
screamer | 2:4e0ed7dc6420 | 11 | * |
screamer | 2:4e0ed7dc6420 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
screamer | 2:4e0ed7dc6420 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
screamer | 2:4e0ed7dc6420 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
screamer | 2:4e0ed7dc6420 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
screamer | 2:4e0ed7dc6420 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
screamer | 2:4e0ed7dc6420 | 17 | */ |
screamer | 2:4e0ed7dc6420 | 18 | |
screamer | 0:4a0b425b71ff | 19 | #include "mbed.h" |
screamer | 0:4a0b425b71ff | 20 | #include "freetronicsLCDShield.h" |
screamer | 0:4a0b425b71ff | 21 | |
jirik09 | 3:8ed2cd310fa7 | 22 | Serial pc(SERIAL_TX, SERIAL_RX); |
jirik09 | 3:8ed2cd310fa7 | 23 | |
jirik09 | 3:8ed2cd310fa7 | 24 | DigitalOut myled(LED1); |
jirik09 | 3:8ed2cd310fa7 | 25 | AnalogIn analog_value(A0); |
screamer | 0:4a0b425b71ff | 26 | |
jirik09 | 3:8ed2cd310fa7 | 27 | #define MV(x) ((0xFFF*x)/3300) |
jirik09 | 3:8ed2cd310fa7 | 28 | #define V25 1.398 |
jirik09 | 3:8ed2cd310fa7 | 29 | #define SLOPE 0.0043 |
jirik09 | 3:8ed2cd310fa7 | 30 | |
jirik09 | 3:8ed2cd310fa7 | 31 | int main() |
jirik09 | 3:8ed2cd310fa7 | 32 | { |
jirik09 | 3:8ed2cd310fa7 | 33 | wait(0.1); |
jirik09 | 3:8ed2cd310fa7 | 34 | |
jirik09 | 3:8ed2cd310fa7 | 35 | freetronicsLCDShield lcd(D6, D7, D2, D3, D4, D5, D8, A1); |
screamer | 0:4a0b425b71ff | 36 | // turn on the back light (it's off by default) |
screamer | 0:4a0b425b71ff | 37 | lcd.setBackLight(true); |
jirik09 | 3:8ed2cd310fa7 | 38 | |
jirik09 | 3:8ed2cd310fa7 | 39 | |
jirik09 | 3:8ed2cd310fa7 | 40 | |
jirik09 | 3:8ed2cd310fa7 | 41 | |
jirik09 | 3:8ed2cd310fa7 | 42 | |
jirik09 | 3:8ed2cd310fa7 | 43 | |
screamer | 0:4a0b425b71ff | 44 | |
screamer | 0:4a0b425b71ff | 45 | // print the first line and wait 3 sec |
jirik09 | 3:8ed2cd310fa7 | 46 | lcd.cls(); |
jirik09 | 3:8ed2cd310fa7 | 47 | |
jirik09 | 3:8ed2cd310fa7 | 48 | int i = 1; |
jirik09 | 3:8ed2cd310fa7 | 49 | float meas=0; |
jirik09 | 3:8ed2cd310fa7 | 50 | float LP=0.025; |
jirik09 | 3:8ed2cd310fa7 | 51 | float LPT=0.0025; |
jirik09 | 3:8ed2cd310fa7 | 52 | float temp=0; |
jirik09 | 3:8ed2cd310fa7 | 53 | lcd.setCursorPosition(0, 0); |
jirik09 | 3:8ed2cd310fa7 | 54 | lcd.printf("Napeti"); |
jirik09 | 3:8ed2cd310fa7 | 55 | ADC1->CR2|=ADC_CR2_TSVREFE; |
jirik09 | 3:8ed2cd310fa7 | 56 | while(1) { |
jirik09 | 3:8ed2cd310fa7 | 57 | meas = LP*analog_value.read()+(1-LP)*meas; // Converts and read the analog input value |
jirik09 | 3:8ed2cd310fa7 | 58 | |
jirik09 | 3:8ed2cd310fa7 | 59 | ADC_RegularChannelConfig(ADC1, 16, 1, ADC_SampleTime_7Cycles5); |
screamer | 0:4a0b425b71ff | 60 | |
jirik09 | 3:8ed2cd310fa7 | 61 | //// Temperature (in °C) = {(V25 - VSENSE) / Avg_Slope} + 25. |
jirik09 | 3:8ed2cd310fa7 | 62 | //v25 = 1.43 |
jirik09 | 3:8ed2cd310fa7 | 63 | //avg_slope 4.3mv/c |
jirik09 | 3:8ed2cd310fa7 | 64 | |
jirik09 | 3:8ed2cd310fa7 | 65 | ADC_SoftwareStartConvCmd(ADC1, ENABLE); // Start conversion |
jirik09 | 3:8ed2cd310fa7 | 66 | |
jirik09 | 3:8ed2cd310fa7 | 67 | while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); // Wait end of conversion |
jirik09 | 3:8ed2cd310fa7 | 68 | |
jirik09 | 3:8ed2cd310fa7 | 69 | temp= LPT*((V25-(float)(ADC_GetConversionValue(ADC1))*3.3/4096)/SLOPE+25)+(1-LPT)*temp; // Get conversion value |
jirik09 | 3:8ed2cd310fa7 | 70 | |
jirik09 | 3:8ed2cd310fa7 | 71 | |
jirik09 | 3:8ed2cd310fa7 | 72 | if(meas > 0.5) { // If the value is greater than 1000 mV toggle the LED |
jirik09 | 3:8ed2cd310fa7 | 73 | myled = 1; |
jirik09 | 3:8ed2cd310fa7 | 74 | } else { |
jirik09 | 3:8ed2cd310fa7 | 75 | myled = 0; |
jirik09 | 3:8ed2cd310fa7 | 76 | } |
jirik09 | 3:8ed2cd310fa7 | 77 | i++; |
jirik09 | 3:8ed2cd310fa7 | 78 | if(i==200) { |
jirik09 | 3:8ed2cd310fa7 | 79 | i=0; |
jirik09 | 3:8ed2cd310fa7 | 80 | lcd.setCursorPosition(0, 0); |
jirik09 | 3:8ed2cd310fa7 | 81 | lcd.printf("%2.2f C ", temp); |
jirik09 | 3:8ed2cd310fa7 | 82 | lcd.setCursorPosition(1, 0); |
jirik09 | 3:8ed2cd310fa7 | 83 | lcd.printf("%1.4f V", meas*3.3); |
jirik09 | 3:8ed2cd310fa7 | 84 | } |
screamer | 1:cb23381652eb | 85 | wait(0.001); |
screamer | 0:4a0b425b71ff | 86 | } |
screamer | 0:4a0b425b71ff | 87 | } |