Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aerowind authored and amotl committed May 27, 2023
0 parents commit f3ec958
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 0 deletions.
104 changes: 104 additions & 0 deletions ADS1231.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "ADS1231.h" //include the declaration for this class

static ADS1231_t ADS1231s[MAX_ADS1231]; // static array of ADS1231 structures
uint8_t ADS1231Count = 0; // the total number of attached ADS1231

//<<constructor>> setup the ADS1231
ADS1231::ADS1231(){
if( ADS1231Count < MAX_ADS1231) {
this->ADS1231Index = ADS1231Count++; // assign a ADS1231 index to this instance
}
else
this->ADS1231Index = INVALID_ADS1231 ; // too many servos
}

//<<destructor>>
ADS1231::~ADS1231(){/*nothing to destruct*/}

//turn on resp. off the ads
void ADS1231::attach( uint8_t sclPin , uint8_t dataPin , uint8_t pwdnPin ) {
if( ADS1231Count < MAX_ADS1231) {
volatile uint8_t *out;
// store Pin numbers
ADS1231s[this->ADS1231Index].sclBit = digitalPinToBitMask(sclPin);
ADS1231s[this->ADS1231Index].sclPort = digitalPinToPort( sclPin);
ADS1231s[this->ADS1231Index].dataBit = digitalPinToBitMask(dataPin);
ADS1231s[this->ADS1231Index].dataPort = digitalPinToPort( dataPin);
ADS1231s[this->ADS1231Index].pwdnBit = digitalPinToBitMask(pwdnPin);
ADS1231s[this->ADS1231Index].pwdnPort = digitalPinToPort( pwdnPin);
// block interrupts
uint8_t oldSREG = SREG;
cli();
// initialize the clock pin as an output:
pinMode( sclPin , OUTPUT );
out = portOutputRegister(ADS1231s[this->ADS1231Index].sclPort);
*out &= ~ADS1231s[this->ADS1231Index].sclBit; // set sclPin low
// initialize the data pin as an input:
pinMode( dataPin , INPUT );
out = portOutputRegister(ADS1231s[this->ADS1231Index].dataPort);
*out &= ~ADS1231s[this->ADS1231Index].dataBit; // set dataPin low
// initialize the power down pin as an output:
pinMode(pwdnPin, OUTPUT);
// reenable interrupts
SREG = oldSREG;
// reset ads1232 with a 10ms pulse on power down pin:
power(LOW);
delay(10);
power(HIGH);
}
}

//turn on resp. off the ads
void ADS1231::power( uint8_t power_mode ){
volatile uint8_t *out;
if( power_mode == LOW ) {
out = portOutputRegister(ADS1231s[this->ADS1231Index].pwdnPort);
*out &= ~ADS1231s[this->ADS1231Index].pwdnBit; // set pwdnPin low
}
else {
out = portOutputRegister(ADS1231s[this->ADS1231Index].pwdnPort);
*out |= ADS1231s[this->ADS1231Index].pwdnBit; // set pwdnPin high
}
}

//check for new data ready
uint8_t ADS1231::check(){
if( *portInputRegister(ADS1231s[this->ADS1231Index].dataPort) & (ADS1231s[this->ADS1231Index].dataBit) ) {
return LOW; }
else {
return HIGH; }
}

//read data from ads1231
long ADS1231::readData(){
long dataValue = 0;
uint8_t inputStatus;
for( uint8_t count = 0 ; count < 25 ; count++ ){
// send pulse to SCL and read the data
sclPulse();
if( *portInputRegister(ADS1231s[this->ADS1231Index].dataPort) & (ADS1231s[this->ADS1231Index].dataBit) ) {
inputStatus = HIGH; }
else {
inputStatus = LOW; }
dataValue |= (long)(inputStatus) << (31 - count);
}
return dataValue;
}

//read data from ads1231 and calibrate
long ADS1231::calibrate(){
long dataValue = 0;
dataValue = readData();
sclPulse();
return dataValue;
}

//send pulse to ads1231
void ADS1231::sclPulse(){
volatile uint8_t *out;
out = portOutputRegister(ADS1231s[this->ADS1231Index].sclPort);
*out |= ADS1231s[this->ADS1231Index].sclBit; // set sclPin high
// delayMicroseconds(1);
out = portOutputRegister(ADS1231s[this->ADS1231Index].sclPort);
*out &= ~ADS1231s[this->ADS1231Index].sclBit; // set sclPin low
}
35 changes: 35 additions & 0 deletions ADS1231.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef ADS1231_H
#define ADS1231_H

#include <inttypes.h>
#include <Arduino.h> //It is very important to remember this! note that if you are using Arduino 1.0 IDE, change "WProgram.h" to "Arduino.h"

#define MAX_ADS1231 6

#define INVALID_ADS1231 255 // flag indicating an invalid servo index

typedef struct {
uint8_t sclBit ; // a pin number from 0 to 63
uint8_t sclPort ; // a pin number from 0 to 63
uint8_t dataBit ; // a pin number from 0 to 63
uint8_t dataPort ; // a pin number from 0 to 63
uint8_t pwdnBit ; // a pin number from 0 to 63
uint8_t pwdnPort ; // a pin number from 0 to 63
} ADS1231_t ;


class ADS1231 {
public:
ADS1231();
~ADS1231();
void attach( uint8_t sclPin , uint8_t dataPin , uint8_t pwdnPin );
void power( uint8_t power_mode );
uint8_t check();
long readData();
long calibrate();
void sclPulse();
private:
uint8_t ADS1231Index; // index into the channel data for this ADS1231
};

#endif
40 changes: 40 additions & 0 deletions examples/MonitorDMS/MonitorDMS.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
MonitorDMS
Reads an analog input using a ADS1231, converts it to strain, and prints the result to the serial monitor.
This example code is in the public domain.
*/

#include <ADS1231.h>

ADS1231 myDMS; // create ADS1231 object to monitor a DMS

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 115200 bits per second:
Serial.begin(115200);
// attach the DMS on pin 15 SCL, 5 Data, 19 PowerDown
myDMS.attach( 15 , 5 , 19 );
//
Serial.println("ADS1231");
Serial.println("time [ms] , sensor , dt sensor read [us]");
}

// the loop routine runs over and over again forever:
void loop() {
// check for data ready
if( myDMS.check() ) {
// get start time of read
unsigned long timeStart = micros();
// read the input on ADS1231:
long sensorValue = myDMS.readData();
// get final time of read
unsigned long timeEnd = micros();
// print out the time, the value read and time required for read:
Serial.print(millis());
Serial.print(" , ");
Serial.print(sensorValue);
Serial.print(" , ");
Serial.println(timeEnd-timeStart);
}
}
23 changes: 23 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#######################################
# Syntax Coloring Map ADS1231
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

ADS1231 KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
attach KEYWORD2
power KEYWORD2
check KEYWORD2
readData KEYWORD2
calibrate KEYWORD2
sclPulse KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

0 comments on commit f3ec958

Please sign in to comment.