Skip to content

Commit e29e583

Browse files
rdondeadprogram
authored andcommitted
fix: use int64 in ReadTemperature to avoid overflow
1 parent 310df7a commit e29e583

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/machine/machine_rp2_adc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ func ReadTemperature() (millicelsius int32) {
100100
rp.ADC.CS.SetBits(rp.ADC_CS_TS_EN)
101101

102102
// T = 27 - (ADC_voltage - 0.706)/0.001721
103-
return (27000<<16 - (int32(thermChan.getVoltage())-706<<16)*581) >> 16
103+
// 1/0.001721 ≈ 581
104+
return int32(((int64(27000) << 16) - ((int64(thermChan.getVoltage()) - (int64(706) << 16)) * 581)) >> 16)
104105
}
105106

106107
// waitForReady spins waiting for the ADC peripheral to become ready.

0 commit comments

Comments
 (0)