Skip to content

Commit

Permalink
Merge branch 'for_experiment'
Browse files Browse the repository at this point in the history
  • Loading branch information
iPAS committed Oct 21, 2020
2 parents 70147f4 + da51738 commit 67cba39
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 50 deletions.
34 changes: 17 additions & 17 deletions ADS1231/ADS1231.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ ADS1231::ADS1231_status_t ADS1231::ADS1231_PowerDown ( void )
*
* @details It reads the raw data from the device.
*
* @param[in] myAverage: How many measurement we have to get and deliver the average.
* @param[in] num_avg: How many measurement we have to get and deliver the average.
*
* @param[out] myNewRawData: The new value from the device.
*
Expand All @@ -124,7 +124,7 @@ ADS1231::ADS1231_status_t ADS1231::ADS1231_PowerDown ( void )
* @pre NaN.
* @warning NaN.
*/
ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadRawData ( Vector_count_t* myNewRawData, uint32_t myAverage )
ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadRawData ( Vector_count_t* myNewRawData, uint8_t num_avg )
{
uint32_t i = 0; // Counter and timeout variable
uint32_t ii = 0; // Counter variable
Expand All @@ -134,8 +134,8 @@ ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadRawData ( Vector_count_t* myN

myNewRawData->myRawValue = 0; // Reset variable at the beginning

// Start collecting the new measurement as many as myAverage
for ( ii = 0; ii < myAverage; ii++ ) {
// Start collecting the new measurement as many as num_avg
for ( ii = 0; ii < num_avg; ii++ ) {
// Reset the value
myAuxData = 0;

Expand Down Expand Up @@ -174,7 +174,7 @@ ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadRawData ( Vector_count_t* myN
myNewRawData->myRawValue += myAuxData;
}

myNewRawData->myRawValue /= ( float )myAverage;
myNewRawData->myRawValue /= ( float )num_avg;



Expand All @@ -187,11 +187,11 @@ ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadRawData ( Vector_count_t* myN


/**
* @brief ADS1231_ReadData_WithCalibratedMass ( Vector_count_t* myNewRawData, uint32_t myAverage )
* @brief ADS1231_ReadData_WithCalibratedMass ( Vector_count_t* myNewRawData, uint8_t num_avg )
*
* @details It reads data with a calibrated mass on the load cell.
*
* @param[in] myAverage: How many data to read.
* @param[in] num_avg: How many data to read.
*
* @param[out] myNewRawData: myRawValue_WithCalibratedMass ( ADC code taken with calibrated mass ).
*
Expand All @@ -205,12 +205,12 @@ ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadRawData ( Vector_count_t* myN
* @pre NaN.
* @warning NaN.
*/
ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadData_WithCalibratedMass ( Vector_count_t* myNewRawData, uint32_t myAverage )
ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadData_WithCalibratedMass(Vector_count_t* myNewRawData, uint8_t num_avg)
{
ADS1231_status_t aux;

// Perform a new bunch of readings
aux = ADS1231_ReadRawData ( myNewRawData, myAverage );
aux = ADS1231_ReadRawData ( myNewRawData, num_avg );


// Update the value with a calibrated mass
Expand All @@ -227,11 +227,11 @@ ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadData_WithCalibratedMass ( Vect


/**
* @brief ADS1231_ReadData_WithoutMass ( Vector_count_t* myNewRawData, uint32_t myAverage )
* @brief ADS1231_ReadData_WithoutMass ( Vector_count_t* myNewRawData, uint8_t num_avg )
*
* @details It reads data without any mass on the load cell.
*
* @param[in] myAverage: How many data to read.
* @param[in] num_avg: How many data to read.
*
* @param[out] myNewRawData: myRawValue_WithoutCalibratedMass ( ADC code taken without any mass ).
*
Expand All @@ -245,12 +245,12 @@ ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadData_WithCalibratedMass ( Vect
* @pre NaN.
* @warning NaN.
*/
ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadData_WithoutMass ( Vector_count_t* myNewRawData, uint32_t myAverage )
ADS1231::ADS1231_status_t ADS1231::ADS1231_ReadData_WithoutMass ( Vector_count_t* myNewRawData, uint8_t num_avg )
{
ADS1231_status_t aux;

// Perform a new bunch of readings
aux = ADS1231_ReadRawData ( myNewRawData, myAverage );
aux = ADS1231_ReadRawData ( myNewRawData, num_avg );


// Update the value without any mass
Expand Down Expand Up @@ -361,7 +361,7 @@ ADS1231::Vector_mass_t ADS1231::ADS1231_CalculateMass ( Vector_count_t* myNewRa
* @param[in] myCalibratedMass: A known value for the calibrated mass when myRawValue_WithCalibratedMass was
* calculated.
* @param[in] myScaleCalibratedMass: The range of the calibrated mass ( kg, g, mg or ug ).
* @param[in] myTime: How long the auto-set lasts.
* @param[in] num_avg: How long the auto-set lasts.
*
* @param[out] myNewRawData: myRawValue_TareWeight ( ADC code taken without any mass ).
*
Expand All @@ -375,7 +375,7 @@ ADS1231::Vector_mass_t ADS1231::ADS1231_CalculateMass ( Vector_count_t* myNewRa
* @pre NaN.
* @warning NaN.
*/
ADS1231::ADS1231_status_t ADS1231::ADS1231_SetAutoTare ( float myCalibratedMass, ADS1231_scale_t myScaleCalibratedMass, Vector_count_t* myNewRawData, float myTime )
ADS1231::ADS1231_status_t ADS1231::ADS1231_SetAutoTare(float myCalibratedMass, ADS1231_scale_t myScaleCalibratedMass, Vector_count_t* myNewRawData, uint8_t num_avg)
{
ADS1231_status_t aux;
Vector_mass_t myCalculatedMass;
Expand All @@ -384,13 +384,13 @@ ADS1231::ADS1231_status_t ADS1231::ADS1231_SetAutoTare ( float myCalibratedMa


// Perform a new bunch of readings every 1 second
for ( i = 0; i < myTime; i++ ) {
for ( i = 0; i < num_avg; i++ ) {
aux = ADS1231_ReadRawData ( myNewRawData, 10 );
myAuxData += myNewRawData->myRawValue;
wait(1);
}

myNewRawData->myRawValue = ( float )( myAuxData / myTime );
myNewRawData->myRawValue = ( float )( myAuxData / num_avg );

// Turn it into mass
myCalculatedMass = ADS1231_CalculateMass ( myNewRawData, myCalibratedMass, myScaleCalibratedMass );
Expand Down
8 changes: 4 additions & 4 deletions ADS1231/ADS1231.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,19 @@ class ADS1231

/** It reads raw data from the device.
*/
ADS1231_status_t ADS1231_ReadRawData ( Vector_count_t* myNewRawData, uint32_t myAverage );
ADS1231_status_t ADS1231_ReadRawData ( Vector_count_t* myNewRawData, uint8_t num_avg );

/** It reads raw data with an user-specified calibrated mass.
*/
ADS1231_status_t ADS1231_ReadData_WithCalibratedMass ( Vector_count_t* myNewRawData, uint32_t myAverage );
ADS1231_status_t ADS1231_ReadData_WithCalibratedMass ( Vector_count_t* myNewRawData, uint8_t num_avg );

/** It reads raw data without any mass.
*/
ADS1231_status_t ADS1231_ReadData_WithoutMass ( Vector_count_t* myNewRawData, uint32_t myAverage );
ADS1231_status_t ADS1231_ReadData_WithoutMass ( Vector_count_t* myNewRawData, uint8_t num_avg );

/** It reads raw data without any mass after the system is calibrated.
*/
ADS1231_status_t ADS1231_SetAutoTare ( float myCalibratedMass, ADS1231_scale_t myScaleCalibratedMass, Vector_count_t* myNewRawData, float myTime );
ADS1231_status_t ADS1231_SetAutoTare ( float myCalibratedMass, ADS1231_scale_t myScaleCalibratedMass, Vector_count_t* myNewRawData, uint8_t num_avg );

/** It sets a tare weight manually.
*/
Expand Down
124 changes: 95 additions & 29 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@
#include "ADS1220.h"


/******************************************************************************
* Definitions
******************************************************************************/
// #define __HX711__
#define __ADS1232__
// #define __ADS1220__

#define TEST_AMOUNT 20
#define LSB_SIZE(PGA, VREF) ((VREF/PGA) / (((long int)1<<23)))

// Blinking rate in milliseconds
#define BLINKING_RATE_MS 1000
#define BLINKING_RATE_MS 1000
DigitalOut led(LED3); // Initialise the digital pin LED1 as an output

volatile static uint16_t sample_count = 0;

// SSD1306 adapted from Adafruit's library
// https://os.mbed.com/users/nkhorman/code/Adafruit_GFX/
Expand All @@ -35,14 +45,20 @@ Adafruit_SSD1306_I2c gOled2(gI2C, P_5, SSD_I2C_ADDRESS, 64, 128);
// https://os.mbed.com/docs/mbed-os/v5.15/apis/serial.html
Serial uart(UART_TX, UART_RX, NULL, 115200);


/******************************************************************************
* Definitions
******************************************************************************/
// #define __HX711__
// #define __ADS1232__
#define __ADS1220__
#define LSB_SIZE(PGA, VREF) ((VREF/PGA) / (((long int)1<<23)))
// #define UART_INTR
#ifdef UART_INTR
static void on_uart_receive(void)
{
char buf[10];
while (uart.readable())
{
// __disable_irq();
uart.read(buf, 10);
// __enable_irq();
}
sample_count = 0;
}
#endif


/******************************************************************************
Expand Down Expand Up @@ -105,18 +121,26 @@ struct
{
ADS1231::ADS1231_status_t status;
ADS1231::Vector_count_t count;
uint8_t num_avg;
ADS1231::Vector_mass_t calculated_mass;
ADS1231::Vector_voltage_t calculated_volt;
} ads1232_sample;

void ads1232_read(void) {
ads1232_sample.status = loadcell_ads1232.ADS1231_ReadRawData(&ads1232_sample.count, 4);
ads1232_sample.status = loadcell_ads1232.ADS1231_ReadRawData(&ads1232_sample.count, ads1232_sample.num_avg);
ads1232_sample.calculated_volt = loadcell_ads1232.ADS1231_CalculateVoltage(&ads1232_sample.count, ADS1232_VREF);
ads1232_sample.calculated_mass = loadcell_ads1232.ADS1231_CalculateMass(&ads1232_sample.count, ADS1232_CAL_MASS, ADS1231::ADS1231_SCALE_g);
}

void ads1232_init(void)
{
gOled2.clearDisplay();
gOled2.printf("Calibrating...\r\n");
gOled2.display();


ads1232_sample.num_avg = 1;
uint8_t num_avg_cal = 4;
ADS1231::ADS1231_status_t sts;

// Reset and wake the ADS1232 up
Expand Down Expand Up @@ -144,39 +168,45 @@ void ads1232_init(void)

int i;
uart.printf("ADS1232: please remove the calibrated mass before calibration ...");
gOled2.printf("Remove mass...\r\n");
gOled2.display();
for (i = 5; i > 0; i--)
{
wait(1);
uart.printf(" %d ", i);
uart.printf("%d ", i);
}
wait(1);
uart.printf("[in progress].\r\n");
uart.printf("[WIP].\r\n");

loadcell_ads1232.ADS1231_ReadData_WithoutMass(&ads1232_sample.count, 4);
loadcell_ads1232.ADS1231_ReadData_WithoutMass(&ads1232_sample.count, num_avg_cal);
uart.printf("ADS1232: .myRawValue_WithoutCalibratedMass > %f\r\n", ads1232_sample.count.myRawValue_WithoutCalibratedMass);

uart.printf("ADS1232: please put a calibrated mass %.1fg on the scale ...", ADS1232_CAL_MASS * 1000);
gOled2.printf("Put mass...\r\n");
gOled2.display();
for (i = 10; i > 0; i--)
{
wait(1);
uart.printf(" %d ", i);
uart.printf("%d ", i);
}
wait(1);
uart.printf("[in progress].\r\n");
uart.printf("[WIP].\r\n");

loadcell_ads1232.ADS1231_ReadData_WithCalibratedMass(&ads1232_sample.count, 4);
loadcell_ads1232.ADS1231_ReadData_WithCalibratedMass(&ads1232_sample.count, num_avg_cal);
uart.printf("ADS1232: .myRawValue_WithCalibratedMass > %f\r\n", ads1232_sample.count.myRawValue_WithCalibratedMass);

uart.printf("ADS1232: please remove the calibrated mass before taring ...");
gOled2.printf("Remove mass again...\r\n");
gOled2.display();
for (i = 5; i > 0; i--)
{
wait(1);
uart.printf(" %d ", i);
uart.printf("%d ", i);
}
wait(1);
uart.printf("[in progress].\r\n");
uart.printf("[WIP].\r\n");

loadcell_ads1232.ADS1231_SetAutoTare(ADS1232_CAL_MASS, ADS1231::ADS1231_SCALE_g, &ads1232_sample.count, 4);
loadcell_ads1232.ADS1231_SetAutoTare(ADS1232_CAL_MASS, ADS1231::ADS1231_SCALE_g, &ads1232_sample.count, num_avg_cal);
uart.printf("ADS1232: .myRawValue_TareWeight > %f\r\n", ads1232_sample.count.myRawValue_TareWeight);

// ads1232_sample.count.myRawValue_WithoutCalibratedMass = 8385827;
Expand Down Expand Up @@ -251,11 +281,17 @@ void ads1220_read(void)
******************************************************************************/
int main()
{
uint16_t x = 0;
ThisThread::sleep_for(1000); // Delay for showing splash

gOled2.clearDisplay();
gOled2.printf("%ux%u OLED Display\r\n", gOled2.width(), gOled2.height());
gOled2.display();
ThisThread::sleep_for(1000);

#ifdef UART_INTR
uart.attach(&on_uart_receive, Serial::RxIrq); // Bind with on-receiving callback function.
#endif


#ifdef __HX711__
hx711_init();
Expand All @@ -268,51 +304,81 @@ int main()
#endif


uart.printf("----------------------------------------\r\n");

while (true) {
led = !led;
// led = 1;
ThisThread::sleep_for(BLINKING_RATE_MS);


if (sample_count > TEST_AMOUNT+1)
{
#if ! defined(UART_INTR) // Clear 'sample_count' without using rx interrupt
if (uart.readable())
{
while (uart.readable())
{
ThisThread::sleep_for(1);
uart.getc();
}
sample_count = 0;
uart.printf("----------------------------------------\r\n");
}
#endif
continue;
}
else
if (++sample_count > TEST_AMOUNT)
{
gOled2.printf("\r\n -- Finish --\r\n");
gOled2.display();
sample_count++;
continue;
}


gOled2.clearDisplay();
gOled2.printf("%u\r", x);
gOled2.display();
gOled2.setTextCursor(0, 0);


#ifdef __HX711__
uart.printf("HX711: raw=%ld volt=%.3fmV mass=%.3fg\r\n",
uart.printf("[%d] HX711: raw=%ld volt=%.3fmV mass=%.3fg\r\n", sample_count,
hx711_sample.raw,
hx711_sample.volt * 1000,
hx711_sample.mass
);
gOled2.printf("%u:HX711 %.2fg\r\n", sample_count, hx711_sample.mass);
#endif


#ifdef __ADS1232__
if (ads1232_sample.status == ADS1231::ADS1231_status_t::ADS1231_FAILURE)
uart.printf("ADS1232 fail on readRaw()\r\n");
else
uart.printf("ADS1232: raw=%ld volt=%0.3fmV mass=%.3fg\r\n",
{
uart.printf("[%d] ADS1232: raw=%ld volt=%.3fmV mass=%.3fg\r\n", sample_count,
ads1232_sample.count.myRawValue,
ads1232_sample.calculated_volt.myVoltage * 1000,
ads1232_sample.calculated_mass.myMass // ADS1231::ADS1231_SCALE_g
);
gOled2.printf("%u:1232 %.2fg\r\n", sample_count, ads1232_sample.calculated_mass.myMass);
}
#endif


#ifdef __ADS1220__
if (ads1220_sample.available)
{
ads1220_read();
uart.printf("ADS1220: raw=%ld volt=%fmV mass=%.3fg\r\n",
uart.printf("[%d] ADS1220: raw=%ld volt=%.3fmV mass=%.3fg\r\n", sample_count,
ads1220_sample.raw,
ads1220_sample.volt * 1000,
ads1220_sample.mass
);
gOled2.printf("%u:1220 %.2fg\r\n", sample_count, ads1220_sample.mass);
}
#endif


x++;
gOled2.display();
}
}

0 comments on commit 67cba39

Please sign in to comment.