|
| 1 | +import teensy |
| 2 | + |
| 3 | +const ADDRESS = 0x77 |
| 4 | +const Oversampling = 0 |
| 5 | + |
| 6 | +I2C = teensy.OpenI2C(1) ' use I2C pins 16 and 17 |
| 7 | +I2C.setClock(100000) |
| 8 | +Init() |
| 9 | + |
| 10 | +while(1) |
| 11 | + ii++ |
| 12 | + m = StartMeasurement() |
| 13 | + print ii, m[0]; "°C", m[1]/100; "hPa" |
| 14 | + delay(500) |
| 15 | +wend |
| 16 | + |
| 17 | +sub Init() |
| 18 | + local buffer |
| 19 | + ' Get chip id |
| 20 | + I2C.write(ADDRESS, 0xD0) |
| 21 | + buffer = I2C.read(ADDRESS, 1) |
| 22 | + if(buffer != 85) |
| 23 | + print "Error Chip ID does not match" |
| 24 | + while(1) |
| 25 | + wend |
| 26 | + endif |
| 27 | + |
| 28 | + ' Read calibration values |
| 29 | + ac1 = short(Read_2Bytes(0xAA)) |
| 30 | + ac2 = short(Read_2Bytes(0xAC)) |
| 31 | + ac3 = short(Read_2Bytes(0xAE)) |
| 32 | + ac4 = Read_2Bytes(0xB0) |
| 33 | + ac5 = Read_2Bytes(0xB2) |
| 34 | + ac6 = Read_2Bytes(0xB4) |
| 35 | + b1 = short(Read_2Bytes(0xB6)) |
| 36 | + b2 = short(Read_2Bytes(0xB8)) |
| 37 | + mb = short(Read_2Bytes(0xBA)) |
| 38 | + mc = short(Read_2Bytes(0xBC)) |
| 39 | + md = short(Read_2Bytes(0xBE)) |
| 40 | +end |
| 41 | + |
| 42 | +func short(dat) |
| 43 | + if dat > 32767 then |
| 44 | + return dat - 65536 |
| 45 | + else |
| 46 | + return dat |
| 47 | + endif |
| 48 | +end |
| 49 | + |
| 50 | +func StartMeasurement() |
| 51 | + |
| 52 | + local buffer, UncompTemp, Temperature, UncompPres, Pressure |
| 53 | + local x1, x2, x3, b3, b5, b6, b4, b7 |
| 54 | + |
| 55 | + ' Read uncompensated temperature |
| 56 | + buffer = [0xF4, 0x2E] |
| 57 | + I2C.write(ADDRESS, buffer) |
| 58 | + delay(5) |
| 59 | + buffer[0] = Read_Byte(0xF6) |
| 60 | + buffer[1] = Read_Byte(0xF7) |
| 61 | + UncompTemp = (buffer[0] lshift 8) BOR buffer[1] |
| 62 | + |
| 63 | + ' Read uncompensated pressure |
| 64 | + buffer[0] = 0xF4 |
| 65 | + buffer[1] = 0x34 + (Oversampling lshift 6) |
| 66 | + I2C.write(ADDRESS, buffer) |
| 67 | + delay(2 + (3 lshift Oversampling)) |
| 68 | + I2C.write(ADDRESS, 0xF6) |
| 69 | + buffer = I2C.read(ADDRESS, 3) |
| 70 | + UncompPres = ((buffer[0] lshift 16) + (buffer[1] lshift 8) + buffer[0]) rshift (8 - Oversampling) |
| 71 | + |
| 72 | + ' Calculate true temperature |
| 73 | + x1 = ((UncompTemp - ac6) * ac5) rshift 15 |
| 74 | + x2 = (mc lshift 11) / (x1 + md) |
| 75 | + b5 = x1 + x2 |
| 76 | + Temperature = ((b5 + 8) rshift 4) / 10.0 |
| 77 | + |
| 78 | + ' Calculate true pressure |
| 79 | + b6 = b5 - 4000 |
| 80 | + x1 = (b2 * (b6 * b6) rshift 12) rshift 11 |
| 81 | + x2 = (ac2 * b6) rshift 11 |
| 82 | + x3 = x1 + x2 |
| 83 | + b3 = (((ac1 * 4 + x3) lshift Oversampling) + 2) rshift 2 |
| 84 | + x1 = (ac3 * b6) rshift 13 |
| 85 | + x2 = (b1 * ((b6 * b6) rshift 12)) rshift 16 |
| 86 | + x3 = ((x1 + x2) + 2) rshift 2 |
| 87 | + b4 = (ac4 * (x3 + 32768)) rshift 15 |
| 88 | + b7 = ((UncompPres - b3) * (50000 rshift Oversampling)) |
| 89 | + if (b7 < 0x80000000) |
| 90 | + Pressure = (b7 lshift 1) / b4 |
| 91 | + else |
| 92 | + Pressure = (b7 / b4) lshift 1 |
| 93 | + endif |
| 94 | + x1 = (Pressure rshift 8) * (Pressure rshift 8) |
| 95 | + x1 = (x1 * 3038) rshift 16 |
| 96 | + x2 = (-7357 * Pressure) rshift 16 |
| 97 | + Pressure = Pressure + ((x1 + x2 + 3791) rshift 4) |
| 98 | + |
| 99 | + return [Temperature, Pressure] |
| 100 | +end |
| 101 | + |
| 102 | +func Read_2Bytes(Reg) |
| 103 | + local buffer |
| 104 | + I2C.write(ADDRESS, Reg) |
| 105 | + buffer = I2C.read(ADDRESS, 2) |
| 106 | + temp = (buffer[0] lshift 8) BOR buffer[1] |
| 107 | + return temp |
| 108 | +end |
| 109 | + |
| 110 | +func Read_Byte(Reg) |
| 111 | + local buffer |
| 112 | + I2C.write(ADDRESS, Reg) |
| 113 | + buffer = I2C.read(ADDRESS, 1) |
| 114 | + return buffer |
| 115 | +end |
| 116 | + |
0 commit comments