#include <SPI.h>
#define PIN_SPI_CS 17
#define PIN_SPI_SCK 18
#define PIN_SPI_TX 19
/* milivolt */
static unsigned int va = 2000;
static unsigned int vb = 2000;
static char line[100];
static unsigned int pos = 0;
static void process_line(void)
{
char buffer[100];
snprintf(buffer, sizeof(buffer), "line = (%s)\n", line);
Serial.print(buffer);
unsigned int value;
if (sscanf(line, "%u", &value) == 1)
{
va = vb = value;
}
else if (sscanf(line, "A%u", &value) == 1)
{
va = value;
}
else if (sscanf(line, "B%u", &value) == 1)
{
vb = value;
}
snprintf(buffer, sizeof(buffer), "va = %u, vb = %u\n", va, vb);
Serial.print(buffer);
}
static void process_serial_rx(void)
{
if (Serial1.available())
{
int data = Serial1.read();
if (data == '\r')
{
line[pos] = '\0';
process_line();
pos = 0;
}
else
{
if (isascii(data))
{
if (pos < (sizeof(line) - 1))
{
line[pos++] = data;
}
}
}
}
}
void setup()
{
Serial.begin(115200);
Serial1.begin(115200);
SPI.setCS(PIN_SPI_CS);
SPI.setSCK(PIN_SPI_SCK);
SPI.setTX(PIN_SPI_TX);
SPI.begin(true);
}
void loop()
{
while (1)
{
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
/* A ve B cikislarini yaz */
SPI.transfer16(0x3000 | va);
SPI.transfer16(0xb000 | vb);
SPI.endTransaction();
process_serial_rx();
delay(100);
}
}