Skip to content

Commit

Permalink
Update ADC module.
Browse files Browse the repository at this point in the history
update adc module ,add ads7830.
  • Loading branch information
Suhayl committed Mar 11, 2020
1 parent 01075b8 commit a2ac6a4
Show file tree
Hide file tree
Showing 45 changed files with 1,397 additions and 801 deletions.
35 changes: 0 additions & 35 deletions Code/C_Code/07.1.1_ADC/ADC.c

This file was deleted.

39 changes: 39 additions & 0 deletions Code/C_Code/07.1.1_ADC/ADC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**********************************************************************
* Filename : ADC.cpp
* Description : Use ADC module to read the voltage value of potentiometer.
* Author : www.freenove.com
* modification: 2020/03/06
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>
#include <ADCDevice.hpp>

ADCDevice *adc; // Define an ADC Device class object

int main(void){
adc = new ADCDevice();
printf("Program is starting ... \n");

if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}

while(1){
int adcValue = adc->analogRead(0); //read analog value of A0 pin
float voltage = (float)adcValue / 255.0 * 3.3; // Calculate voltage
printf("ADC value : %d ,\tVoltage : %.2fV\n",adcValue,voltage);
delay(100);
}
return 0;
}
39 changes: 0 additions & 39 deletions Code/C_Code/08.1.1_Softlight/Softlight.c

This file was deleted.

44 changes: 44 additions & 0 deletions Code/C_Code/08.1.1_Softlight/Softlight.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**********************************************************************
* Filename : Softlight.cpp
* Description : Use potentiometer to control LED
* Author : www.freenove.com
* modification: 2020/03/07
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>
#include <softPwm.h>
#include <ADCDevice.hpp>

#define ledPin 0

ADCDevice *adc; // Define an ADC Device class object

int main(void){
adc = new ADCDevice();
printf("Program is starting ... \n");

if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}
wiringPiSetup();
softPwmCreate(ledPin,0,100);
while(1){
int adcValue = adc->analogRead(0); //read analog value of A0 pin
softPwmWrite(ledPin,adcValue*100/255); // Mapping to PWM duty cycle
float voltage = (float)adcValue / 255.0 * 3.3; // Calculate voltage
printf("ADC value : %d ,\tVoltage : %.2fV\n",adcValue,voltage);
delay(30);
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
/**********************************************************************
* Filename : ColorfulSoftlight.c
* Description : Potentiometer control RGBLED
* Filename : Softlight.cpp
* Description : Use potentiometer to control LED
* Author : www.freenove.com
* modification: 2019/12/27
* modification: 2020/03/07
**********************************************************************/
#include <wiringPi.h>
#include <pcf8591.h>
#include <stdio.h>
#include <softPwm.h>

#define address 0x48 //pcf8591 default address
#define pinbase 64 //any number above 64
#define A0 pinbase + 0
#define A1 pinbase + 1
#define A2 pinbase + 2
#define A3 pinbase + 3
#include <ADCDevice.hpp>

#define ledRedPin 3 //define 3 pins for RGBLED
#define ledGreenPin 2
#define ledBluePin 0

ADCDevice *adc; // Define an ADC Device class object

int main(void){
int val_Red,val_Green,val_Blue;

adc = new ADCDevice();
printf("Program is starting ... \n");


if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}
wiringPiSetup();

softPwmCreate(ledRedPin,0,100); //creat 3 PMW output pins for RGBLED
softPwmCreate(ledGreenPin,0,100);
softPwmCreate(ledBluePin,0,100);
pcf8591Setup(pinbase,address); //initialize PCF8591

while(1){
val_Red = analogRead(A0); //read analog value of 3 potentiometers
val_Green = analogRead(A1);
val_Blue = analogRead(A2);
int val_Red = adc->analogRead(0); //read analog value of 3 potentiometers
int val_Green = adc->analogRead(1);
int val_Blue = adc->analogRead(2);
softPwmWrite(ledRedPin,val_Red*100/255); //map the read value of potentiometers into PWM value and output it
softPwmWrite(ledGreenPin,val_Green*100/255);
softPwmWrite(ledBluePin,val_Blue*100/255);
Expand Down
39 changes: 0 additions & 39 deletions Code/C_Code/10.1.1_Nightlamp/Nightlamp.c

This file was deleted.

44 changes: 44 additions & 0 deletions Code/C_Code/10.1.1_Nightlamp/Nightlamp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**********************************************************************
* Filename : Nightlamp.cpp
* Description : Photoresistor control LED
* Author : www.freenove.com
* modification: 2020/03/09
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>
#include <softPwm.h>
#include <ADCDevice.hpp>

#define ledPin 0

ADCDevice *adc; // Define an ADC Device class object

int main(void){
adc = new ADCDevice();
printf("Program is starting ... \n");

if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}
wiringPiSetup();
softPwmCreate(ledPin,0,100);
while(1){
int value = adc->analogRead(0); //read analog value of A0 pin
softPwmWrite(ledPin,value*100/255);
float voltage = (float)value / 255.0 * 3.3; // calculate voltage
printf("ADC value : %d ,\tVoltage : %.2fV\n",value,voltage);
delay(100);
}
return 0;
}
39 changes: 0 additions & 39 deletions Code/C_Code/11.1.1_Thermometer/Thermometer.c

This file was deleted.

43 changes: 43 additions & 0 deletions Code/C_Code/11.1.1_Thermometer/Thermometer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**********************************************************************
* Filename : Thermometer.cpp
* Description : DIY Thermometer
* Author : www.freenove.com
* modification: 2020/03/09
**********************************************************************/
#include <wiringPi.h>
#include <stdio.h>
#include <math.h>
#include <ADCDevice.hpp>

ADCDevice *adc; // Define an ADC Device class object

int main(void){
adc = new ADCDevice();
printf("Program is starting ... \n");

if(adc->detectI2C(0x48)){ // Detect the pcf8591.
delete adc; // Free previously pointed memory
adc = new PCF8591(); // If detected, create an instance of PCF8591.
}
else if(adc->detectI2C(0x4b)){// Detect the ads7830
delete adc; // Free previously pointed memory
adc = new ADS7830(); // If detected, create an instance of ADS7830.
}
else{
printf("No correct I2C address found, \n"
"Please use command 'i2cdetect -y 1' to check the I2C address! \n"
"Program Exit. \n");
return -1;
}
printf("Program is starting ... \n");
while(1){
int adcValue = adc->analogRead(0); //read analog value A0 pin
float voltage = (float)adcValue / 255.0 * 3.3; // calculate voltage
float Rt = 10 * voltage / (3.3 - voltage); //calculate resistance value of thermistor
float tempK = 1/(1/(273.15 + 25) + log(Rt/10)/3950.0); //calculate temperature (Kelvin)
float tempC = tempK -273.15; //calculate temperature (Celsius)
printf("ADC value : %d ,\tVoltage : %.2fV, \tTemperature : %.2fC\n",adcValue,voltage,tempC);
delay(100);
}
return 0;
}
Loading

0 comments on commit a2ac6a4

Please sign in to comment.