added libs for AS6221 temperature sensor and SHT40 humidity and temperature sensor

This commit is contained in:
Angoosh Leviocki 2021-11-15 13:18:11 +01:00
parent e9e6d9df75
commit 2650265070
4 changed files with 331 additions and 0 deletions

201
AS6221.c Normal file
View File

@ -0,0 +1,201 @@
/*
* AS6221.c
*
* Created on: Nov 15, 2021
* Author: angoosh
*/
#include "AS6221.h"
#include "main.h"
extern I2C_HandleTypeDef AS6221_I2C_HANDLE;
float AS6221_Read_Temperature();
int AS6221_Register(SETUP_REG reg, uint8_t value, ACCESS_OP rw);
/*
* @brief Reads temperature measured by sensor
* @retval temperature in C
*/
float AS6221_Read_Temperature(){
uint8_t temp_raw[2];
uint16_t temp_bin = 0;
double temp = 0;
uint8_t index[1] = {1};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), index, 2, 0xFFFF);
HAL_I2C_Master_Receive(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), temp_raw, 2, 0xFFFF);
temp_bin = temp_raw[0] * 256 + temp_raw[1];
if(temp_bin >= 0x8000){//is negative
temp = ((~(temp_bin - 1)) * LSB) * (-1);
}
else{//is positive
temp = temp_bin * LSB;
}
return temp;
}
/*
* @brief Reads and Writes to control register
* @param register
* @param value to write
* @param access operation: R,W,RW
* @retval register contents
*/
int AS6221_Register(SETUP_REG reg, uint8_t value, ACCESS_OP rw){
uint8_t conf_reg[2];
uint8_t index[1] = {1};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), index, 2, 0xFFFF);
HAL_I2C_Master_Receive(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), conf_reg, 2, 0xFFFF);
switch(reg){
case AL:
return ((conf_reg[0] & 0x04) >> 2);
case CR:
if(rw == R){
return (conf_reg[0] & 0x03);
}
else if(rw == W){
value = value & 0x03;
conf_reg[0] = (conf_reg[0] & ~(0x03 << 0));
conf_reg[0] = (conf_reg[0] | (value << 0));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
}
else{
value = value & 0x03;
uint8_t retVal =(conf_reg[0] & 0x03);
conf_reg[0] = (conf_reg[0] & ~(0x03 << 0));
conf_reg[0] = (conf_reg[0] | (value << 0));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
return retVal;
}
case SM:
if(rw == R){
return (conf_reg[1] & 0x80);
}
else if(rw == W){
value = value & 0x01;
conf_reg[1] = (conf_reg[1] & ~(0x01 << 7));
conf_reg[1] = (conf_reg[1] | (value << 7));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
}
else{
value = value & 0x01;
uint8_t retVal =(conf_reg[1] & 0x80);
conf_reg[1] = (conf_reg[1] & ~(0x01 << 7));
conf_reg[1] = (conf_reg[1] | (value << 7));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
return retVal;
}
case IM:
if(rw == R){
return (conf_reg[1] & 0x40);
}
else if(rw == W){
value = value & 0x01;
conf_reg[1] = (conf_reg[1] & ~(0x01 << 6));
conf_reg[1] = (conf_reg[1] | (value << 6));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
}
else{
value = value & 0x01;
uint8_t retVal =(conf_reg[1] & 0x40);
conf_reg[1] = (conf_reg[1] & ~(0x01 << 6));
conf_reg[1] = (conf_reg[1] | (value << 6));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
return retVal;
}
case POL:
if(rw == R){
return (conf_reg[1] & 0x20);
}
else if(rw == W){
value = value & 0x01;
conf_reg[1] = (conf_reg[1] & ~(0x01 << 5));
conf_reg[1] = (conf_reg[1] | (value << 5));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
}
else{
value = value & 0x01;
uint8_t retVal =(conf_reg[1] & 0x20);
conf_reg[1] = (conf_reg[1] & ~(0x01 << 5));
conf_reg[1] = (conf_reg[1] | (value << 5));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
return retVal;
}
case CF:
if(rw == R){
return (conf_reg[1] & 0x18);
}
else if(rw == W){
value = value & 0x03;
conf_reg[1] = (conf_reg[1] & ~(0x03 << 3));
conf_reg[1] = (conf_reg[1] | (value << 3));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
}
else{
value = value & 0x03;
uint8_t retVal =(conf_reg[1] & 0x18);
conf_reg[1] = (conf_reg[1] & ~(0x01 << 3));
conf_reg[1] = (conf_reg[1] | (value << 3));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
return retVal;
}
case SS:
if(rw == R){
return (conf_reg[1] & 0x01);
}
else if(rw == W){
value = value & 0x01;
conf_reg[1] = (conf_reg[1] & ~(0x01 << 0));
conf_reg[1] = (conf_reg[1] | (value << 0));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
}
else{
value = value & 0x01;
uint8_t retVal =(conf_reg[1] & 0x01);
conf_reg[1] = (conf_reg[1] & ~(0x01 << 0));
conf_reg[1] = (conf_reg[1] | (value << 0));
uint8_t msg[3] = {index[0], conf_reg[0], conf_reg[1]};
HAL_I2C_Master_Transmit(&AS6221_I2C_HANDLE, (AS6221_ADDRESS << 1), msg, 4, 0xFFFF);
return retVal;
}
}
return 0;
}

36
AS6221.h Normal file
View File

@ -0,0 +1,36 @@
/*
* AS6221.h
*
* Created on: Nov 15, 2021
* Author: angoosh
*/
#ifndef INC_AS6221_H_
#define INC_AS6221_H_
#include "stm32l0xx_hal.h"
#define AS6221_ADDRESS 0x48
#define LSB (double)0.0078125
typedef enum {
AL = 0,
CR = 1,
SM = 2,
IM = 3,
POL = 4,
CF = 5,
SS = 6
} SETUP_REG;
typedef enum {
R = 0,
W = 1,
RW = 2
} ACCESS_OP;
float AS6221_Read_Temperature();
int AS6221_Register(SETUP_REG reg, uint8_t value, ACCESS_OP rw);
#endif /* INC_AS6221_H_ */

62
SHT40.c Normal file
View File

@ -0,0 +1,62 @@
/*
* SHT40.c
*
* Created on: Nov 15, 2021
* Author: angoosh
*/
#include "SHT40.h"
#include "main.h"
extern I2C_HandleTypeDef STH40_I2C_HANDLE;
void STH40_Read_Sensor_Data(STH40_COMMANDS command, float *retVal);
/* @brief Read sensor data of SHT40
* @param command to send
* @param returns temperature and humidity
* @retval float temperature or humidity
*/
void STH40_Read_Sensor_Data(STH40_COMMANDS command, float *retVal){
uint8_t data[1] = {command};
uint8_t SHT40_data[6];
if((command != Serial_Number) || (command != Soft_Reset)){
HAL_I2C_Master_Transmit(&STH40_I2C_HANDLE, (STH40_ADDRESS << 1), data, 2, 0xFFFF);
if((command == High_Precision) || (command == Medium_Precision) || (command == Low_Precision)){
HAL_Delay(10);
}
else if((command == Heater_200mW_1s) || (command == Heater_110mW_1s) || (command == Heater_20mW_1s)){
HAL_Delay(1010);
}
else{
HAL_Delay(110);
}
HAL_I2C_Master_Receive(&STH40_I2C_HANDLE, (STH40_ADDRESS << 1), SHT40_data, 6, 0xFFFF);
int t_ticks = SHT40_data[0] * 256 + SHT40_data[1];
int rh_ticks = SHT40_data[3] * 256 + SHT40_data[4];
float temp = -45 + 175 * t_ticks/65535;
float hum = -6 + 125 * rh_ticks/65535;
if(hum > 100){
hum = 100;
}
if(hum < 0){
hum = 0;
}
retVal[0] = temp;
retVal[1] = hum;
}
else if(command == Serial_Number){
HAL_I2C_Master_Transmit(&STH40_I2C_HANDLE, (STH40_ADDRESS << 1), data, 2, 0xFFFF);
HAL_Delay(10);
HAL_I2C_Master_Receive(&STH40_I2C_HANDLE, (STH40_ADDRESS << 1), SHT40_data, 6, 0xFFFF);
retVal[0] = SHT40_data[0] * 256 + SHT40_data[1];
retVal[1] = SHT40_data[3] * 256 + SHT40_data[4];
}
else{
HAL_I2C_Master_Transmit(&STH40_I2C_HANDLE, (STH40_ADDRESS << 1), data, 2, 0xFFFF);
}
}

32
SHT40.h Normal file
View File

@ -0,0 +1,32 @@
/*
* SHT40.h
*
* Created on: Nov 15, 2021
* Author: angoosh
*/
#ifndef INC_SHT40_H_
#define INC_SHT40_H_
#include "stm32l0xx_hal.h"
#define STH40_ADDRESS 0x44 //pro STH40xx-B is address 0x45
typedef enum {
High_Precision = 0xFD,
Medium_Precision = 0xF6,
Low_Precision = 0xE0,
Heater_200mW_1s = 0x39,
Heater_200mW_100ms = 0x32,
Heater_110mW_1s = 0x39,
Heater_110mW_100ms = 0x32,
Heater_20mW_1s = 0x39,
Heater_20mW_100ms = 0x32,
Serial_Number = 0x89,
Soft_Reset = 0x94
} STH40_COMMANDS;
void STH40_Read_Sensor_Data(STH40_COMMANDS command, float *retVal);
#endif /* INC_SHT40_H_ */