Update ADC module.

update adc module ,add ads7830.
This commit is contained in:
Suhayl
2020-03-11 16:06:06 +08:00
parent 01075b8d39
commit a2ac6a4988
45 changed files with 1399 additions and 803 deletions
@@ -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,7 +1,7 @@
/******************************************************************************* //<>//
* Sketch App_01_1_1_Oscilloscope
* Author Freenove (http://www.freenove.com)
* Date 2016/08/26
* Date 2020/03/10
******************************************************************************
* Brief
* This sketch is used to make an oscilloscope
@@ -15,7 +15,7 @@
*/
import processing.io.*;
PCF8591 pcf = new PCF8591(0x48);
ADCDevice adc = new ADCDevice();
int[] analogs; // Analog data send from serial device
int analogsCount; // Length of analogs[] array
int voltage = 0; // Voltage
@@ -25,6 +25,14 @@ boolean pause = false; // Storage is suspended display
void setup()
{
size(530, 290);
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);
}
background(102);
textAlign(CENTER, CENTER);
textSize(64);
@@ -42,7 +50,7 @@ void setup()
void draw()
{
int analog = pcf.analogRead(0); //serialDevice.requestAnalog();
int analog = adc.analogRead(0); //serialDevice.requestAnalog();
if (analog != -1)
{
// GUI
@@ -91,7 +99,6 @@ void draw()
line(i, analogs[a], i - hMult * width / analogsCount, analogs[a - 1]);
}
}
}
void keyPressed()
@@ -106,8 +113,7 @@ void keyPressed()
hMult = 5;
else if (hMult == 5)
hMult = 10;
}
else if (keyCode == DOWN)
} else if (keyCode == DOWN)
{
if (hMult == 10)
hMult = 5;
@@ -116,8 +122,7 @@ void keyPressed()
else if (hMult == 2)
hMult = 1;
}
}
else
} else
{
if (key == ' ')
{
@@ -127,10 +132,9 @@ void keyPressed()
for (int i = 0; i < analogsCount; i++)
analogs[i] = -1;
}
}
else if (key == '\n' || key == '\r')
} else if (key == '\n' || key == '\r')
{
link("http://www.freenove.com");
}
}
}
}
@@ -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;
}
}
@@ -14,10 +14,18 @@
******************************************************************************
*/
import processing.io.*;
PCF8591 pcf = new PCF8591(0x48);
ADCDevice adc = new ADCDevice();
void setup()
{
size(360, 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);
}
background(102);
textAlign(CENTER, CENTER);
textSize(64);
@@ -29,8 +37,8 @@ void setup()
void draw()
{
int[] analogs = new int[2];
analogs[0] = pcf.analogRead(0);
analogs[1] = pcf.analogRead(1);
analogs[0] = adc.analogRead(0);
analogs[1] = adc.analogRead(1);
if (analogs != null)
{
background(102);
@@ -61,4 +69,4 @@ void keyPressed()
{
link("http://www.freenove.com");
}
}
}
@@ -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;
}
}
@@ -2,7 +2,7 @@
******************************************************************************
* Sketch App_03_1_1_Pong_Game
* Author Freenove (http://www.freenove.com)
* Date 2016/08/26
* Date 2020/03/10
******************************************************************************
* Brief
* This sketch is used to play pong game
@@ -16,7 +16,7 @@
*/
import processing.io.*;
PCF8591 pcf = new PCF8591(0x48);
ADCDevice adc = new ADCDevice();
int winScore = 3;
float acceleration = 0.5;
@@ -30,6 +30,14 @@ int lScore, rScore;
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);
}
background(102);
textAlign(CENTER, CENTER);
textSize(64);
@@ -45,9 +53,9 @@ void setup() {
void draw() {
int[] analogs = new int[2];
analogs[0] = pcf.analogRead(0);
analogs[1] = pcf.analogRead(1);
if (analogs != null)
analogs[0] = adc.analogRead(0);
analogs[1] = adc.analogRead(1);
if (analogs !=null)
{
lPaddle.position.y = analogs[0] * height /255;
rPaddle.position.y = analogs[1] * height /255;
@@ -169,4 +177,4 @@ void keyPressed() {
ball.reset();
gameState = GameState.PLAYING;
}
}
}
@@ -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();
}
}