0.1.2 added get and set functions; fix return values; cleaned up code

This commit is contained in:
RobTillaart 2017-12-12 16:28:41 +01:00
parent c6427331b3
commit ba91a43896
7 changed files with 155 additions and 95 deletions

View File

@ -1,12 +1,13 @@
//
// FILE: AM232X.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// VERSION: 0.1.2
// PURPOSE: AM232X library for AM2320 for Arduino.
//
// HISTORY:
// 0.1.0 - 2017-12-11 initial version
// 0.1.1 = 2017-12-12 added CRC checking
// 0.1.1 - 2017-12-12 added CRC checking
// 0.1.2 - 2017-12-12 get and set functions.
//
// Released to the public domain
//
@ -20,16 +21,13 @@
AM232X::AM232X()
{
_deviceAddress = AM232X_ADDRESS;
_model = -1;
_version = -1;
_deviceID = -1;
}
int AM232X::read()
{
// READ HUMIDITY AND TEMPERATURE REGISTERS
int rv = _readRegister(0x00, 4);
if (rv < 0) return AM232X_ERROR_CONNECT;
if (rv < 0) return rv;
// CONVERT AND STORE
humidity = (bits[2]*256 + bits[3]) * 0.1;
@ -45,59 +43,75 @@ int AM232X::read()
int AM232X::getModel()
{
int rv = _readRegister(0x08, 2);
_model = (bits[2] * 256) + bits[3];
if (rv < 0) return AM232X_ERROR_CONNECT;
return AM232X_OK;
if (rv < 0) return rv;
return (bits[2] * 256) + bits[3];
}
int AM232X::getVersion()
{
int rv = _readRegister(0x0A, 1);
_version = bits[2];
if (rv < 0) return AM232X_ERROR_CONNECT;
return AM232X_OK;
if (rv < 0) return rv;
return bits[2];
}
uint32_t AM232X::getDeviceID()
{
int rv = _readRegister(0x0B, 4);
_deviceID = (bits[2] * 256) + bits[3];
if (rv < 0) return rv;
uint32_t _deviceID = (bits[2] * 256) + bits[3];
_deviceID = _deviceID * 256 + bits[4];
_deviceID = _deviceID * 256 + bits[5];
if (rv < 0) return AM232X_ERROR_CONNECT;
return AM232X_OK;
return _deviceID;
}
int AM232X::getStatus()
{
_readRegister(0x0F, 1);
int rv = _readRegister(0x0F, 1);
if (rv < 0) return rv;
return bits[2];
}
int AM232X::getUserRegisterA()
{
_readRegister(0x10, 2);
int rv = _readRegister(0x10, 2);
if (rv < 0) return rv;
return (bits[2] * 256) + bits[3];
}
int AM232X::getUserRegisterB()
{
_readRegister(0x12, 2);
int rv = _readRegister(0x12, 2);
if (rv < 0) return rv;
return (bits[2] * 256) + bits[3];
}
int AM232X::setStatus(uint8_t value)
{
int rv = _writeRegister(0x0F, 1, value);
if (rv < 0) return rv;
return AM232X_OK;
}
int AM232X::setUserRegisterA(int value)
{
// TODO
_writeRegister(0x10, 2, value);
int rv = _writeRegister(0x10, 2, value);
if (rv < 0) return rv;
return AM232X_OK;
}
int AM232X::setUserRegisterB(int value)
{
// TODO
_writeRegister(0x12, 2, value);
int rv = _writeRegister(0x12, 2, value);
if (rv < 0) return rv;
return AM232X_OK;
}
@ -107,8 +121,6 @@ int AM232X::setUserRegisterB(int value)
//
int AM232X::_readRegister(uint8_t reg, uint8_t count)
{
bool debug = true;
// wake up the sensor - see 8.2
Wire.beginTransmission(_deviceAddress);
int rv = Wire.endTransmission();
@ -126,17 +138,80 @@ int AM232X::_readRegister(uint8_t reg, uint8_t count)
uint8_t length = count + 4;
int bytes = Wire.requestFrom(_deviceAddress, length);
if (debug) Serial.println();
for (int i = 0; i < bytes; i++)
{
bits[i] = Wire.read();
if (debug)
}
// ANALYZE ERRORS
// will not detect if we requested 1 byte as that will
// return 5 bytes as requested. E.g. getStatus()
// TODO: design a fix.
if (bytes != length)
{
Serial.print(bits[i], HEX);
Serial.print("\t");
switch(bits[3])
{
case 0x80: return AM232X_ERROR_FUNCTION;
case 0x81: return AM232X_ERROR_ADDRESS;
case 0x82: return AM232X_ERROR_REGISTER;
case 0x83: return AM232X_ERROR_CRC_1; // prev write had a wrong CRC
case 0x84: return AM232X_ERROR_WRITE_DISABLED;
default: return AM232X_ERROR_UNKNOWN;
}
}
if (debug) Serial.println();
// CRC is LOW Byte first
uint16_t crc = bits[bytes - 1]*256 + bits[bytes - 2];
if (crc16(&bits[0], bytes - 2) != crc)
{
return AM232X_ERROR_CRC_2; // read itself has wrong CRC
}
return AM232X_OK;
}
int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int16_t value)
{
// wake up the sensor - see 8.2
Wire.beginTransmission(_deviceAddress);
int rv = Wire.endTransmission();
delayMicroseconds(1000); // TODO tune
// prepare data to send
bits[0] = 0x10;
bits[1] = reg;
bits[2] = cnt;
// TODO: is the order correct? MSB LSB
if (cnt == 2)
{
bits[4] = value & 0xFF;
bits[3] = (value >> 8) & 0xFF;
}
else
{
bits[3] = value & 0xFF;
}
// send data
uint8_t length = cnt + 3; // 3 = cmd, startReg, #bytes
Wire.beginTransmission(_deviceAddress);
for (int i=0; i< length; i++)
{
Wire.write(bits[i]);
}
// send the CRC
uint16_t crc = crc16(bits, length);
Wire.write(crc & 0xFF);
Wire.write(crc >> 8);
rv = Wire.endTransmission();
if (rv < 0) return rv;
// wait for the answer
int bytes = Wire.requestFrom(_deviceAddress, length);
for (int i = 0; i < bytes; i++)
{
bits[i] = Wire.read();
}
// ANALYZE ERRORS
// will not detect if we requested 1 byte as that will
@ -151,64 +226,16 @@ int AM232X::_readRegister(uint8_t reg, uint8_t count)
case 0x82: return AM232X_ERROR_REGISTER;
case 0x83: return AM232X_ERROR_CRC_1; // prev write had a wrong CRC
case 0x84: return AM232X_ERROR_WRITE_DISABLED;
default: return AM232X_MISSING_BYTES;
default: return AM232X_ERROR_UNKNOWN;
}
}
// CRC is LOW Byte first
uint16_t crc = bits[bytes - 1]*256 + bits[bytes - 2];
if (crc16(&bits[0], count-2) != crc)
crc = bits[bytes - 1]*256 + bits[bytes - 2];
if (crc16(&bits[0], bytes - 2) != crc)
{
if (debug)
{
Serial.print("CRC: ");
Serial.println(crc);
}
return AM232X_ERROR_CRC_2; // read itself has wrong CRC
}
return AM232X_OK;
}
int AM232X::_writeRegister(uint8_t reg, uint8_t cnt, int32_t value)
{
// wake up the sensor - see 8.2
Wire.beginTransmission(_deviceAddress);
int rv = Wire.endTransmission();
delayMicroseconds(1000); // TODO tune
// prepare data to send
bits[0] = 0x10;
bits[1] = reg;
bits[2] = cnt;
if (cnt == 4)
{
bits[6] = value & 0xFF;
bits[5] = (value >> 8) & 0xFF;
bits[4] = (value >> 8) & 0xFF;
bits[3] = (value >> 8) & 0xFF;
}
else if (cnt == 2)
{
bits[4] = value & 0xFF;
bits[3] = (value >> 8) & 0xFF;
}
else if (cnt == 1)
{
bits[3] = value & 0xFF;
}
uint16_t crc = crc16(bits, cnt + 3);
// send data
Wire.beginTransmission(_deviceAddress);
for (int i=0; i< cnt+3; i++)
{
Wire.write(bits[i]);
}
Wire.write(crc & 0xFF);
Wire.write(crc >> 8);
rv = Wire.endTransmission();
if (rv < 0) return rv;
return AM232X_OK;
}
@ -235,6 +262,5 @@ uint16_t AM232X::crc16(uint8_t *ptr, uint8_t len)
}
return crc;
}
//
// END OF FILE
//

View File

@ -4,9 +4,9 @@
// FILE: AM232X.h
// AUTHOR: Rob Tillaart
// PURPOSE: AM232X library for Arduino .
// VERSION: 0.1.1
// VERSION: 0.1.2
// HISTORY: See AM232X.cpp
// URL:
// URL: https://github.com/RobTillaart/Arduino/tree/master/libraries/
//
// Released to the public domain
//
@ -14,10 +14,10 @@
#include "Wire.h"
#include "Arduino.h"
#define AM232X_LIB_VERSION "0.1.1"
#define AM232X_LIB_VERSION "0.1.2"
#define AM232X_OK 0
#define AM232X_MISSING_BYTES -10
#define AM232X_ERROR_UNKNOWN -10
#define AM232X_ERROR_CONNECT -11
#define AM232X_ERROR_FUNCTION -12
#define AM232X_ERROR_ADDRESS -13
@ -25,6 +25,8 @@
#define AM232X_ERROR_CRC_1 -15
#define AM232X_ERROR_CRC_2 -16
#define AM232X_ERROR_WRITE_DISABLED -17
#define AM232X_ERROR_WRITE_COUNT -18
#define AM232X_MISSING_BYTES -19
/*
from datasheet
0x80: not support function code
@ -50,6 +52,7 @@ public:
int getUserRegisterA();
int getUserRegisterB();
int setStatus(uint8_t value);
int setUserRegisterA(int value);
int setUserRegisterB(int value);
@ -58,14 +61,10 @@ public:
private:
uint8_t _deviceAddress;
int _model;
int _version;
uint32_t _deviceID;
uint8_t bits[8];
int _readRegister(uint8_t reg, uint8_t cnt);
int _writeRegister(uint8_t reg, uint8_t cnt, int32_t value);
int _readRegister(uint8_t reg, uint8_t cnt);
int _writeRegister(uint8_t reg, uint8_t cnt, int16_t value);
uint16_t crc16(uint8_t *ptr, uint8_t len);
};

View File

@ -37,11 +37,9 @@ void loop()
case AM232X_OK:
Serial.print("OK,\t");
break;
case AM232X_MISSING_BYTES:
Serial.print("Missing Bytes,\t");
break;
default:
Serial.print("Unknown error,\t");
Serial.print(status);
Serial.print("\t");
break;
}
// DISPLAY DATA, sensor only returns one decimal.

View File

@ -1,11 +1,12 @@
//
// FILE: AM2320_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// PURPOSE: demo sketch for AM2320 I2C humidity & temperature sensor
//
// HISTORY:
// 0.1.0 - 2017-12-11 initial version
// 0.1.1 - 2017-12-12 added test for set* functions
//
// Released to the public domain
//
@ -24,14 +25,27 @@ void setup()
Serial.println(AM232X_LIB_VERSION);
Serial.println();
AM2320.read();
// sensor only returns one decimal.
Serial.println();
Serial.println("Read: \t");
int status = AM2320.read();
switch (status)
{
case AM232X_OK:
Serial.println("OK");
break;
default:
Serial.println(status);
break;
}
Serial.print("Temp:\t");
Serial.println(AM2320.temperature, 1);
Serial.print("Hum:\t");
Serial.println(AM2320.humidity, 1);
Serial.println();
Serial.println("GET SENSOR INFO (experimental)");
Serial.print("Model:\t");
Serial.println(AM2320.getModel());
Serial.print("Version:\t");
@ -39,6 +53,8 @@ void setup()
Serial.print("DevId:\t");
Serial.println(AM2320.getDeviceID());
Serial.println();
Serial.println("GET REGISTERS (experimental)");
Serial.print("Status:\t");
Serial.println(AM2320.getStatus());
Serial.print("UserA:\t");
@ -46,6 +62,18 @@ void setup()
Serial.print("UserB:\t");
Serial.println(AM2320.getUserRegisterB());
Serial.println();
Serial.println("SET REGISTERS (experimental)");
AM2320.setStatus(42);
Serial.print("Status 42 ==> ");
Serial.println(AM2320.getStatus());
AM2320.setUserRegisterA(1234);
Serial.print("UserA 1234 ==> ");
Serial.println(AM2320.getUserRegisterA());
AM2320.setUserRegisterB(5678);
Serial.print("UserB 5678 ==> ");
Serial.println(AM2320.getUserRegisterB());
Serial.println("done...");
}
@ -55,4 +83,4 @@ void loop()
//
// END OF FILE
//
///

View File

@ -37,11 +37,9 @@ void loop()
case AM232X_OK:
Serial.print("OK,\t");
break;
case AM232X_MISSING_BYTES:
Serial.print("Missing Bytes,\t");
break;
default:
Serial.print("Unknown error,\t");
Serial.print(status);
Serial.print("\t");
break;
}
// DISPLAY DATA, sensor only returns one decimal.

View File

@ -13,6 +13,17 @@ AM232X KEYWORD1
#######################################
read KEYWORD2
getModel KEYWORD2
getVersion KEYWORD2
getDeviceID KEYWORD2
getStatus KEYWORD2
getUserRegisterA KEYWORD2
getUserRegisterB KEYWORD2
setStatus KEYWORD2
setUserRegisterA KEYWORD2
setUserRegisterB KEYWORD2
humidity KEYWORD2
temperature KEYWORD2
#######################################
# Constants (LITERAL1)

View File

@ -1,5 +1,5 @@
name=AM232X
version=0.1.1
version=0.1.2
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for AM232X Temperature and Humidity sensor.