Update ADC module.
update adc module ,add ads7830.
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
/*****************************************************
|
||||
* Filename : ADCDevice
|
||||
* Description : class ADCDevice
|
||||
* auther : www.freenove.com
|
||||
* modification: 2020/03/09
|
||||
*****************************************************/
|
||||
|
||||
class ADCDevice {
|
||||
public int address = 0;
|
||||
public int cmd = 0;
|
||||
public I2C i2c;
|
||||
public ADCDevice() {
|
||||
i2c = new I2C(I2C.list()[0]);
|
||||
}
|
||||
|
||||
public boolean detectI2C(int addr) {
|
||||
i2c.beginTransmission(addr);
|
||||
i2c.write(0);
|
||||
try {
|
||||
i2c.endTransmission();
|
||||
System.out.printf("Found device in address 0x%x\n", addr);
|
||||
return true;
|
||||
}
|
||||
catch(Exception e) {
|
||||
System.out.printf("Not found device in address 0x%x\n", addr);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int analogRead(int chn) {
|
||||
println("Implemented in subclass! \n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
class PCF8591 extends ADCDevice {
|
||||
//constructor,Parameters for the PCF8591 I2C address
|
||||
public PCF8591(int addr) {
|
||||
address = addr;
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
public int analogRead(int chn) {
|
||||
int result = 0;
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(0x40 | chn);
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40 | chn);
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
//Read the ADC value of all channels
|
||||
public byte[] analogRead() {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x44);
|
||||
i2c.endTransmission();
|
||||
byte[] in = i2c.read(4);
|
||||
return in;
|
||||
}
|
||||
//Write the DACvalue
|
||||
public void analogWrite(int data) {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40);
|
||||
i2c.write(data);
|
||||
i2c.endTransmission();
|
||||
}
|
||||
}
|
||||
class ADS7830 extends ADCDevice {
|
||||
//constructor,Parameters for the ADS7830 I2C address
|
||||
public ADS7830(int addr) {
|
||||
address = addr;
|
||||
cmd = 0x84;
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
public int analogRead(int chn) {
|
||||
int result = 0;
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(cmd|(((chn<<2 | chn>>1)&0x07)<<4));
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+19
-14
@@ -1,28 +1,33 @@
|
||||
/*****************************************************
|
||||
* Filename : Sketch_06_1_1_PCF8591
|
||||
* Description : ADC and DAC of PCF8591
|
||||
* Filename : Sketch_06_1_1_ADC
|
||||
* Description : Making a voltmeter with Freenove ADC module.
|
||||
* auther : www.freenove.com
|
||||
* modification: 2016/08/20
|
||||
* modification: 2020/03/09
|
||||
*****************************************************/
|
||||
import processing.io.*;
|
||||
//Create a object of class PCF8591
|
||||
PCF8591 pcf = new PCF8591(0x48);
|
||||
//Create a object of class ADCDevice
|
||||
ADCDevice adc = new ADCDevice();
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
if (adc.detectI2C(0x48)) {
|
||||
adc = new PCF8591(0x48);
|
||||
} else if (adc.detectI2C(0x4b)) {
|
||||
adc = new ADS7830(0x4b);
|
||||
} else {
|
||||
println("Not found ADC Module!");
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
void draw() {
|
||||
int adc = pcf.analogRead(0); //Read the ADC value of channel 0
|
||||
float volt = adc*3.3/255.0; //calculate the voltage
|
||||
pcf.analogWrite(adc); //Write the DAC
|
||||
int adcValue = adc.analogRead(0); //Read the ADC value of channel 0
|
||||
float volt = adcValue*3.3/255.0; //calculate the voltage
|
||||
background(255);
|
||||
titleAndSiteInfo();
|
||||
|
||||
|
||||
fill(0);
|
||||
textAlign(CENTER); //set the text centered
|
||||
textSize(30);
|
||||
text("ADC: "+nf(adc, 3, 0), width / 2, height/2+50);
|
||||
textSize(30);
|
||||
text("DAC: "+nf(adc, 3, 0), width / 2, height/2+100);
|
||||
text("ADC: "+nf(adcValue, 3, 0), width / 2, height/2+50);
|
||||
textSize(40); //set text size
|
||||
text("Voltage: "+nf(volt, 0, 2)+"V", width / 2, height/2); //
|
||||
}
|
||||
@@ -30,7 +35,7 @@ void titleAndSiteInfo() {
|
||||
fill(0);
|
||||
textAlign(CENTER); //set the text centered
|
||||
textSize(40); //set text size
|
||||
text("ADC & DAC", width / 2, 40); //title
|
||||
text("ADC", width / 2, 40); //title
|
||||
textSize(16);
|
||||
text("www.freenove.com", width / 2, height - 20); //site
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
/*****************************************************
|
||||
* Filename : PCF8591
|
||||
* Description : class PCF8591,DAC and ADC
|
||||
* auther : www.freenove.com
|
||||
* modification: 2016/08/20
|
||||
*****************************************************/
|
||||
|
||||
class PCF8591 {
|
||||
private int address;
|
||||
private I2C i2c;
|
||||
//constructor,Parameters for the PCF8591 I2C address
|
||||
public PCF8591(int addr) {
|
||||
address = addr;
|
||||
i2c = new I2C(I2C.list()[0]);
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
public int analogRead(int chn) {
|
||||
int result = 0;
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(0x40 | chn);
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
//Read the ADC value of all channels
|
||||
public byte[] analogRead() {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x44);
|
||||
i2c.endTransmission();
|
||||
byte[] in = i2c.read(4);
|
||||
return in;
|
||||
}
|
||||
//Write the DACvalue
|
||||
public void analogWrite(int data) {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40);
|
||||
i2c.write(data);
|
||||
i2c.endTransmission();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/*****************************************************
|
||||
* Filename : ADCDevice
|
||||
* Description : class ADCDevice
|
||||
* auther : www.freenove.com
|
||||
* modification: 2020/03/09
|
||||
*****************************************************/
|
||||
|
||||
class ADCDevice {
|
||||
public int address = 0;
|
||||
public int cmd = 0;
|
||||
public I2C i2c;
|
||||
public ADCDevice() {
|
||||
i2c = new I2C(I2C.list()[0]);
|
||||
}
|
||||
|
||||
public boolean detectI2C(int addr) {
|
||||
i2c.beginTransmission(addr);
|
||||
i2c.write(0);
|
||||
try {
|
||||
i2c.endTransmission();
|
||||
System.out.printf("Found device in address 0x%x\n", addr);
|
||||
return true;
|
||||
}
|
||||
catch(Exception e) {
|
||||
System.out.printf("Not found device in address 0x%x\n", addr);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int analogRead(int chn) {
|
||||
println("Implemented in subclass! \n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
class PCF8591 extends ADCDevice {
|
||||
//constructor,Parameters for the PCF8591 I2C address
|
||||
public PCF8591(int addr) {
|
||||
address = addr;
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
public int analogRead(int chn) {
|
||||
int result = 0;
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(0x40 | chn);
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40 | chn);
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
//Read the ADC value of all channels
|
||||
public byte[] analogRead() {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x44);
|
||||
i2c.endTransmission();
|
||||
byte[] in = i2c.read(4);
|
||||
return in;
|
||||
}
|
||||
//Write the DACvalue
|
||||
public void analogWrite(int data) {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40);
|
||||
i2c.write(data);
|
||||
i2c.endTransmission();
|
||||
}
|
||||
}
|
||||
class ADS7830 extends ADCDevice {
|
||||
//constructor,Parameters for the ADS7830 I2C address
|
||||
public ADS7830(int addr) {
|
||||
address = addr;
|
||||
cmd = 0x84;
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
public int analogRead(int chn) {
|
||||
int result = 0;
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(cmd|(((chn<<2 | chn>>1)&0x07)<<4));
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
/*****************************************************
|
||||
* Filename : PCF8591
|
||||
* Description : class PCF8591,DAC and ADC
|
||||
* auther : www.freenove.com
|
||||
* modification: 2016/08/20
|
||||
*****************************************************/
|
||||
|
||||
class PCF8591 {
|
||||
private int address;
|
||||
private I2C i2c;
|
||||
//constructor,Parameters for the PCF8591 I2C address
|
||||
public PCF8591(int addr) {
|
||||
address = addr;
|
||||
i2c = new I2C(I2C.list()[0]);
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
public int analogRead(int chn) {
|
||||
int result = 0;
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(0x40 | chn);
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
//Read the ADC value of all channels
|
||||
public byte[] analogRead() {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x44);
|
||||
i2c.endTransmission();
|
||||
byte[] in = i2c.read(4);
|
||||
return in;
|
||||
}
|
||||
//Write the DACvalue
|
||||
public void analogWrite(int data) {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40);
|
||||
i2c.write(data);
|
||||
i2c.endTransmission();
|
||||
}
|
||||
}
|
||||
@@ -2,21 +2,29 @@
|
||||
* Filename : Sketch_07_1_1_SoftLight
|
||||
* Description : control the brightness of led through a potentiometer
|
||||
* auther : www.freenove.com
|
||||
* modification: 2016/08/20
|
||||
* modification: 2020/03/09
|
||||
*****************************************************/
|
||||
import processing.io.*;
|
||||
|
||||
int ledPin = 17; //led
|
||||
//Create a object of class PCF8591
|
||||
PCF8591 pcf = new PCF8591(0x48);
|
||||
//Create a object of class ADCDevice
|
||||
ADCDevice adc = new ADCDevice();
|
||||
SOFTPWM p = new SOFTPWM(ledPin, 0, 100);
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
if (adc.detectI2C(0x48)) {
|
||||
adc = new PCF8591(0x48);
|
||||
} else if (adc.detectI2C(0x4b)) {
|
||||
adc = new ADS7830(0x4b);
|
||||
} else {
|
||||
println("Not found ADC Module!");
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
void draw() {
|
||||
int adc = pcf.analogRead(0); //Read the ADC value of channel 0
|
||||
float volt = adc*3.3/255.0; //calculate the voltage
|
||||
float dt = adc/255.0;
|
||||
int adcValue = adc.analogRead(0); //Read the ADC value of channel 0
|
||||
float volt = adcValue*3.3/255.0; //calculate the voltage
|
||||
float dt = adcValue/255.0;
|
||||
p.softPwmWrite((int)(dt*100)); //output the pwm
|
||||
background(255);
|
||||
titleAndSiteInfo();
|
||||
@@ -28,7 +36,7 @@ void draw() {
|
||||
fill(0);
|
||||
textAlign(CENTER); //set the text centered
|
||||
textSize(30);
|
||||
text("ADC: "+nf(adc, 3, 0), width / 2, height/2+130);
|
||||
text("ADC: "+nf(adcValue, 3, 0), width / 2, height/2+130);
|
||||
text("Voltage: "+nf(volt, 0, 2)+"V", width / 2, height/2+100); //
|
||||
}
|
||||
void titleAndSiteInfo() {
|
||||
@@ -38,4 +46,4 @@ void titleAndSiteInfo() {
|
||||
text("SoftLight", width / 2, 40); //title
|
||||
textSize(16);
|
||||
text("www.freenove.com", width / 2, height - 20); //site
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/*****************************************************
|
||||
* Filename : ADCDevice
|
||||
* Description : class ADCDevice
|
||||
* auther : www.freenove.com
|
||||
* modification: 2020/03/09
|
||||
*****************************************************/
|
||||
|
||||
class ADCDevice {
|
||||
public int address = 0;
|
||||
public int cmd = 0;
|
||||
public I2C i2c;
|
||||
public ADCDevice() {
|
||||
i2c = new I2C(I2C.list()[0]);
|
||||
}
|
||||
|
||||
public boolean detectI2C(int addr) {
|
||||
i2c.beginTransmission(addr);
|
||||
i2c.write(0);
|
||||
try {
|
||||
i2c.endTransmission();
|
||||
System.out.printf("Found device in address 0x%x\n", addr);
|
||||
return true;
|
||||
}
|
||||
catch(Exception e) {
|
||||
System.out.printf("Not found device in address 0x%x\n", addr);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int analogRead(int chn) {
|
||||
println("Implemented in subclass! \n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
class PCF8591 extends ADCDevice {
|
||||
//constructor,Parameters for the PCF8591 I2C address
|
||||
public PCF8591(int addr) {
|
||||
address = addr;
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
public int analogRead(int chn) {
|
||||
int result = 0;
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(0x40 | chn);
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40 | chn);
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
//Read the ADC value of all channels
|
||||
public byte[] analogRead() {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x44);
|
||||
i2c.endTransmission();
|
||||
byte[] in = i2c.read(4);
|
||||
return in;
|
||||
}
|
||||
//Write the DACvalue
|
||||
public void analogWrite(int data) {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40);
|
||||
i2c.write(data);
|
||||
i2c.endTransmission();
|
||||
}
|
||||
}
|
||||
class ADS7830 extends ADCDevice {
|
||||
//constructor,Parameters for the ADS7830 I2C address
|
||||
public ADS7830(int addr) {
|
||||
address = addr;
|
||||
cmd = 0x84;
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
public int analogRead(int chn) {
|
||||
int result = 0;
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(cmd|(((chn<<2 | chn>>1)&0x07)<<4));
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
/*****************************************************
|
||||
* Filename : PCF8591
|
||||
* Description : class PCF8591,DAC and ADC
|
||||
* auther : www.freenove.com
|
||||
* modification: 2016/08/20
|
||||
*****************************************************/
|
||||
|
||||
class PCF8591 {
|
||||
private int address;
|
||||
private I2C i2c;
|
||||
//constructor,Parameters for the PCF8591 I2C address
|
||||
public PCF8591(int addr) {
|
||||
address = addr;
|
||||
i2c = new I2C(I2C.list()[0]);
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
public int analogRead(int chn) {
|
||||
int result = 0;
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(0x40 | chn);
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
//Read the ADC value of all channels
|
||||
public byte[] analogRead() {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x44);
|
||||
i2c.endTransmission();
|
||||
byte[] in = i2c.read(4);
|
||||
return in;
|
||||
}
|
||||
//Write the DACvalue
|
||||
public void analogWrite(int data) {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40);
|
||||
i2c.write(data);
|
||||
i2c.endTransmission();
|
||||
}
|
||||
}
|
||||
@@ -2,17 +2,25 @@
|
||||
* Filename : Sketch_08_1_1_Thermometer
|
||||
* Description : A DIY Thermometer
|
||||
* auther : www.freenove.com
|
||||
* modification: 2016/08/21
|
||||
* modification: 2020/03/09
|
||||
*****************************************************/
|
||||
import processing.io.*;
|
||||
//Create a object of class PCF8591
|
||||
PCF8591 pcf = new PCF8591(0x48);
|
||||
//Create a object of class ADCDevice
|
||||
ADCDevice adc = new ADCDevice();
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
if (adc.detectI2C(0x48)) {
|
||||
adc = new PCF8591(0x48);
|
||||
} else if (adc.detectI2C(0x4b)) {
|
||||
adc = new ADS7830(0x4b);
|
||||
} else {
|
||||
println("Not found ADC Module!");
|
||||
System.exit(-1);
|
||||
}
|
||||
}
|
||||
void draw() {
|
||||
int adc = pcf.analogRead(0); //Read the ADC value of channel 0
|
||||
float volt = adc*3.3/255.0; //calculate the voltage
|
||||
int adcValue = adc.analogRead(0); //Read the ADC value of channel 0
|
||||
float volt = adcValue*3.3/255.0; //calculate the voltage
|
||||
float tempK,tempC,Rt; //
|
||||
Rt = 10*volt / (3.3-volt); //calculate the resistance value of thermistor
|
||||
tempK = 1/(1/(273.15+25) + log(Rt/10)/3950); //calaulate temperature(Kelvin)
|
||||
@@ -24,7 +32,7 @@ void draw() {
|
||||
fill(0);
|
||||
textAlign(CENTER); //set the text centered
|
||||
textSize(30);
|
||||
text("ADC: "+nf(adc, 0, 0), width / 2, height/2+50);
|
||||
text("ADC: "+nf(adcValue, 0, 0), width / 2, height/2+50);
|
||||
textSize(30);
|
||||
text("voltage: "+nf(volt, 0, 2)+"V", width / 2, height/2+100);
|
||||
textSize(40); //set text size
|
||||
@@ -37,4 +45,4 @@ void titleAndSiteInfo() {
|
||||
text("Thermometer", width / 2, 40); //title
|
||||
textSize(16);
|
||||
text("www.freenove.com", width / 2, height - 20); //site
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/*****************************************************
|
||||
* Filename : ADCDevice
|
||||
* Description : class ADCDevice
|
||||
* auther : www.freenove.com
|
||||
* modification: 2020/03/09
|
||||
*****************************************************/
|
||||
|
||||
class ADCDevice {
|
||||
public int address = 0;
|
||||
public int cmd = 0;
|
||||
public I2C i2c;
|
||||
public ADCDevice() {
|
||||
i2c = new I2C(I2C.list()[0]);
|
||||
}
|
||||
|
||||
public boolean detectI2C(int addr) {
|
||||
i2c.beginTransmission(addr);
|
||||
i2c.write(0);
|
||||
try {
|
||||
i2c.endTransmission();
|
||||
System.out.printf("Found device in address 0x%x\n", addr);
|
||||
return true;
|
||||
}
|
||||
catch(Exception e) {
|
||||
System.out.printf("Not found device in address 0x%x\n", addr);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public int analogRead(int chn) {
|
||||
println("Implemented in subclass! \n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
class PCF8591 extends ADCDevice {
|
||||
//constructor,Parameters for the PCF8591 I2C address
|
||||
public PCF8591(int addr) {
|
||||
address = addr;
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
public int analogRead(int chn) {
|
||||
int result = 0;
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(0x40 | chn);
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40 | chn);
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
//Read the ADC value of all channels
|
||||
public byte[] analogRead() {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x44);
|
||||
i2c.endTransmission();
|
||||
byte[] in = i2c.read(4);
|
||||
return in;
|
||||
}
|
||||
//Write the DACvalue
|
||||
public void analogWrite(int data) {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40);
|
||||
i2c.write(data);
|
||||
i2c.endTransmission();
|
||||
}
|
||||
}
|
||||
class ADS7830 extends ADCDevice {
|
||||
//constructor,Parameters for the ADS7830 I2C address
|
||||
public ADS7830(int addr) {
|
||||
address = addr;
|
||||
cmd = 0x84;
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
public int analogRead(int chn) {
|
||||
int result = 0;
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(cmd|(((chn<<2 | chn>>1)&0x07)<<4));
|
||||
try {
|
||||
byte[] in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
}
|
||||
catch(Exception e) {
|
||||
println(e);
|
||||
}
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
/*****************************************************
|
||||
* Filename : PCF8591
|
||||
* Description : class PCF8591,DAC and ADC
|
||||
* auther : www.freenove.com
|
||||
* modification: 2016/08/20
|
||||
*****************************************************/
|
||||
|
||||
class PCF8591 {
|
||||
private int address;
|
||||
private I2C i2c;
|
||||
//constructor,Parameters for the PCF8591 I2C address
|
||||
public PCF8591(int addr) {
|
||||
address = addr;
|
||||
i2c = new I2C(I2C.list()[0]);
|
||||
}
|
||||
//Read the ADC value of one channel
|
||||
//read twice,otherwise errors will occur
|
||||
public int analogRead(int chn) {
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(0x40 | chn);
|
||||
byte[] in = i2c.read(1);
|
||||
int result = in[0]&0xff;
|
||||
i2c.endTransmission();
|
||||
|
||||
i2c.beginTransmission(address);
|
||||
constrain(chn, 0, 3);
|
||||
i2c.write(0x40 | chn);
|
||||
in = i2c.read(1);
|
||||
result = in[0]&0xff;
|
||||
i2c.endTransmission();
|
||||
return result;
|
||||
}
|
||||
//Read the ADC value of all channels
|
||||
public byte[] analogRead() {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x44);
|
||||
//i2c.endTransmission();
|
||||
//i2c.beginTransmission(address);
|
||||
//i2c.write(0x44);
|
||||
byte[] in = i2c.read(4);
|
||||
//i2c.endTransmission();
|
||||
return in;
|
||||
}
|
||||
//Write the DACvalue
|
||||
public void analogWrite(int data) {
|
||||
i2c.beginTransmission(address);
|
||||
i2c.write(0x40);
|
||||
i2c.write(data);
|
||||
i2c.endTransmission();
|
||||
}
|
||||
}
|
||||
@@ -2,15 +2,23 @@
|
||||
* Filename : Sketch_14_1_1_Joystick
|
||||
* Description : Display the position of the joystick
|
||||
* auther : www.freenove.com
|
||||
* modification: 2016/08/29
|
||||
* modification: 2020/03/11
|
||||
*****************************************************/
|
||||
import processing.io.*;
|
||||
//Create a object of class PCF8591
|
||||
PCF8591 pcf = new PCF8591(0x48);
|
||||
//Create a object of class ADCDevice
|
||||
ADCDevice adc = new ADCDevice();
|
||||
int cx, cy, cd, cr; //define the center point,side length & half.
|
||||
|
||||
void setup() {
|
||||
size(640, 360);
|
||||
if (adc.detectI2C(0x48)) {
|
||||
adc = new PCF8591(0x48);
|
||||
} else if (adc.detectI2C(0x4b)) {
|
||||
adc = new ADS7830(0x4b);
|
||||
} else {
|
||||
println("Not found ADC Module!");
|
||||
System.exit(-1);
|
||||
}
|
||||
cx = width/2; //center of the display window
|
||||
cy = height/2; //
|
||||
cd = (int)(height/1.5);
|
||||
@@ -18,15 +26,15 @@ void setup() {
|
||||
}
|
||||
void draw() {
|
||||
int x=0, y=0, z=0;
|
||||
x = pcf.analogRead(2); //read the ADC of joystick
|
||||
y = pcf.analogRead(1); //
|
||||
z = pcf.analogRead(0);
|
||||
x = adc.analogRead(0); //read the ADC of joystick
|
||||
y = adc.analogRead(1); //
|
||||
z = adc.analogRead(2);
|
||||
background(102);
|
||||
titleAndSiteInfo();
|
||||
fill(0);
|
||||
textSize(20);
|
||||
textAlign(LEFT,TOP);
|
||||
text("X:"+x+"\nY:"+y+"\nZ:"+z,10,10);
|
||||
textAlign(LEFT, TOP);
|
||||
text("X:"+x+"\nY:"+y+"\nZ:"+z, 10, 10);
|
||||
|
||||
fill(255); //wall color
|
||||
rect(cx-cr, cy-cr, cd, cd);
|
||||
@@ -40,4 +48,4 @@ void titleAndSiteInfo() {
|
||||
text("Joystick", width / 2, 40); //title
|
||||
textSize(16);
|
||||
text("www.freenove.com", width / 2, height - 20); //site
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user