crjeder / hx711_spi

This is a library for the hx711 chip. It uses SPI instead of bit banging for more reliability.
MIT License
14 stars 4 forks source link

Tweak to work on stm32f103 #6

Closed andreyk0 closed 2 years ago

andreyk0 commented 2 years ago

Hey! I've tried connecting it to an stm32f103 ("blue pill") board and ran into a couple of issues that this patch seems to address.

First ChAGain128 seemed to be missing a 0, noticed only because initially I tried with SPI mode 0 where the MOSI (used here as a clock driver) pin was going "high" in between bytes, resulting in an extra 1/0 transition.

That said running SPI in the mode 1 seems closer to the original intention of the code (not sure about rpi though).

This PR seems to fix it for me.

See a couple of screenshots below:

SPI mode 0 (the missing 0 fix already included) SPI-mode0

SPI mode 1 SPI-mode1

crjeder commented 2 years ago

Hi! I am not sure if I read your diagrams correctly. As far as I understand the difference between mode 0 and 1 is that sampling happens on the rising or falling edges respectively. When I compare both diagrams the values are sampeled correct in both cases. Tha's probably because the 'clock' (MOSI or SDO) is shifted by a half cycle and the SDI (or MISO) values are sampled allways in a steady state of the signal. Of course that's more obvious in case of mode 2. I have to test this on the Raspberry.

andreyk0 commented 2 years ago

Right! So the diff between 0 and 1 is as you say but notice the "default" state of the pin (yellow line is MOSI you use to construct "fake clock"). In mode 0 on this board it goes high and in mode 1 it stays low.

So, initially when I tried (without the missing leading 0 in the enum fix, with mode 0) there was effectively an extra transition because the pin would go up "high" in between 4-pulse "clock" bytes. If the next pin transition is to "high" (due to high bit set) - it still works, it just "stretches" a bit but if the next bit is 0 - there's an extra transition.

Hope that makes sense.

andreyk0 commented 2 years ago

So, I guess we can close it, you've merged my PR, thanks!