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:87de77e861fc, 2022-06-22 (annotated)
- Committer:
- Pavle998
- Date:
- Wed Jun 22 17:19:53 2022 +0000
- Revision:
- 0:87de77e861fc
Code example for ambient temperature mesurement
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Pavle998 | 0:87de77e861fc | 1 | #include "mbed.h" |
| Pavle998 | 0:87de77e861fc | 2 | |
| Pavle998 | 0:87de77e861fc | 3 | #define IrThermo_Addr (0x5A<<1) |
| Pavle998 | 0:87de77e861fc | 4 | #define Tamb 0x06 |
| Pavle998 | 0:87de77e861fc | 5 | #define Tobj 0x07 |
| Pavle998 | 0:87de77e861fc | 6 | I2C i2c(D14, D15); |
| Pavle998 | 0:87de77e861fc | 7 | Serial pc(USBTX, USBRX); |
| Pavle998 | 0:87de77e861fc | 8 | |
| Pavle998 | 0:87de77e861fc | 9 | int temperature=0; |
| Pavle998 | 0:87de77e861fc | 10 | char data_read[2]; |
| Pavle998 | 0:87de77e861fc | 11 | char data_write[1]= {Tobj}; |
| Pavle998 | 0:87de77e861fc | 12 | char data_write1[1]={Tamb}; |
| Pavle998 | 0:87de77e861fc | 13 | char data_add[1]= {(0x2E)}; |
| Pavle998 | 0:87de77e861fc | 14 | |
| Pavle998 | 0:87de77e861fc | 15 | char counter = 0; |
| Pavle998 | 0:87de77e861fc | 16 | uint32_t cc=0; |
| Pavle998 | 0:87de77e861fc | 17 | uint32_t cc1=0; |
| Pavle998 | 0:87de77e861fc | 18 | int main() { |
| Pavle998 | 0:87de77e861fc | 19 | //i2c.start(); |
| Pavle998 | 0:87de77e861fc | 20 | while(1) { |
| Pavle998 | 0:87de77e861fc | 21 | i2c.write(IrThermo_Addr, data_write, 1, 1); |
| Pavle998 | 0:87de77e861fc | 22 | i2c.read(IrThermo_Addr, data_read, 2, 0); |
| Pavle998 | 0:87de77e861fc | 23 | temperature = (int) ((int)data_read[1] << 8) | data_read[0]; |
| Pavle998 | 0:87de77e861fc | 24 | temperature = (temperature * 0.02 - 273.15); |
| Pavle998 | 0:87de77e861fc | 25 | pc.printf("Tobj=%d, ",temperature); |
| Pavle998 | 0:87de77e861fc | 26 | |
| Pavle998 | 0:87de77e861fc | 27 | i2c.write(IrThermo_Addr,data_write1 , 1, 1); |
| Pavle998 | 0:87de77e861fc | 28 | i2c.read(IrThermo_Addr, data_read, 2, 0); |
| Pavle998 | 0:87de77e861fc | 29 | temperature = (int) ((int)data_read[1] << 8) | data_read[0]; |
| Pavle998 | 0:87de77e861fc | 30 | temperature = (temperature * 0.02 - 273.15); |
| Pavle998 | 0:87de77e861fc | 31 | pc.printf("Tamb=%d\n",temperature); |
| Pavle998 | 0:87de77e861fc | 32 | |
| Pavle998 | 0:87de77e861fc | 33 | } |
| Pavle998 | 0:87de77e861fc | 34 | } |