+ initial version

+ several example sketches
This commit is contained in:
Rob Tillaart 2013-10-15 20:18:00 +02:00
parent 4592f98a04
commit 1896163d06
8 changed files with 411 additions and 0 deletions

103
libraries/AD524X/AD524X.cpp Normal file
View File

@ -0,0 +1,103 @@
//
// FILE: AD524X.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: digital PotentioMeter AD5241 AD5242
// DATE: 2013-10-12
// URL:
//
// Released to the public domain
//
#include "AD524X.h"
#define AS524X_RDAC0 0x00
#define AS524X_RDAC1 0x80
#define AS524X_RESET 0x40
#define AS524X_SHUTDOWN 0x20
#define AS524X_O1_HIGH 0x10
#define AS524X_O2_HIGH 0x08
AD524X::AD524X(uint8_t address)
{
// address: 0x01011xx = 0x2C - 0x2F
_address = address;
_lastValue[0] = _lastValue[1] = 0;
_O1 = 0;
_O2 = 0;
}
uint8_t AD524X::write(uint8_t RDAC, uint8_t value)
{
if (RDAC > 1) return AS524X_ERROR;
uint8_t cmd = (RDAC == 0) ? AS524X_RDAC0 : AS524X_RDAC1;
// remember the O masks
cmd = cmd | _O1 | _O2;
_lastValue[RDAC] = value;
return send(cmd, value);
}
uint8_t AD524X::write(uint8_t RDAC, uint8_t value, uint8_t O1, uint8_t O2)
{
if (RDAC > 1) return AS524X_ERROR;
uint8_t cmd = (RDAC == 0) ? AS524X_RDAC0 : AS524X_RDAC1;
// remember the O masks
cmd = cmd | _O1 | _O2;
_O1 = (O1 == LOW) ? 0 : AS524X_O1_HIGH;
_O2 = (O2 == LOW) ? 0 : AS524X_O2_HIGH;
cmd = cmd | _O1 | _O2;
_lastValue[RDAC] = value;
return send(cmd, value);
}
uint8_t AD524X::setO1(uint8_t v)
{
_O1 = (v == LOW) ? 0 : AS524X_O1_HIGH;
uint8_t cmd = AS524X_RDAC0 | _O1 | _O2;
return send(cmd, _lastValue[0]);
}
uint8_t AD524X::setO2(uint8_t v)
{
_O2 = (v == LOW) ? 0: AS524X_O2_HIGH;
uint8_t cmd = AS524X_RDAC0 | _O1 | _O2;
return send(cmd, _lastValue[0]);
}
uint8_t AD524X::read(uint8_t RDAC)
{
return _lastValue[RDAC];
}
uint8_t AD524X::midScaleReset(uint8_t RDAC)
{
if (RDAC > 1) return AS524X_ERROR;
uint8_t cmd = AS524X_RESET;
if (RDAC == 1) cmd |= AS524X_RDAC1;
cmd = cmd | _O1 | _O2;
_lastValue[RDAC] = 127;
return send(cmd, _lastValue[RDAC]);
}
// TODO read datasheet
// uint8_t AD524X::shutDown()
// {
// uint8_t cmd = AS524X_SHUTDOWN;
// sendCommand(cmd, 0)
// }
//////////////////////////////////////////////////////////
//
// PRIVATE
//
uint8_t AD524X::send(uint8_t cmd, uint8_t value)
{
Wire.beginTransmission(_address);
Wire.write(cmd);
Wire.write(value);
return Wire.endTransmission();
}
// -- END OF FILE --

59
libraries/AD524X/AD524X.h Normal file
View File

@ -0,0 +1,59 @@
//
// FILE: AD524X.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: digital PotentioMeter AD5241 AD5242
// DATE: 2013-10-12
// URL:
//
// Released to the public domain
//
#ifndef AD524X_h
#define AD524X_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#define AD524X_VERSION "0.1.00"
#define AS524X_OK 0
#define AS524X_ERROR -10
class AD524X
{
public:
// address
AD524X(uint8_t);
// rdac value
uint8_t write(uint8_t, uint8_t);
// rdac value O1 O2
uint8_t write(uint8_t, uint8_t, uint8_t, uint8_t);
// O1
uint8_t setO1(uint8_t);
// O2
uint8_t setO2(uint8_t);
// rdac
uint8_t midScaleReset(uint8_t);
//
// uint8_t shutDown();
// rdac
uint8_t read(uint8_t);
private:
uint8_t send(uint8_t, uint8_t); // cmd value
uint8_t _address;
uint8_t _lastValue[2];
uint8_t _O1;
uint8_t _O2;
};
#endif
// -- END OF FILE --

View File

@ -0,0 +1,42 @@
//
// FILE: AD524X_followA0.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: AD524X demo program
// DATE: 2013-10-12
// URL:
//
// Released to the public domain
//
#include "AD524X.h"
#include "Wire.h"
AD524X AD01(0x2C); // AD0 & AD1 == GND
void setup()
{
Serial.begin(115200);
Serial.print("\nStart AD524X_followA0 : ");
Serial.println(AD524X_VERSION);
Wire.begin();
TWBR=72; // 100KHz I2C
}
void loop()
{
int x = analogRead(A0);
Serial.print(x);
Serial.print('\t');
Serial.print(x/4);
int rv = AD01.write(1, x/4);
Serial.print('\t');
Serial.print(rv);
//rv = AD01.write(0, x/4);
Serial.print('\t');
Serial.println(rv);
delay(100);
}

View File

@ -0,0 +1,44 @@
//
// FILE: AD524X_midScaleReset.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: AD524X demo program
// DATE: 2013-10-12
// URL:
//
// Released to the public domain
//
#include "AD524X.h"
#include "Wire.h"
AD524X AD01(0x2C); // AD0 & AD1 == GND
void setup()
{
Serial.begin(115200);
Serial.print("\nStart AD524X_midScaleReset : ");
Serial.println(AD524X_VERSION);
Wire.begin();
TWBR=12; // 400 KHz
}
void loop()
{
Serial.println(255);
AD01.write(1, 255);
delay(1000);
Serial.println("reset");
AD01.midScaleReset(1);
delay(1000);
Serial.println(0);
AD01.write(1, 0);
delay(1000);
Serial.println("reset");
AD01.midScaleReset(1);
delay(1000);
}

View File

@ -0,0 +1,41 @@
//
// FILE: AD524X_read.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: AD524X demo program
// DATE: 2013-10-12
// URL:
//
// Released to the public domain
//
#include "AD524X.h"
#include "Wire.h"
AD524X AD01(0x2C); // AD0 & AD1 == GND
void setup()
{
Serial.begin(115200);
Serial.print("\nStart AD524X_read : ");
Serial.println(AD524X_VERSION);
Wire.begin();
TWBR=12; // 400 KHz
}
void loop()
{
for (int val = 0; val < 255; val++)
{
Serial.print(val);
AD01.write(1, val);
delay(100);
int x = AD01.read(1);
Serial.print('\t');
Serial.println(x);
delay(100);
}
delay(1000);
}

View File

@ -0,0 +1,36 @@
//
// FILE: AD524X_sawtooth.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: AD524X demo program
// DATE: 2013-10-12
// URL:
//
// Released to the public domain
//
#include "AD524X.h"
#include "Wire.h"
AD524X AD01(0x2C); // AD0 & AD1 == GND
void setup()
{
Serial.begin(115200);
Serial.print("\nStart AD524X_sawtooth : ");
Serial.println(AD524X_VERSION);
Wire.begin();
TWBR=72; // 100KHz
}
void loop()
{
for (int i=0; i<255; i++)
{
AD01.write(1, i);
Serial.println(i);
delay(20);
}
}

View File

@ -0,0 +1,40 @@
//
// FILE: AD524X_setO.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: AD524X demo program
// DATE: 2013-10-12
// URL:
//
// Released to the public domain
//
#include "AD524X.h"
#include "Wire.h"
AD524X AD01(0x2C); // AD0 & AD1 == GND
void setup()
{
Serial.begin(115200);
Serial.print("\nStart AD524X_setO : ");
Serial.println(AD524X_VERSION);
Wire.begin();
TWBR=72; // 100KHz
}
void loop()
{
int x = 10+analogRead(A0);
AD01.setO1(LOW);
AD01.setO2(HIGH);
delay(x);
x = 10+analogRead(A0);
AD01.setO1(HIGH);
AD01.setO2(LOW);
delay(x);
}

View File

@ -0,0 +1,46 @@
//
// FILE: AD524X_write.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: AD524X demo program
// DATE: 2013-10-12
// URL:
//
// Released to the public domain
//
#include "AD524X.h"
#include "Wire.h"
AD524X AD01(0x2C); // AD0 & AD1 == GND
void setup()
{
Serial.begin(115200);
Serial.print("\nStart AD524X_write : ");
Serial.println(AD524X_VERSION);
Wire.begin();
TWBR = 12; // 400 KHz
}
void loop()
{
for (int i=0; i<255; i++)
{
AD01.write(1, i);
if (i == 200)
{
AD01.write(1, i, HIGH, LOW);
}
if (i == 0)
{
AD01.write(1, i, LOW, LOW);
}
Serial.println(i);
delay(20);
}
}