somewhat working ads1015 but results are still raw added an I2C function writeWordWithReg we nnet to think about it's meaning but it's wokring

master
Kerem Yollu 4 years ago
parent 822af574d4
commit f28dc3e09c

@ -1,6 +1,8 @@
cpp_src = $(wildcard *.cpp) $(wildcard ./drivers/bh1750/*.cpp) $(wildcard ./drivers/pca9685/*.cpp) $(wildcard ./management/*.cpp) $(wildcard ./systems/*.cpp) $(wildcard ./periferals/i2c/*.cpp) cpp_src = $(wildcard *.cpp) $(wildcard ./drivers/bh1750/*.cpp) $(wildcard ./drivers/pca9685/*.cpp) $(wildcard ./management/*.cpp) $(wildcard ./systems/*.cpp) $(wildcard ./periferals/i2c/*.cpp)
cpp_src += $(wildcard ./drivers/ads1015/*.cpp)
c_src = $(wildcard ./drivers/pf8574/*.c) c_src = $(wildcard ./drivers/pf8574/*.c)
c_src += $(wildcard ./algorithms/*.c)
cpp_obj = $(cpp_src:.cpp=.o) cpp_obj = $(cpp_src:.cpp=.o)
c_obj = $(c_src:.c=.o) c_obj = $(c_src:.c=.o)

@ -1,4 +1,3 @@
#include "../main.h"
#include"bitgestion.h" #include"bitgestion.h"
//SET N th bit to one //SET N th bit to one
@ -7,10 +6,11 @@
void throwError( uint16_t error) void throwError( uint16_t error)
{ {
printf("\n\rAglorithm.c : An Error has been generated on line : >>%d<<\n\r",error); printf("\n\rAglorithm.c : An Error has been generated on line : >>%d<<\n\r",error);
exit(1);
} }
void set_nth_bit_uint8( uint8_t *value, uint8_t &n) void setBitUint8(uint8_t *value, uint8_t &n)
{ {
if (n < 8 && n >=0) if (n < 8 && n >=0)
{ {
@ -22,7 +22,7 @@ void set_nth_bit_uint8( uint8_t *value, uint8_t &n)
} }
} }
void set_nth_bit_int8( int8_t *value, uint8_t &n) void setBitInt8(int8_t *value, uint8_t &n)
{ {
if (n < 8 && n >=0) if (n < 8 && n >=0)
{ {
@ -34,7 +34,7 @@ void set_nth_bit_int8( int8_t *value, uint8_t &n)
} }
} }
void set_nth_bit_uint16( uint16_t *value, uint8_t &n) void setBitUint16(uint16_t *value, uint8_t &n)
{ {
if (n < 16 && n >=0) if (n < 16 && n >=0)
{ {
@ -46,7 +46,7 @@ void set_nth_bit_uint16( uint16_t *value, uint8_t &n)
} }
} }
void set_nth_bit_int16( int16_t *value, uint8_t &n) void setBitInt16(int16_t *value, uint8_t &n)
{ {
if (n < 16 && n >=0) if (n < 16 && n >=0)
{ {
@ -59,7 +59,7 @@ void set_nth_bit_int16( int16_t *value, uint8_t &n)
} }
//SET N th bit to 0 //SET N th bit to 0
void unset_nth_bit_uint8( uint8_t *value, uint8_t &n) void unsetBitUint8(uint8_t *value, uint8_t &n)
{ {
if (n < 8 && n >=0) if (n < 8 && n >=0)
{ {
@ -71,7 +71,7 @@ void unset_nth_bit_uint8( uint8_t *value, uint8_t &n)
} }
} }
void unset_nth_bit_int8( int8_t *value, uint8_t &n) void unsetBitInt8(int8_t *value, uint8_t &n)
{ {
if (n < 8 && n >=0) if (n < 8 && n >=0)
{ {
@ -83,7 +83,7 @@ void unset_nth_bit_int8( int8_t *value, uint8_t &n)
} }
} }
void unset_nth_bit_uint16( uint16_t *value, uint8_t &n) void unsetBitUint16(uint16_t *value, uint8_t &n)
{ {
if (n < 16 && n >=0) if (n < 16 && n >=0)
{ {
@ -95,7 +95,7 @@ void unset_nth_bit_uint16( uint16_t *value, uint8_t &n)
} }
} }
void unset_nth_bit_int16( int16_t *value, uint8_t &n) void unsetBitInt16(int16_t *value, uint8_t &n)
{ {
if (n < 16 && n >=0) if (n < 16 && n >=0)
{ {
@ -108,7 +108,7 @@ void unset_nth_bit_int16( int16_t *value, uint8_t &n)
} }
//Toggle N th bit //Toggle N th bit
void toggle_nth_bit_uint8( uint8_t *value, uint8_t &n) void toggleBitUint8(uint8_t *value, uint8_t &n)
{ {
if (n < 8 && n >=0) if (n < 8 && n >=0)
{ {
@ -120,7 +120,7 @@ void toggle_nth_bit_uint8( uint8_t *value, uint8_t &n)
} }
} }
void toggle_nth_bit_int8( int8_t *value, uint8_t &n) void toggleBitInt8(int8_t *value, uint8_t &n)
{ {
if (n < 8 && n >=0) if (n < 8 && n >=0)
{ {
@ -132,7 +132,7 @@ void toggle_nth_bit_int8( int8_t *value, uint8_t &n)
} }
} }
void toggle_nth_bit_uint16( uint16_t *value, uint8_t &n) void toggleBitUint16(uint16_t *value, uint8_t &n)
{ {
if (n < 16 && n >=0) if (n < 16 && n >=0)
{ {
@ -144,7 +144,7 @@ void toggle_nth_bit_uint16( uint16_t *value, uint8_t &n)
} }
} }
void toggle_nth_bit_int16( int16_t *value, uint8_t &n) void toggleBitInt16(int16_t *value, uint8_t &n)
{ {
if (n < 16 && n >=0) if (n < 16 && n >=0)
{ {
@ -157,52 +157,56 @@ void toggle_nth_bit_int16( int16_t *value, uint8_t &n)
} }
//Get N th bit //Get N th bit
void get_nth_bit_uint8( uint8_t *value, uint8_t &n) uint8_t getBitUint8(uint8_t *value, uint8_t &n)
{ {
if (n < 8 && n >=0) if (n < 8 && n >=0)
{ {
*value= (*value>> n) & 1; return (*value>> n) & 1;
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_nth_bit_int8( int8_t *value, uint8_t &n) int8_t getBitInt8(int8_t *value, uint8_t &n)
{ {
if (n < 8 && n >=0) if (n < 8 && n >=0)
{ {
*value= (*value>> n) & 1; return (*value>> n) & 1;
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_nth_bit_uint16( uint16_t *value, uint8_t &n) uint16_t getBitUint16(uint16_t *value, uint8_t &n)
{ {
if (n < 16 && n >=0) if (n < 16 && n >=0)
{ {
*value= (*value>> n) & 1; return (*value>> n) & 1;
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_nth_bit_int16( int16_t *value, uint8_t &n) int16_t getBitInt16(int16_t *value, uint8_t &n)
{ {
if (n < 16 && n >=0) if (n < 16 && n >=0)
{ {
*value= (*value>> n) & 1; return (*value>> n) & 1;
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
@ -210,7 +214,7 @@ void get_nth_bit_int16( int16_t *value, uint8_t &n)
//Change N th bit to x //Change N th bit to x
//number = (number & ~(1UL << n)) | (x << n); //number = (number & ~(1UL << n)) | (x << n);
void change_nth_bit_to_x_uint8( uint8_t *value, uint8_t &n, uint8_t &x) void changeBitUint8(uint8_t *value, uint8_t &n, uint8_t &x)
{ {
if (n < 8 && n >=0) if (n < 8 && n >=0)
{ {
@ -222,7 +226,7 @@ void change_nth_bit_to_x_uint8( uint8_t *value, uint8_t &n, uint8_t &x)
} }
} }
void change_nth_bit_to_x_int8( int8_t *value, uint8_t &n, uint8_t &x) void changeBitInt8(int8_t *value, uint8_t &n, uint8_t &x)
{ {
if (n < 8 && n >=0) if (n < 8 && n >=0)
{ {
@ -234,7 +238,7 @@ void change_nth_bit_to_x_int8( int8_t *value, uint8_t &n, uint8_t &x)
} }
} }
void change_nth_bit_to_x_uint16( uint16_t *value, uint8_t &n, uint8_t &x) void changeBitUint16(uint16_t *value, uint8_t &n, uint8_t &x)
{ {
if (n < 16 && n >=0) if (n < 16 && n >=0)
{ {
@ -246,7 +250,7 @@ void change_nth_bit_to_x_uint16( uint16_t *value, uint8_t &n, uint8_t &x)
} }
} }
void change_nth_bit_to_x_int16( int16_t *value, uint8_t &n, uint8_t &x) void changeBitInt16(int16_t *value, uint8_t &n, uint8_t &x)
{ {
if (n < 16 && n >=0) if (n < 16 && n >=0)
{ {
@ -262,69 +266,74 @@ void change_nth_bit_to_x_int16( int16_t *value, uint8_t &n, uint8_t &x)
//GET N numer of least significant bits //GET N numer of least significant bits
void get_n_bits_lsb_uint8( uint8_t *value, uint8_t &bits) uint8_t get_n_bits_lsb_uint8( uint8_t *value, uint8_t &bits)
{ {
if (bits < 8 && bits >=0) if (bits < 8 && bits >=0)
{ {
*value= *value& ((1 << bits)-1); return *value& ((1 << bits)-1);
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_n_bits_lsb_int8(int8_t *value, uint8_t &bits) int8_t get_n_bits_lsb_int8(int8_t *value, uint8_t &bits)
{ {
if (bits < 8 && bits >=0) if (bits < 8 && bits >=0)
{ {
*value= *value& ((1 << bits)-1); return *value& ((1 << bits)-1);
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_n_bits_lsb_uint16( uint16_t *value, uint8_t &bits) uint16_t get_n_bits_lsb_uint16( uint16_t *value, uint8_t &bits)
{ {
if (bits < 8 && bits >=0) if (bits < 8 && bits >=0)
{ {
*value= *value& ((1 << bits)-1); return *value& ((1 << bits)-1);
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_n_bits_lsb_int16( int16_t *value, uint8_t &bits) int16_t get_n_bits_lsb_int16( int16_t *value, uint8_t &bits)
{ {
if (bits < 16 && bits >=0) if (bits < 16 && bits >=0)
{ {
*value= *value& ((1 << bits)-1); return *value& ((1 << bits)-1);
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
//Get N number of most signigiant bit. //Get N number of most signigiant bit.
void get_n_bits_msb_uint8( uint8_t *value, uint8_t &bits) uint8_t get_n_bits_msb_uint8( uint8_t *value, uint8_t &bits)
{ {
if (bits < 8 && bits >=0) if (bits < 8 && bits >=0)
{ {
*value= *value>> (8-bits); return *value>> (8-bits);
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_n_bits_msb_int8(int8_t *value, uint8_t &bits) int8_t get_n_bits_msb_int8(int8_t *value, uint8_t &bits)
{ {
if (bits < 8 && bits >=0) if (bits < 8 && bits >=0)
{ {
@ -334,34 +343,37 @@ void get_n_bits_msb_int8(int8_t *value, uint8_t &bits)
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_n_bits_msb_uint16( uint16_t *value, uint8_t &bits) uint16_t get_n_bits_msb_uint16( uint16_t *value, uint8_t &bits)
{ {
if (bits < 16 && bits >=0) if (bits < 16 && bits >=0)
{ {
*value= *value>> (16-bits); return *value>> (16-bits);
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_n_bits_msb_int16( int16_t *value, uint8_t &bits) int16_t get_n_bits_msb_int16( int16_t *value, uint8_t &bits)
{ {
if (bits < 16 && bits >=0) if (bits < 16 && bits >=0)
{ {
*value= *value>> (16-bits); return *value>> (16-bits);
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
// Get Bits range from startBit to stopBit and *value= them shifted. // Get Bits range from startBit to stopBit and return them shifted.
void get_bits_range_uint8( uint8_t *value, uint8_t &startBit, uint8_t &stopBit) uint8_t get_bits_range_uint8( uint8_t *value, uint8_t &startBit, uint8_t &stopBit)
{ {
if (startBit < 8 && startBit >=0) if (startBit < 8 && startBit >=0)
{ {
@ -371,21 +383,30 @@ void get_bits_range_uint8( uint8_t *value, uint8_t &startBit, uint8_t &stopBit)
{ {
uint8_t mask = 0; uint8_t mask = 0;
uint8_t i = 0; uint8_t i = 0;
for(i = startBit; i < stopBit; i++ ) for(i = startBit; i <= stopBit; i++ )
{ {
mask |= (1 << i); mask |= (1 << i);
} }
*value= ((*value& mask) >> startBit); return ((*value& mask) >> startBit);
}
else
{
throwError(__LINE__);
} }
} }
else
{
throwError(__LINE__);
}
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_bits_range_int8( int8_t *value, uint8_t &startBit, uint8_t &stopBit) int8_t get_bits_range_int8( int8_t *value, uint8_t &startBit, uint8_t &stopBit)
{ {
if (startBit < 8 && startBit >=0) if (startBit < 8 && startBit >=0)
{ {
@ -395,21 +416,30 @@ void get_bits_range_int8( int8_t *value, uint8_t &startBit, uint8_t &stopBit)
{ {
uint8_t mask = 0; uint8_t mask = 0;
uint8_t i = 0; uint8_t i = 0;
for(i = startBit; i < stopBit; i++ ) for(i = startBit; i <= stopBit; i++ )
{ {
mask |= (1 << i); mask |= (1 << i);
} }
*value= (*value& mask) >> startBit; return (*value& mask) >> startBit;
} }
else
{
throwError(__LINE__);
}
}
else
{
throwError(__LINE__);
} }
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_bits_range_uint16( uint16_t *value, uint8_t &startBit, uint8_t &stopBit) uint16_t get_bits_range_uint16( uint16_t *value, uint8_t &startBit, uint8_t &stopBit)
{ {
if (startBit < 16 && startBit >=0) if (startBit < 16 && startBit >=0)
{ {
@ -417,25 +447,34 @@ void get_bits_range_uint16( uint16_t *value, uint8_t &startBit, uint8_t &stopBit
{ {
if(startBit < stopBit) if(startBit < stopBit)
{ {
uint8_t mask = 0; uint16_t mask = 0;
uint8_t i = 0; uint8_t i = 0;
for(i = startBit; i < stopBit; i++ ) for(i = startBit; i <= stopBit; i++ )
{ {
mask |= (1 << i); mask |= (1 << i);
} }
*value= (*value& mask) >> startBit; return (*value& mask) >> startBit;
}
else
{
throwError(__LINE__);
} }
} }
else
{
throwError(__LINE__);
}
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
void get_bits_range_int16( int16_t *value, uint8_t &startBit, uint8_t &stopBit) int16_t get_bits_range_int16( int16_t *value, uint8_t &startBit, uint8_t &stopBit)
{ {
if (startBit < 16 && startBit >=0) if (startBit < 16 && startBit >=0)
{ {
@ -443,20 +482,29 @@ void get_bits_range_int16( int16_t *value, uint8_t &startBit, uint8_t &stopBit)
{ {
if(startBit < stopBit) if(startBit < stopBit)
{ {
uint8_t mask = 0; int16_t mask = 0;
uint8_t i = 0; uint8_t i = 0;
for(i = startBit; i < stopBit; i++ ) for(i = startBit; i <= stopBit; i++ )
{ {
mask |= (1 << i); mask |= (1 << i);
} }
*value= (*value& mask) >> startBit; return (*value& mask) >> startBit;
}
else
{
throwError(__LINE__);
}
} }
else
{
throwError(__LINE__);
} }
} }
else else
{ {
throwError(__LINE__); throwError(__LINE__);
} }
return 0;
} }
@ -470,12 +518,29 @@ void set_bits_range_uint8(uint8_t *value, uint8_t &startBit, uint8_t &stopBit, u
{ {
uint8_t mask = 0; uint8_t mask = 0;
uint8_t i = 0; uint8_t i = 0;
for(i = startBit; i < stopBit; i++ ) mask = (replace >> (stopBit - startBit +1));
if(mask == 0)
{
for(i = startBit; i <= stopBit; i++ )
{ {
mask |= (1 << i); mask |= (1 << i);
} }
*value= (*value & ~mask) | replace << startBit; *value= (*value & ~mask) | replace << startBit;
} }
else
{
throwError(__LINE__);
}
}
else
{
throwError(__LINE__);
}
}
else
{
throwError(__LINE__);
} }
} }
else else
@ -483,7 +548,6 @@ void set_bits_range_uint8(uint8_t *value, uint8_t &startBit, uint8_t &stopBit, u
throwError(__LINE__); throwError(__LINE__);
} }
} }
void set_bits_range_uint16(uint16_t *value, uint8_t &startBit, uint8_t &stopBit, uint16_t &replace) void set_bits_range_uint16(uint16_t *value, uint8_t &startBit, uint8_t &stopBit, uint16_t &replace)
{ {
if (startBit < 16 && startBit >=0) if (startBit < 16 && startBit >=0)
@ -492,14 +556,32 @@ void set_bits_range_uint16(uint16_t *value, uint8_t &startBit, uint8_t &stopBit,
{ {
if(startBit < stopBit) if(startBit < stopBit)
{ {
uint8_t mask = 0; uint16_t mask = 1;
uint8_t i = 0; uint8_t i = 0;
for(i = startBit; i < stopBit; i++ )
mask = (replace >> (stopBit - startBit +1));
if(mask == 0)
{
for(i = startBit; i <= stopBit; i++ )
{ {
mask |= (1 << i); mask |= (1 << i);
} }
*value= (*value & ~mask) | replace << startBit; *value= (*value & ~mask) | replace << startBit;
} }
else
{
throwError(__LINE__);
}
}
else
{
throwError(__LINE__);
}
}
else
{
throwError(__LINE__);
} }
} }
else else

@ -1,44 +1,48 @@
void set_bits_range_uint16(uint16_t *value, uint8_t &startBit, uint8_t &stopBit, uint16_t &replace); #include <stdint.h>
void set_bits_range_uint8(uint8_t *value, uint8_t &startBit, uint8_t &stopBit, uint8_t &replace); #include <stdio.h>
#include <stdlib.h>
void throwError( uint16_t error); void throwError( uint16_t error);
void set_nth_bit_uint8( uint8_t *value, uint8_t &n); void setBitUint8(uint8_t *value, uint8_t &n);
void set_nth_bit_int8( int8_t *value, uint8_t &n); void setBitInt8(int8_t *value, uint8_t &n);
void set_nth_bit_uint16( uint16_t *value, uint8_t &n); void setBitUint16(uint16_t *value, uint8_t &n);
void set_nth_bit_int16( int16_t *value, uint8_t &n); void setBitInt16(int16_t *value, uint8_t &n);
void unset_nth_bit_uint8( uint8_t *value, uint8_t &n); void unsetBitUint8(uint8_t *value, uint8_t &n);
void unset_nth_bit_int8( int8_t *value, uint8_t &n); void unsetBitInt8(int8_t *value, uint8_t &n);
void unset_nth_bit_uint16( uint16_t *value, uint8_t &n); void unsetBitUint16(uint16_t *value, uint8_t &n);
void unset_nth_bit_int16( int16_t *value, uint8_t &n); void unsetBitInt16(int16_t *value, uint8_t &n);
void toggle_nth_bit_uint8( uint8_t *value, uint8_t &n); void toggleBitUint8(uint8_t *value, uint8_t &n);
void toggle_nth_bit_int8( int8_t *value, uint8_t &n); void toggleBitInt8(int8_t *value, uint8_t &n);
void toggle_nth_bit_uint16( uint16_t *value, uint8_t &n); void toggleBitUint16(uint16_t *value, uint8_t &n);
void toggle_nth_bit_int16( int16_t *value, uint8_t &n); void toggleBitInt16(int16_t *value, uint8_t &n);
void get_nth_bit_uint8( uint8_t *value, uint8_t &n); uint8_t getBitUint8(uint8_t *value, uint8_t &n);
void get_nth_bit_int8( int8_t *value, uint8_t &n); int8_t getBitInt8(int8_t *value, uint8_t &n);
void get_nth_bit_uint16( uint16_t *value, uint8_t &n); uint16_t getBitUint16(uint16_t *value, uint8_t &n);
void get_nth_bit_int16( int16_t *value, uint8_t &n); int16_t getBitInt16(int16_t *value, uint8_t &n);
void change_nth_bit_to_x_uint8( uint8_t *value, uint8_t &n, uint8_t &x); void changeBitUint8(uint8_t *value, uint8_t &n, uint8_t &x);
void change_nth_bit_to_x_int8( int8_t *value, uint8_t &n, uint8_t &x); void changeBitInt8(int8_t *value, uint8_t &n, uint8_t &x);
void change_nth_bit_to_x_uint16( uint16_t *value, uint8_t &n, uint8_t &x); void changeBitUint16(uint16_t *value, uint8_t &n, uint8_t &x);
void change_nth_bit_to_x_int16( int16_t *value, uint8_t &n, uint8_t &x); void changeBitInt16(int16_t *value, uint8_t &n, uint8_t &x);
void get_n_bits_lsb_uint8( uint8_t *value, uint8_t &bits); uint8_t get_n_bits_lsb_uint8(uint8_t *value, uint8_t &bits);
void get_n_bits_lsb_int8(int8_t *value, uint8_t &bits); int8_t get_n_bits_lsb_int8(int8_t *value, uint8_t &bits);
void get_n_bits_lsb_uint16( uint16_t *value, uint8_t &bits); uint16_t get_n_bits_lsb_uint16(uint16_t *value, uint8_t &bits);
void get_n_bits_lsb_int16( int16_t *value, uint8_t &bits); int16_t get_n_bits_lsb_int16(int16_t *value, uint8_t &bits);
void get_n_bits_msb_uint8( uint8_t *value, uint8_t &bits); uint8_t get_n_bits_msb_uint8(uint8_t *value, uint8_t &bits);
void get_n_bits_msb_int8(int8_t *value, uint8_t &bits); int8_t get_n_bits_msb_int8(int8_t *value, uint8_t &bits);
void get_n_bits_msb_uint16( uint16_t *value, uint8_t &bits); uint16_t get_n_bits_msb_uint16(uint16_t *value, uint8_t &bits);
void get_n_bits_msb_int16( int16_t *value, uint8_t &bits); int16_t get_n_bits_msb_int16(int16_t *value, uint8_t &bits);
void get_bits_range_uint8( uint8_t *value, uint8_t &startBit, uint8_t &stopBit); uint8_t get_bits_range_uint8(uint8_t *value, uint8_t &startBit, uint8_t &stopBit);
void get_bits_range_int8( int8_t *value, uint8_t &startBit, uint8_t &stopBit); int8_t get_bits_range_int8(int8_t *value, uint8_t &startBit, uint8_t &stopBit);
void get_bits_range_uint16( uint16_t *value, uint8_t &startBit, uint8_t &stopBit); uint16_t get_bits_range_uint16(uint16_t *value, uint8_t &startBit, uint8_t &stopBit);
void get_bits_range_int16( int16_t *value, uint8_t &startBit, uint8_t &stopBit); int16_t get_bits_range_int16(int16_t *value, uint8_t &startBit, uint8_t &stopBit);
void set_bits_range_uint16(uint16_t *value, uint8_t &startBit, uint8_t &stopBit, uint16_t &replace);
void set_bits_range_uint8(uint8_t *value, uint8_t &startBit, uint8_t &stopBit, uint8_t &replace);

@ -0,0 +1,105 @@
#include "ads1050.h"
Ads1015::Ads1015(i2c_ch1_pImpL* i2c,ErrorHandler* err)
{
i2c_ads1015 = i2c;
errorHandling = err;
i2cWrite = 1;
configuration = ADS1015_CONFIG_RESET;
writeConfig();
configuration = 0;
errorHandling->addNewError(-1,__FILE__,"This mutpilexer mode does not Exist",KILL);
}
void Ads1015::writeConfig()
{
std::cout << "To be written conf = "<< unsigned(configuration) << std::endl;
i2c_ads1015->writeWordWithReg(ADS1015_I2C_ADRS,ADS1015_REG_CONFIG,configuration);
}
uint16_t Ads1015::getConfig()
{
configuration = i2c_ads1015->readWord(ADS1015_I2C_ADRS, ADS1015_REG_CONFIG);
return configuration;
}
void Ads1015::setMultiplexer(uint16_t mode)
{
if(mode <= ADS1015_MUX_AIN3_GND)
{
bitStart = ADS1015_MUX_BIT_NO;
bitEnd = bitStart + ADS1015_MUX_BIT_LEN -1;
set_bits_range_uint16(&configuration,bitStart,bitEnd,mode);
}
else
{
errorHandling->handleError(-1,__FILE__);
}
}
void Ads1015::setGain(uint16_t gain)
{
if(gain <= ADS1015_GAIN_256)
{
bitStart = ADS1015_GAIN_BIT_NO;
bitEnd = bitStart + ADS1015_GAIN_BIT_LEN -1;
set_bits_range_uint16(&configuration,bitStart,bitEnd,gain);
}
}
void Ads1015::setOperationMode(uint8_t mode)
{
if(mode <= ADS1015_MODE_SINGLE)
{
bitNo = ADS1015_MODE_BIT_NO;
changeBitUint16(&configuration,bitNo,mode);
}
}
void Ads1015::setDataRate(uint8_t rate)
{
}
void Ads1015::setComparatorMode(uint8_t mode)
{
}
void Ads1015::setComparatorPolarity(uint8_t pol)
{
}
void Ads1015::setComparatorLatch(uint8_t latch)
{
}
void Ads1015::setComparatorQueue(uint8_t queue)
{
}
uint8_t Ads1015::checkConversion()
{
bitNo = ADS1015_OS_CONVERTING;
return getBitUint16(&configuration,bitNo);
}
int16_t Ads1015::getConversion()
{
conversion = i2c_ads1015 -> readWord(ADS1015_I2C_ADRS,ADS1015_REG_CONVERSION)>>4;
if (conversion > 0x07FF) {
// negative number - extend the sign to 16th bit
conversion |= 0xF000;
}
return (int16_t)conversion;
}
void Ads1015::startSingleConvertion()
{
bitNo = ADS1015_OS_BIT_NO;
getConfig();
setBitUint16(&configuration,bitNo);
}

@ -11,26 +11,78 @@
#ifndef _ADS1015_H_ #ifndef _ADS1015_H_
#define _ADS1015_H_ #define _ADS1015_H_
#define ADS1015_I2C_ADRS 0x48
#define ADS1015_REG_CONVERSION 0 // Conversion Register
#define ADS1015_REG_CONFIG 1 // Config Register
#define ADS1015_REG_LOW_TRESHOLD 2 // Low Thresh Register
#define ADS1015_REG_HIGH_TRESHOLD 3 // Hight Thresh Register
#define ADS115_MUX_AIN0P_AIN1N 0 // AINP=AIN0 and AINN=AIN1
#define ADS115_MUX_AIN0P_AIN3N 1 // AINP=AIN0 and AINN=AIN3
#define ADS115_MUX_AIN1P_AIN3N 2 // AINP=AIN1 and AINN=AIN3
#define ADS115_MUX_AIN2P_AIN3N 3 // AINP=AIN2 and AINN=AIN3
#define ADS115_MUX_AIN0_GND 4 // AIN0 and AINN=GND
#define ADS115_MUX_AIN0_GND 5 // AIN1 and AINN=GND
#define ADS115_MUX_AIN0_GND 6 // AIN2 and AINN=GND
#define ADS115_MUX_AIN0_GND 7 // AIN3 and AINN=GND
//do not applymorethan VDD+0.3 V to the analog inputs of the device #define ADS1015_CONFIG_RESET 0x8583 // Default mode.
#define ADS115_GAIN_6144 0 // ±6.144V #define ADS1015_OS_CONVERTING 0 // When reading: Device is currently performing a conversion
#define ADS115_GAIN_4096 1 // ±6.144V #define ADS1015_OS_READY 1 // When reading: Device is not currently performing a conversion
#define ADS115_GAIN_2048 2 // ±6.144V #define ADS1015_OS_START_SINGLE 1 // Start a single conversion(when in power-down state)
#define ADS115_GAIN_1024 3 // ±6.144V #define ADS1015_OS_BIT_NO 15
#define ADS115_GAIN_512 4 // ±6.144V #define ADS1015_OS_BIT_LEN 1
#define ADS115_GAIN_256 5 // ±6.144V
#define ADS1015_MUX_AIN0P_AIN1N 0 // AINP=AIN0 and AINN=AIN1
#define ADS1015_MUX_AIN0P_AIN3N 1 // AINP=AIN0 and AINN=AIN3
#define ADS1015_MUX_AIN1P_AIN3N 2 // AINP=AIN1 and AINN=AIN3
#define ADS1015_MUX_AIN2P_AIN3N 3 // AINP=AIN2 and AINN=AIN3
#define ADS1015_MUX_AIN0_GND 4 // AIN0 and AINN=GND
#define ADS1015_MUX_AIN1_GND 5 // AIN1 and AINN=GND
#define ADS1015_MUX_AIN2_GND 6 // AIN2 and AINN=GND
#define ADS1015_MUX_AIN3_GND 7 // AIN3 and AINN=GND
#define ADS1015_MUX_BIT_NO 12
#define ADS1015_MUX_BIT_LEN 3
// Do not applymorethan VDD+0.3 V to the analog inputs of the device
#define ADS1015_GAIN_6144 0 // +-6.144 V
#define ADS1015_GAIN_4096 1 // +-4.096 V
#define ADS1015_GAIN_2048 2 // +-2.048 V
#define ADS1015_GAIN_1024 3 // +-1.024 V
#define ADS1015_GAIN_512 4 // +-0.512 V
#define ADS1015_GAIN_256 5 // +-0.256 V
#define ADS1015_GAIN_BIT_NO 9
#define ADS1015_GAIN_BIT_LEN 3
#define ADS1015_MODE_CONTINIOUS 0
#define ADS1015_MODE_SINGLE 1
#define ADS1015_MODE_BIT_NO 8
#define ADS1015_MODE_BIT_LEN 1
#define ADS1015_DR_128_SPS 0 // 000 : 128 SPS
#define ADS1015_DR_250_SPS 1 // 001 : 250 SPS
#define ADS1015_DR_490_SPS 2 // 010 : 490 SPS
#define ADS1015_DR_920_SPS 3 // 011 : 920 SPS
#define ADS1015_DR_1600_SPS 4 // 100 : 1600 SPS(default)
#define ADS1015_DR_2400_SPS 5 // 101 : 2400 SPS
#define ADS1015_DR_3300_SPS 6 // 110 : 3300 SPS
#define ADS1015_DR_BIT_NO 5
#define ADS1015_DR_BIT_LEN 3
#define ADS1015_COMP_MODE_NORMAL 0 // Traditional comparator(default
#define ADS1015_COMP_MODE_WINDOW 1 // Window comparator
#define ADS1015_COMP_MODE_BIT_NO 4
#define ADS1015_COMP_MODE_BIT_LEN 1
#define ADS1015_COMP_POL_LOW 0 // Active low (default)
#define ADS1015_COMP_POL_HIGH 1 // Active high
#define ADS1015_COMP_POL_BIT_NO 3
#define ADS1015_COMP_POL_BIT_LEN 1
#define ADS1015_COMP_LAT_OFF 0 // Non latching comparator. The ALERT/RDY pin does not latch when asserted (default)
#define ADS1015_COMP_LAT_ON 1 // Latching comparator.The asserted ALERT/RDY pin remains latched until conversion data are read by the master or an appropriate SMBus alert response is sent by the master. The device responds with its address,and it is the lowest address currently asserting the ALERT/RDY bus line.
#define ADS1015_COMP_LAT_BIT_NO 2
#define ADS1015_COMP_LAT_BIT_LEN 1
#define ADS1015_COMP_QUE_1 0 // Assert after one conversion
#define ADS1015_COMP_QUE_2 1 // Assert after two conversion
#define ADS1015_COMP_QUE_3 2 // Assert after four conversion
#define ADS1015_COMP_QUE_0 3 // Disable comparator and set ALERT/RDY pin to high-impedance (default)
#define ADS1015_COMP_QUE_BIT_NO 0
#define ADS1015_COMP_QUE_BIT_LEN 2
#define ADS115_MODE_CONTINIOUS 0
#define ADS115_MODE_SINGLE 1
#include <stdint.h> #include <stdint.h>
@ -39,20 +91,39 @@
#include <math.h> #include <math.h>
#include "../../periferals/i2c/i2c_ch1_pImpL.hpp" #include "../../periferals/i2c/i2c_ch1_pImpL.hpp"
#include "../../management/errorHandling.h" #include "../../management/errorHandling.h"
#include "../../algorithms/bitgestion.h"
class Ads1015 class Ads1015
{ {
public: public:
Ads1015(i2c_ch1_pImpL* i2c,ErrorHandler* err); Ads1015(i2c_ch1_pImpL* i2c,ErrorHandler* err);
setMultiplexer(uint8_t mode) void setMultiplexer(uint16_t mode);
void setGain(uint16_t gain);
void setOperationMode(uint8_t mode);
void setDataRate(uint8_t rate);
void setComparatorMode(uint8_t mode);
void setComparatorPolarity(uint8_t pol);
void setComparatorLatch(uint8_t latch);
void setComparatorQueue(uint8_t queue);
void startSingleConvertion();
uint8_t checkConversion();
int16_t getConversion();
uint16_t getConfig();
void configMode();
void writeConfig();
private: private:
ErrorHandler* errorHandling; ErrorHandler* errorHandling;
i2c_ch1_pImpL* i2c_ads1015; i2c_ch1_pImpL* i2c_ads1015;
uint8_t i2cWrite;
uint8_t bitNo;
uint8_t bitStart;
uint8_t bitEnd;
uint16_t Toset;
uint16_t configuration;
uint16_t conversion;
uint16_t lowTreshold;
uint16_t highTreshlod;
}; };
#endif //_ADS1015_H_ #endif //_ADS1015_H_

@ -10,15 +10,19 @@
#include <iostream> #include <iostream>
#include <stdint.h> #include <stdint.h>
#include <unistd.h> #include <unistd.h>
#include <bitset>
#include "./management/errorHandling.h" #include "./management/errorHandling.h"
#include "./management/commandManager.h" #include "./management/commandManager.h"
#include "./drivers/bh1750/bh1750.h" #include "./drivers/bh1750/bh1750.h"
#include "./drivers/pf8574/pf8574lcd.h" #include "./drivers/pf8574/pf8574lcd.h"
#include "./drivers/pca9685/pca9685.h" #include "./drivers/pca9685/pca9685.h"
#include "./drivers/ads1015/ads1050.h"
#include "./algorithms/bitgestion.h"
ErrorHandler errorHandle; ErrorHandler errorHandle;
CommandManager commander; CommandManager commander;
i2c_ch1_pImpL i2c(1, &errorHandle); i2c_ch1_pImpL i2c(1, &errorHandle);
Ads1015 adc(&i2c,&errorHandle);
char char_array[TOTAL_CHAR_CAP]; char char_array[TOTAL_CHAR_CAP];
int freq = 0; int freq = 0;
@ -65,14 +69,25 @@ void bh1750_test()
void dummy() void dummy()
{ {
Pca9685 pwmGenarator(&i2c, &errorHandle); uint16_t conf = 0;
for(int i = 0 ; i <= 100; i++) conf = adc.getConfig();
adc.setOperationMode(ADS1015_MODE_SINGLE);
adc.setMultiplexer(ADS1015_MUX_AIN0_GND);
adc.setGain(ADS1015_GAIN_6144);
adc.writeConfig();
while(adc.checkConversion() == 0)
{ {
pwmGenarator.setOnDutyPercent(0,i); std::cout << "converting" << std::endl;
usleep(200000); usleep(100000);
} }
uint16_t conv = adc.getConversion();
std::cout << "Convertion " << unsigned(conv) << std::endl;
exit(1);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
// Init // Init

@ -68,7 +68,7 @@ uint8_t i2c_ch1_pImpL::readByte(const uint8_t& address, const uint8_t& reg)
} }
uint8_t i2c_ch1_pImpL::writeWord(const uint8_t& address, const uint8_t& reg, const uint16_t& data) // retuns 0 when a sucsessful transation ocures uint8_t i2c_ch1_pImpL::writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data) // retuns 0 when a sucsessful transation ocures
{ {
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0) if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
@ -86,6 +86,25 @@ uint8_t i2c_ch1_pImpL::writeWord(const uint8_t& address, const uint8_t& reg, con
return 0; return 0;
} }
uint8_t i2c_ch1_pImpL::writeWordWithReg(const uint8_t& address, const uint8_t& reg, const uint16_t& data) // retuns 0 when a sucsessful transation ocures
{
if (ioctl(deviceDescriptor, I2C_SLAVE, address) < 0)
{
errorHandling->handleError(-2,__FILE__);
}
send_buffer[0] = reg;
send_buffer[1] = data >> 8;
send_buffer[2] = data;
if ((write(deviceDescriptor, send_buffer, 3)) != 3)
{
errorHandling->handleError(-3,__FILE__);
}
return 0;
}
uint16_t i2c_ch1_pImpL::readWord(const uint8_t& address, const uint8_t& reg) uint16_t i2c_ch1_pImpL::readWord(const uint8_t& address, const uint8_t& reg)
{ {
uint16_t result = 0 ; uint16_t result = 0 ;

@ -14,7 +14,8 @@ class i2c_ch1_pImpL
uint8_t readByte(const uint8_t& address, const uint8_t& reg); uint8_t readByte(const uint8_t& address, const uint8_t& reg);
uint16_t readWord(const uint8_t& address, const uint8_t& reg); uint16_t readWord(const uint8_t& address, const uint8_t& reg);
uint8_t writeByte(const uint8_t& address, const uint8_t& data); uint8_t writeByte(const uint8_t& address, const uint8_t& data);
uint8_t writeWord(const uint8_t& address, const uint8_t& reg, const uint16_t& data); uint8_t writeWord(const uint8_t& address, const uint8_t& reg, const uint8_t& data);
uint8_t writeWordWithReg(const uint8_t& address, const uint8_t& reg, const uint16_t& data); // retuns 0 when a sucsessful transation ocures
private: private:

Loading…
Cancel
Save