mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-09-19 16:46:11 -04:00
+ version 0.1.00 initial
+ writeFastMode() only - basic + setValue(v) + smooth2Value(v, n) - in n steps to new value
This commit is contained in:
parent
85e3149d9e
commit
d7e68d4c3a
100
libraries/MCP4725/MCP4725.cpp
Normal file
100
libraries/MCP4725/MCP4725.cpp
Normal file
@ -0,0 +1,100 @@
|
||||
//
|
||||
// FILE: MCP4725.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: Simple MCP4725 DAC library for Arduino
|
||||
// VERSION: 1.0.00
|
||||
// HISTORY: See MCP4725.cpp
|
||||
// URL:
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.00 - 2013-11-24 initial version
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include "MCP4725.h"
|
||||
|
||||
MCP4725::MCP4725(uint8_t device)
|
||||
{
|
||||
_deviceAddress = device;
|
||||
_lastValue = 0;
|
||||
}
|
||||
|
||||
void MCP4725::begin()
|
||||
{
|
||||
Wire.begin();
|
||||
|
||||
TWBR = 72;
|
||||
// 0=1000 1=888 2=800 8=500
|
||||
// 12=400KHz 24=250 32=200 72=100 152=50
|
||||
// F_CPU/16+(2*TWBR) // TWBR is a uint8_t
|
||||
}
|
||||
|
||||
int MCP4725::setValue(uint16_t value)
|
||||
{
|
||||
// speed optimization
|
||||
if (value == _lastValue) return 0;
|
||||
|
||||
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
|
||||
|
||||
int rv = writeFastMode(value);
|
||||
if (rv == 0)
|
||||
{
|
||||
_lastValue = value;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
int MCP4725::smooth2Value(uint16_t value, uint8_t steps)
|
||||
{
|
||||
// speed optimization
|
||||
if (value == _lastValue) return 0;
|
||||
|
||||
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
|
||||
if (steps == 0) steps++;
|
||||
|
||||
uint16_t delta = (value - _lastValue)/steps;
|
||||
if (delta > 0)
|
||||
{
|
||||
uint16_t v = _lastValue;
|
||||
for (int i=0; i < steps-1; i++)
|
||||
{
|
||||
v += delta;
|
||||
writeFastMode(v);
|
||||
}
|
||||
}
|
||||
// be sure to get the end value right
|
||||
int rv = writeFastMode(value);
|
||||
_lastValue = value;
|
||||
return rv;
|
||||
}
|
||||
|
||||
uint16_t MCP4725::getValue()
|
||||
{
|
||||
return _lastValue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
|
||||
// PAGE 18 DATASHEET
|
||||
int MCP4725::writeFastMode(uint16_t value)
|
||||
{
|
||||
Wire.beginTransmission(_deviceAddress);
|
||||
uint8_t h = ((value / 256) & 0x0F); // set C0 = C1 = 0, no PDmode
|
||||
uint8_t l = value & 0xFF;
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
Wire.write(h);
|
||||
Wire.write(l);
|
||||
#else
|
||||
Wire.send(h);
|
||||
Wire.send(l);
|
||||
#endif
|
||||
return Wire.endTransmission();
|
||||
}
|
||||
|
||||
// END OF FILE
|
50
libraries/MCP4725/MCP4725.h
Normal file
50
libraries/MCP4725/MCP4725.h
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef MCP4725_H
|
||||
#define MCP4725_H
|
||||
//
|
||||
// FILE: MCP4725.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: Simple MCP4725 DAC library for Arduino
|
||||
// VERSION: 1.0.00
|
||||
// HISTORY: See MCP4725.cpp
|
||||
// URL:
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include <Wire.h>
|
||||
|
||||
#if defined(ARDUINO) && ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#include "Wstring.h"
|
||||
#include "Wiring.h"
|
||||
#endif
|
||||
|
||||
#define MCP4725_VERSION "1.0.00"
|
||||
|
||||
#define MCP4725_MAXVALUE 4095
|
||||
#define MCP4725_VALUE_ERROR -999
|
||||
|
||||
#define MCP4725_DAC 0x40
|
||||
|
||||
// #define MCP4725_EXTENDED
|
||||
|
||||
class MCP4725
|
||||
{
|
||||
public:
|
||||
MCP4725(uint8_t deviceAddress);
|
||||
|
||||
void begin();
|
||||
int setValue(uint16_t value);
|
||||
int smooth2Value(uint16_t value, uint8_t steps);
|
||||
uint16_t getValue();
|
||||
|
||||
private:
|
||||
uint8_t _deviceAddress;
|
||||
int writeFastMode(uint16_t value);
|
||||
uint16_t _lastValue;
|
||||
};
|
||||
|
||||
#endif
|
||||
// END OF FILE
|
86
libraries/MCP4725/examples/mcp4725_test/mcp4725_test.ino
Normal file
86
libraries/MCP4725/examples/mcp4725_test/mcp4725_test.ino
Normal file
@ -0,0 +1,86 @@
|
||||
//
|
||||
// FILE: mcp4725_test.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.00
|
||||
// PURPOSE: test mcp4725 lib
|
||||
// DATE: 2013-11-24
|
||||
// URL:
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#include <Wire.h>
|
||||
#include "MCP4725.h"
|
||||
|
||||
MCP4725 DAC(0x62); // 0x62 or 0x63
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println("MCP4725 test program: ");
|
||||
Serial.println(MCP4725_VERSION);
|
||||
|
||||
DAC.begin();
|
||||
|
||||
Serial.print("Value:\t");
|
||||
Serial.println(DAC.getValue());
|
||||
|
||||
Serial.println("setValue(100)");
|
||||
Serial.println(DAC.setValue(100));
|
||||
Serial.print("Value:\t");
|
||||
Serial.println(DAC.getValue());
|
||||
|
||||
Serial.println("setValue(200)");
|
||||
Serial.println(DAC.setValue(200));
|
||||
Serial.print("Value:\t");
|
||||
Serial.println(DAC.getValue());
|
||||
|
||||
Serial.println("smooth2Value(100, 10)");
|
||||
Serial.println(DAC.smooth2Value(100, 10));
|
||||
Serial.print("Value:\t");
|
||||
Serial.println(DAC.getValue());
|
||||
|
||||
Serial.println("smooth2Value(200, 10)");
|
||||
Serial.println(DAC.smooth2Value(200, 10));
|
||||
Serial.print("Value:\t");
|
||||
Serial.println(DAC.getValue());
|
||||
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
Serial.println("\n\nPERFORMANCE\n");
|
||||
|
||||
uint32_t start = micros();
|
||||
for (int i=0; i< 1000; i++)
|
||||
{
|
||||
volatile int x = DAC.getValue();
|
||||
}
|
||||
uint32_t end = micros();
|
||||
Serial.print("1000x DAC.getValue():\t");
|
||||
Serial.println(end - start);
|
||||
|
||||
start = micros();
|
||||
for (int i=0; i< 1000; i++)
|
||||
{
|
||||
DAC.setValue(i);
|
||||
}
|
||||
end = micros();
|
||||
Serial.print("1000x DAC.setValue(i):\t");
|
||||
Serial.println(end - start);
|
||||
|
||||
start = micros();
|
||||
for (int i=0; i< 100; i++)
|
||||
{
|
||||
DAC.smooth2Value(i*10, 10);
|
||||
}
|
||||
end = micros();
|
||||
Serial.print("100x DAC.smooth2Value(i*10, 10):\t");
|
||||
Serial.println(end - start);
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user