GY-63_MS5611/libraries/MCP4725/examples/smooth2Value/smooth2Value.ino
2020-11-27 11:20:37 +01:00

90 lines
1.8 KiB
C++

//
// FILE: smooth2Value.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// PURPOSE: test mcp4725 lib
// DATE: 2013-12-01
// URL: https://github.com/RobTillaart/MCP4725
//
// Released to the public domain
//
#include "Wire.h"
#include "MCP4725.h"
MCP4725 DAC(0x62); // 0x62 or 0x63
void setup()
{
Serial.begin(115200);
Serial.print("MCP4725 test program: ");
Serial.println(MCP4725_VERSION);
DAC.begin();
Serial.print("\nValue:\t");
Serial.println(DAC.getValue());
Serial.println();
Serial.println("smooth2Value(2000, 10)");
smooth2Value(2000, 10);
Serial.print("Value:\t");
Serial.println(DAC.getValue());
Serial.println();
Serial.println("smooth2Value(100, 50)");
smooth2Value(100, 10);
Serial.print("Value:\t");
Serial.println(DAC.getValue());
Serial.println();
//////////////////////////////////////////////////
Serial.println("\n\nPERFORMANCE\n");
uint32_t start = micros();
for (int i = 0; i < 100; i++)
{
smooth2Value(i * 10, 10);
}
uint32_t end = micros();
Serial.print("100x DAC.smooth2Value(i*10, 10):\t");
Serial.println(end - start);
Serial.print("\nDone... (start triangle mode)");
}
void loop()
{
// different gradients
smooth2Value(4095, 4096);
smooth2Value(0, 4096);
smooth2Value(4095, 2048);
smooth2Value(0, 2048);
smooth2Value(4095, 1024);
smooth2Value(0, 1024);
}
int smooth2Value(uint16_t value, uint16_t steps)
{
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
if (steps > 1)
{
uint16_t startValue = DAC.getValue();
float delta = (1.0 * (value - startValue)) / steps;
for (uint16_t i = 0; i < steps - 1; i++)
{
DAC.setValue( round(startValue + i * delta) );
}
}
// get the end value right
return DAC.setValue(value);
}
// -- END OF FILE --