Skip to content

Commit

Permalink
Fix #10 comparator modi (#11)
Browse files Browse the repository at this point in the history
- fix#10 analogRead() for differential modi.
- add defines for 4 different modi
- add four comparator functions (wrappers).
- update readme.md
- update keywords.txt
- update GitHub actions
- update license 2023
- minor edits
  • Loading branch information
RobTillaart authored Feb 23, 2023
1 parent 2c998b0 commit 4ee8d83
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 78 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,32 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.2.0] - 2023-02-23
- fix#10 analogRead() for differential modi.
- add defines for 4 different modi
- add four comparator functions (wrappers).
- update readme.md
- update keywords.txt
- update GitHub actions
- update license 2023
- minor edits

----

## [0.1.3] - 2022-11-21
- add RP2040 to build-CI
- add changelog.md
- move code from .h to .cpp
- update unit test


## [0.1.2] - 2021-12-23
- update library.json
- update readme.md
- update license
- minor edits

## [0.1.1] - 2021-01-14
- added WireN
- added WireN
- improve error handling.

## [0.1.0] - 2021-01-04
Expand Down
73 changes: 52 additions & 21 deletions PCF8591.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FILE: PCF8591.cpp
// AUTHOR: Rob Tillaart
// DATE: 2020-03-12
// VERSION: 0.1.3
// VERSION: 0.2.0
// PURPOSE: I2C PCF8591 library for Arduino
// URL: https://github.com/RobTillaart/PCF8591

Expand Down Expand Up @@ -50,6 +50,7 @@ bool PCF8591::begin(uint8_t sda, uint8_t scl, uint8_t val)
}
#endif


bool PCF8591::begin(uint8_t val)
{
_wire->begin();
Expand All @@ -62,30 +63,31 @@ bool PCF8591::begin(uint8_t val)
bool PCF8591::isConnected()
{
_wire->beginTransmission(_address);
_error = _wire->endTransmission(); // default == 0 == PCF8591_OK
return( _error == PCF8591_OK);
_error = _wire->endTransmission(); // default == 0 == PCF8591_OK
return (_error == PCF8591_OK);
}


//////////////////////////////////////////////////////////
//
// ADC PART
//
void PCF8591::enableINCR()
{
_control |= PCF8591_INCR_FLAG;
};
}


void PCF8591::disableINCR()
{
_control &= ~PCF8591_INCR_FLAG;
};
}


bool PCF8591::isINCREnabled()
{
return ((_control & PCF8591_INCR_FLAG) > 0);
};
}


uint8_t PCF8591::analogRead(uint8_t channel, uint8_t mode)
Expand All @@ -95,44 +97,41 @@ uint8_t PCF8591::analogRead(uint8_t channel, uint8_t mode)
_error = PCF8591_MODE_ERROR;
return 0;
}
_control &= 0b01000100; // clear all except flags
_control &= 0b01000100; // clear all except flags
_control |= (mode << 4);

_error = PCF8591_CHANNEL_ERROR;
switch(mode)
{
case 0:
case PCF8591_FOUR_SINGLE_CHANNEL:
if (channel > 3) return 0;
_control |= channel;
break;
case 1:
case PCF8591_THREE_DIFFERENTIAL:
if (channel > 2) return 0;
_control |= (channel << 4);
break;
case 2:
case PCF8591_MIXED:
if (channel > 2) return 0;
_control |= (channel << 4);
break;
case 3:
case PCF8591_TWO_DIFFERENTIAL:
if (channel > 1) return 0;
_control |= (channel << 4);
break;
default:
return 0;
}
_control |= channel;
_error = PCF8591_OK;

// NOTE: one must read two values to get an up to date value.
// Page 8 datasheet.
// NOTE: one must read two values to get an up to date value.
// Page 8 datasheet.
_wire->beginTransmission(_address);
_wire->write(_control);
_error = _wire->endTransmission(); // default == 0 == PCF8591_OK
_error = _wire->endTransmission(); // default == 0 == PCF8591_OK
if (_error != 0) return PCF8591_I2C_ERROR;

if (_wire->requestFrom(_address, (uint8_t)2) != 2)
{
_error = PCF8591_I2C_ERROR;
return _adc[channel]; // known last value
return _adc[channel]; // return last known value
}
_wire->read();
_adc[channel] = _wire->read();
Expand All @@ -142,14 +141,16 @@ uint8_t PCF8591::analogRead(uint8_t channel, uint8_t mode)

uint8_t PCF8591::analogRead4()
{
_control &= 0b01000100; // clear all except flags
// clear all except flags
// MODE == PCF8591_FOUR_SINGLE_CHANNEL.
_control &= 0b01000100;
uint8_t channel = 0;
_control |= channel;

enableINCR();
_wire->beginTransmission(_address);
_wire->write(_control);
_error = _wire->endTransmission(); // default == 0 == PCF8591_OK
_error = _wire->endTransmission(); // default == 0 == PCF8591_OK
if (_error != 0)
{
_error = PCF8591_I2C_ERROR;
Expand Down Expand Up @@ -180,6 +181,36 @@ uint8_t PCF8591::lastRead(uint8_t channel)
};


// comparator calls need testing.
int PCF8591::readComparator_01()
{
int8_t v = analogRead(0, 3);
return v;
}


int PCF8591::readComparator_23()
{
int8_t v = analogRead(1, 3);
return v;
}


int PCF8591::readComparator_03()
{
int8_t v = analogRead(0, 1);
return v;
}


int PCF8591::readComparator_13()
{
int8_t v = analogRead(1, 1);
return v;
}



//////////////////////////////////////////////////////////
//
// DAC PART
Expand Down
39 changes: 27 additions & 12 deletions PCF8591.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// FILE: PCF8591.h
// AUTHOR: Rob Tillaart
// DATE: 2020-03-12
// VERSION: 0.1.3
// VERSION: 0.2.0
// PURPOSE: I2C PCF8591 library for Arduino
// URL: https://github.com/RobTillaart/PCF8591

Expand All @@ -12,21 +12,28 @@
#include "Wire.h"


#define PCF8591_LIB_VERSION (F("0.1.3"))
#define PCF8591_LIB_VERSION (F("0.2.0"))

#define PCF8591_OK 0x00
#define PCF8591_PIN_ERROR 0x81
#define PCF8591_I2C_ERROR 0x82
#define PCF8591_MODE_ERROR 0x83
#define PCF8591_CHANNEL_ERROR 0x84
#define PCF8591_ADDRESS_ERROR 0x85
#define PCF8591_OK 0x00
#define PCF8591_PIN_ERROR 0x81
#define PCF8591_I2C_ERROR 0x82
#define PCF8591_MODE_ERROR 0x83
#define PCF8591_CHANNEL_ERROR 0x84
#define PCF8591_ADDRESS_ERROR 0x85

// datasheet figure 4 page 6
#define PCF8591_FOUR_SINGLE_CHANNEL 0x00 // default
#define PCF8591_THREE_DIFFERENTIAL 0x01
#define PCF8591_MIXED 0x02
#define PCF8591_TWO_DIFFERENTIAL 0x03


class PCF8591
{
public:
explicit PCF8591(const uint8_t address = 0x48, TwoWire *wire = &Wire);

// set initial value for DAC, default 0
#if defined (ESP8266) || defined(ESP32)
bool begin(uint8_t sda, uint8_t scl, uint8_t value = 0);
#endif
Expand All @@ -42,18 +49,26 @@ class PCF8591
bool isINCREnabled();

// analogRead() returns the value.
uint8_t analogRead(uint8_t channel, uint8_t mode = 0);
// analogRead4() returns PCF8591_OK or an error code.
uint8_t analogRead4();
// mode 0 = PCF8591_FOUR_SINGLE_CHANNEL
uint8_t analogRead(uint8_t channel, uint8_t mode = 0);
// analogRead4() returns PCF8591_OK or an error code.
uint8_t analogRead4();
// access the 4 channels read with analogRead4()
uint8_t lastRead(uint8_t channel);

// datasheet par 8.2 figure 4
// not for PCF8591_MIXED mode.
// comparator calls need testing.
int readComparator_01(); // channel 0 mode 3
int readComparator_23(); // channel 1 mode 3
int readComparator_03(); // channel 0 mode 1
int readComparator_13(); // channel 1 mode 1


// DAC PART
void enableDAC();
void disableDAC();
bool isDACEnabled();

bool analogWrite(uint8_t value = 0); // returns true on success.
uint8_t lastWrite(); // returns last successful write

Expand Down
Loading

0 comments on commit 4ee8d83

Please sign in to comment.