]> kolegite.com Git - vmks.git/commitdiff
FEAT: Added a SPI digital potentiometer example
authorVladimir Garistov <vl.garistov@gmail.com>
Mon, 15 Jul 2024 10:20:54 +0000 (13:20 +0300)
committerVladimir Garistov <vl.garistov@gmail.com>
Mon, 15 Jul 2024 10:20:54 +0000 (13:20 +0300)
Examples/SPI/SPI_digital_pot_MCP4261/SPI_digital_pot_MCP4261.ino [new file with mode: 0644]

diff --git a/Examples/SPI/SPI_digital_pot_MCP4261/SPI_digital_pot_MCP4261.ino b/Examples/SPI/SPI_digital_pot_MCP4261/SPI_digital_pot_MCP4261.ino
new file mode 100644 (file)
index 0000000..6c09275
--- /dev/null
@@ -0,0 +1,34 @@
+#include <SPI.h>
+
+#define INPUT_PIN      A0
+
+#define WRITE_CMD      0x00
+#define INCR_CMD       0x01
+#define DECR_CMD       0x02
+#define READ_CMD       0x03
+
+#define WIPER0_ADDR    0x00
+#define WIPER1_ADDR    0x01
+
+#define ADDR_OFFSET    12U
+#define CMD_OFFSET     10U
+
+#define POT_MIN                0U
+#define POT_MAX                256U
+
+void setup()
+{
+       pinMode(INPUT_PIN, INPUT);
+       SPI.begin();
+}
+
+void loop()
+{
+       SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
+       SPI.transfer16(
+               WRITE_CMD << CMD_OFFSET |
+               WIPER0_ADDR << ADDR_OFFSET |
+               map(analogRead(INPUT_PIN), 0, 1023, 0, 256));
+       SPI.endTransaction();
+       delay(5);
+}