]> kolegite.com Git - vmks.git/commitdiff
FIX: I don't actually remember what was wrong ¯\_(ツ)_/¯
authorVladimir Garistov <vl.garistov@gmail.com>
Thu, 29 Aug 2024 18:17:40 +0000 (21:17 +0300)
committerVladimir Garistov <vl.garistov@gmail.com>
Thu, 29 Aug 2024 18:17:40 +0000 (21:17 +0300)
Examples/SPI/SPI_digital_pot_MCP4261/SPI_digital_pot_MCP4261.ino

index 6c092758a5f8e24af532ccba0f606b58aa0715bd..cbed6cc0e105e6018a8f168418bb5e2e8fd9f608 100644 (file)
@@ -1,14 +1,15 @@
 #include <SPI.h>
 
 #define INPUT_PIN      A0
+#define CS_PIN    53
 
-#define WRITE_CMD      0x00
-#define INCR_CMD       0x01
-#define DECR_CMD       0x02
-#define READ_CMD       0x03
+#define WRITE_CMD      ((uint16_t) 0x00)
+#define INCR_CMD       ((uint16_t) 0x01)
+#define DECR_CMD       ((uint16_t) 0x02)
+#define READ_CMD       ((uint16_t) 0x03)
 
-#define WIPER0_ADDR    0x00
-#define WIPER1_ADDR    0x01
+#define WIPER0_ADDR    ((uint16_t) 0x00)
+#define WIPER1_ADDR    ((uint16_t) 0x01)
 
 #define ADDR_OFFSET    12U
 #define CMD_OFFSET     10U
@@ -24,11 +25,12 @@ void setup()
 
 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));
+  uint16_t pot_val = map(analogRead(INPUT_PIN), 0, 1023, 0, 256);
+       SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
+  digitalWrite(CS_PIN, LOW);
+       SPI.transfer16(WRITE_CMD << CMD_OFFSET | WIPER0_ADDR << ADDR_OFFSET | pot_val);
+  SPI.transfer16(WRITE_CMD << CMD_OFFSET | WIPER1_ADDR << ADDR_OFFSET | pot_val);
+  digitalWrite(CS_PIN, HIGH);
        SPI.endTransaction();
        delay(5);
 }