- for (i = 0; i < 100; i++)
- ao_arch_nop();
- ao_gpio_set(AO_MAX6691_GPIO, AO_MAX6691_PIN, 1);
- ao_arch_nop();
- ao_set_input(AO_MAX6691_GPIO, AO_MAX6691_PIN);
- i = 0;
- p = 1;
- for (tick = 0; i < 8 && tick < 10000; tick++)
- {
- v = ao_gpio_get(AO_MAX6691_GPIO, AO_MAX6691_PIN);
- if (v != p) {
- ao_max6691_data[i++] = tick;
- p = v;
- }
+ int i;
+ for (i = 0; i < AO_MAX6691_SAMPLES; i++)
+ printf("%d: %5u\n", i, ao_max6691_raw[i]);
+
+ for (i = 0; i < AO_MAX6691_CHANNELS; i++) {
+ uint16_t t_high = ao_max6691.sensor[i].t_high;
+ uint16_t t_low = ao_max6691.sensor[i].t_low;
+
+ /*
+ * From the MAX6691 data sheet
+ *
+ * Thigh Vext Rext
+ * ----- = ---- - 0.0002 = ---------- - 0.0002
+ * Tlow Vref Rext - Rth
+ *
+ * We want to find Rth given Rext and the timing values
+ *
+ * Thigh Rext
+ * ----- + 0.0002 = ----------
+ * Tlow Rext + Rth
+ *
+ * V = (Thigh / Tlow + 0.0002)
+ *
+ * (Rext + Rth) * V = Rext
+ *
+ * Rext * V + Rth * V = Rext
+ *
+ * Rth * V = Rext - Rext * V
+ *
+ * Rth * V = Rext * (1 - V)
+ *
+ * Rth = Rext * (1 - V) / V
+ */
+
+ float V = (float) t_high / (float) t_low + 0.0002f;
+
+ float Rth = R_EXT * (1 - V) / V;
+
+ printf("%d: high %5u low %5u ohms: %7g\n", i, t_high, t_low, Rth);