From: Vladimir Garistov Date: Mon, 15 Jul 2024 10:20:54 +0000 (+0300) Subject: FEAT: Added a SPI digital potentiometer example X-Git-Url: https://kolegite.com/gitweb/?a=commitdiff_plain;h=bb472ddaad7ab7056fb22f22738d1879867c9b03;p=vmks.git FEAT: Added a SPI digital potentiometer example --- 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 index 0000000..6c09275 --- /dev/null +++ b/Examples/SPI/SPI_digital_pot_MCP4261/SPI_digital_pot_MCP4261.ino @@ -0,0 +1,34 @@ +#include + +#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); +}