Skip to content

Instantly share code, notes, and snippets.

@aleksas
Last active May 12, 2020 17:27
Show Gist options
  • Save aleksas/d099d8401e613767df96099ad5c1ef2c to your computer and use it in GitHub Desktop.
Save aleksas/d099d8401e613767df96099ad5c1ef2c to your computer and use it in GitHub Desktop.
guess
#include <SPI.h>
// Generated using https://www.daycounter.com/Calculators/Triangle-Wave-Generator-Calculator.phtml
// max value 2^9
// num values 128
// val 0-4096 | mA 0-300
//
int16_t* pValues = NULL;
int valuesCount = 0;
// Due to ADN8810 output impedance drop off
const int currentChangeFrequecy = 10000;
const int triangleMmodulationFrequency = 520;
const float minCurrent = 0.02335; //23.35 mA
const float maxCurrent = 0.02345; //23.75 mA
const int16_t maxValue12Bit = 4095;
const float maxAmps = 0.3;
const float conversionFactor = maxValue12Bit / maxAmps;
// Assume address 0
bool fillTriangleValues(int modulationPPFrequency, int16_t minValue, int16_t maxValue) {
int16_t value = 0;
int16_t step = 0;
int valuesCount = 0;
int valuesHalfCount = 0;
if (modulationPPFrequency > currentChangeFrequecy){
return -1;
}
if (pValues)
{
free(pValues);
pValues = NULL;
}
valuesCount = currentChangeFrequecy / modulationPPFrequency;
valuesHalfCount = valuesCount/2;
pValues = (int16_t*) malloc(valuesCount * sizeof(int16_t));
if (!pValues) {
valuesCount = 0;
return -2;
}
step = (maxValue - minValue)/valuesHalfCount;
if (minValue < 0) return -3;
if (maxValue > maxValue12Bit) return -4;
value = minValue;
for (int i = 0; i < valuesCount; i++)
{
int sign = i < valuesHalfCount ? 1 : -1;
value += step * sign;
value = value > maxValue ? maxValue : value;
value = value < minValue ? minValue : value;
pValues[i] = value;
}
return 0;
}
// using two incompatible SPI devices, A and B. Incompatible means that they need different SPI_MODE
const int slavePin = 4;
// set up the speed, data order and data mode
// modes: https://www.arduino.cc/en/reference/SPI
SPISettings settingsB(currentChangeFrequecy, MSBFIRST, SPI_MODE3);
void setup() {
if (fillTriangleValues(triangleMmodulationFrequency, ) >= 0)
{
// set the Slave Select Pins as outputs:
pinMode (slavePin, OUTPUT);
// initialize SPI:
SPI.begin();
}
}
uint8_t i = 0;
void loop() {
SPI.beginTransaction(settingsB);
SPI.transfer16(triangle[i]);
SPI.endTransaction();
i = (i + 1) % triangle_len;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment