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
+68
View File
@@ -0,0 +1,68 @@
/**********************************************************************
* Filename : ADCDevice.cpp
* Description : Freenove ADC Module library.
* Author : www.freenove.com
* modification: 2020/03/06
**********************************************************************/
#include "ADCDevice.hpp"
ADCDevice::ADCDevice(){
address = 0;
wiringPiSetup(); //Initialize wiringPi.
}
int ADCDevice::detectI2C(int addr){
_fd = wiringPiI2CSetup (addr);
if (_fd < 0){
printf("Error address : 0x%x \n",addr);
return 0 ;
}
else{
if(wiringPiI2CWrite(_fd,0) < 0){
printf("Not found device in address 0x%x \n",addr);
return 0;
}
else{
printf("Found device in address 0x%x \n",addr);
return 1 ;
}
}
}
int ADCDevice::analogRead(int chn){
printf("Implemented in subclass! \n");
return 0;
}
PCF8591::PCF8591(int addr)
{
address = addr;
cmd = 0x40; //The default command for PCF8591 is 0x40.
wiringPiSetup();
detectI2C(address);
printf("PCF8591 setup successful! \n");
}
int PCF8591::analogRead(int chn){
wiringPiI2CWrite(_fd, cmd+chn);
wiringPiI2CRead(_fd);
wiringPiI2CWrite(_fd, cmd+chn);
return wiringPiI2CRead(_fd);
}
int PCF8591::analogWrite(int value){
return wiringPiI2CWriteReg8(_fd, cmd, value);
}
ADS7830::ADS7830(int addr)
{
address = addr;
cmd = 0x84;
wiringPiSetup();
detectI2C(address);
printf("ADS7830 setup successful! \n");
}
int ADS7830::analogRead(int chn){
wiringPiI2CWrite(_fd, cmd|(((chn<<2 | chn>>1)&0x07)<<4));
return wiringPiI2CRead(_fd);
}
+37
View File
@@ -0,0 +1,37 @@
/**********************************************************************
* Filename : ADCDevice.hpp
* Description : Header file of Freenove ADC Module library.
* Author : www.freenove.com
* modification: 2020/03/06
**********************************************************************/
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
class ADCDevice{
protected:
int _fd;
public:
int address;
int cmd;
ADCDevice();
virtual ~ADCDevice(){};
int detectI2C(int addr);
virtual int analogRead(int chn);
//~ADCDevice(){printf("Destructor ... \n");}
};
class PCF8591:public ADCDevice{
public:
PCF8591(int addr = 0x48); //0x48 is the default i2c address for PCF8591 Module.
int analogRead(int chn); //PCF8591 has 4 ADC input pins, chn:0,1,2,3
int analogWrite(int value); //PCF8591 has DAC function
};
class ADS7830:public ADCDevice{
public:
ADS7830(int addr = 0x4b); //0x4b is the default i2c address for ADS7830 Module.
int analogRead(int chn); //ADS7830 has 8 ADC input pins, chn:0,1,2,3,4,5,6,7
};
+12
View File
@@ -0,0 +1,12 @@
#!/bin/sh
current_file=$0
cd "$(dirname "$current_file")"
g++ -c ADCDevice.cpp
g++ -shared -fPIC -o libADCDevice.so ADCDevice.o
sudo cp ADCDevice.hpp /usr/include/
sudo cp libADCDevice.so /usr/lib
sudo ldconfig
echo "build completed!"
Binary file not shown.