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:83dfc83e48bf, 2013-11-06 (annotated)
- Committer:
- yzakizon
- Date:
- Wed Nov 06 07:31:40 2013 +0000
- Revision:
- 0:83dfc83e48bf
Initial release to get MCP9700 temperature sensor
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yzakizon | 0:83dfc83e48bf | 1 | #include "mbed.h" |
| yzakizon | 0:83dfc83e48bf | 2 | |
| yzakizon | 0:83dfc83e48bf | 3 | DigitalOut myled(LED1); |
| yzakizon | 0:83dfc83e48bf | 4 | Serial pc(USBTX, USBRX); |
| yzakizon | 0:83dfc83e48bf | 5 | AnalogIn tempPin(p15); |
| yzakizon | 0:83dfc83e48bf | 6 | |
| yzakizon | 0:83dfc83e48bf | 7 | float tempTotal; |
| yzakizon | 0:83dfc83e48bf | 8 | uint32_t tempDiv; |
| yzakizon | 0:83dfc83e48bf | 9 | |
| yzakizon | 0:83dfc83e48bf | 10 | #define MAX_TEMP_AVG 10 |
| yzakizon | 0:83dfc83e48bf | 11 | |
| yzakizon | 0:83dfc83e48bf | 12 | void sampleTemperature(); |
| yzakizon | 0:83dfc83e48bf | 13 | void sampleTemperatures(); |
| yzakizon | 0:83dfc83e48bf | 14 | void resetTemperatureSamples(); |
| yzakizon | 0:83dfc83e48bf | 15 | float getAvgTemperature(); |
| yzakizon | 0:83dfc83e48bf | 16 | |
| yzakizon | 0:83dfc83e48bf | 17 | void sampleTemperature() |
| yzakizon | 0:83dfc83e48bf | 18 | { |
| yzakizon | 0:83dfc83e48bf | 19 | tempTotal += tempPin; //we use 3.3V from VOUT |
| yzakizon | 0:83dfc83e48bf | 20 | tempDiv++; |
| yzakizon | 0:83dfc83e48bf | 21 | } |
| yzakizon | 0:83dfc83e48bf | 22 | |
| yzakizon | 0:83dfc83e48bf | 23 | void sampleTemperatures() |
| yzakizon | 0:83dfc83e48bf | 24 | { |
| yzakizon | 0:83dfc83e48bf | 25 | for(int i=0;i<MAX_TEMP_AVG;i++) { |
| yzakizon | 0:83dfc83e48bf | 26 | sampleTemperature(); |
| yzakizon | 0:83dfc83e48bf | 27 | wait(0.1); |
| yzakizon | 0:83dfc83e48bf | 28 | } |
| yzakizon | 0:83dfc83e48bf | 29 | } |
| yzakizon | 0:83dfc83e48bf | 30 | |
| yzakizon | 0:83dfc83e48bf | 31 | void resetTemperatureSamples() |
| yzakizon | 0:83dfc83e48bf | 32 | { |
| yzakizon | 0:83dfc83e48bf | 33 | tempTotal = 0.0; |
| yzakizon | 0:83dfc83e48bf | 34 | tempDiv = 0; |
| yzakizon | 0:83dfc83e48bf | 35 | } |
| yzakizon | 0:83dfc83e48bf | 36 | float getAvgTemperature() |
| yzakizon | 0:83dfc83e48bf | 37 | { |
| yzakizon | 0:83dfc83e48bf | 38 | /* Temperature calculations is (VOut - V0)/Tc |
| yzakizon | 0:83dfc83e48bf | 39 | * VOut = V out from sensor |
| yzakizon | 0:83dfc83e48bf | 40 | * V0 = Voltage on 0 Celcius, 500mv for MCP9700 |
| yzakizon | 0:83dfc83e48bf | 41 | * Tc = Temperatur constant, 10mV/C |
| yzakizon | 0:83dfc83e48bf | 42 | */ |
| yzakizon | 0:83dfc83e48bf | 43 | float tempAvg = (((tempTotal*3300)/(float)tempDiv)-500.0)/10.0; |
| yzakizon | 0:83dfc83e48bf | 44 | return tempAvg; |
| yzakizon | 0:83dfc83e48bf | 45 | } |
| yzakizon | 0:83dfc83e48bf | 46 | |
| yzakizon | 0:83dfc83e48bf | 47 | int main() { |
| yzakizon | 0:83dfc83e48bf | 48 | tempDiv = 0; |
| yzakizon | 0:83dfc83e48bf | 49 | pc.printf("MBED ready..\r\n"); |
| yzakizon | 0:83dfc83e48bf | 50 | sampleTemperatures(); |
| yzakizon | 0:83dfc83e48bf | 51 | pc.printf("Temp: %.2f.\r\n",getAvgTemperature()); |
| yzakizon | 0:83dfc83e48bf | 52 | while(1) { |
| yzakizon | 0:83dfc83e48bf | 53 | sampleTemperature(); |
| yzakizon | 0:83dfc83e48bf | 54 | |
| yzakizon | 0:83dfc83e48bf | 55 | myled = 1; |
| yzakizon | 0:83dfc83e48bf | 56 | wait(0.5); |
| yzakizon | 0:83dfc83e48bf | 57 | myled = 0; |
| yzakizon | 0:83dfc83e48bf | 58 | wait(0.5); |
| yzakizon | 0:83dfc83e48bf | 59 | |
| yzakizon | 0:83dfc83e48bf | 60 | if (tempDiv > MAX_TEMP_AVG) { |
| yzakizon | 0:83dfc83e48bf | 61 | pc.printf("Temp: %.2f.\r\n",getAvgTemperature()); |
| yzakizon | 0:83dfc83e48bf | 62 | resetTemperatureSamples(); |
| yzakizon | 0:83dfc83e48bf | 63 | } |
| yzakizon | 0:83dfc83e48bf | 64 | } |
| yzakizon | 0:83dfc83e48bf | 65 | } |