diff --git a/Sensors/MCP4725.c b/Sensors/MCP4725.c index 19513aa..91e2ac6 100644 --- a/Sensors/MCP4725.c +++ b/Sensors/MCP4725.c @@ -43,18 +43,19 @@ void _i2c_MCP4725_setVoltage( T_ULONG output, T_BOOLEAN writeEEPROM ) { I2C.start(MCP4725.address); + if (writeEEPROM) { //I2C.send(MCP4725_CMD_WRITEDACEEPROM); } else { - //I2C.send(MCP4725_CMD_WRITEDAC); + I2C.send( (T_UBYTE)(0x000F & (output>>8)) ); // Upper data bits (D11.D10.D9.D8.D7.D6.D5.D4) + I2C.send( (T_UBYTE)(0x00FF & (output)) ); // Lower data bits (D3.D2.D1.D0.x.x.x.x) } - I2C.send( (T_UBYTE)(0x000F & (output>>8)) ); // Upper data bits (D11.D10.D9.D8.D7.D6.D5.D4) - I2C.send( (T_UBYTE)(0x00FF & (output)) ); // Lower data bits (D3.D2.D1.D0.x.x.x.x) - I2C.end(); + I2C.end(1); + } diff --git a/Sensors/MMA8451.c b/Sensors/MMA8451.c new file mode 100644 index 0000000..953b350 --- /dev/null +++ b/Sensors/MMA8451.c @@ -0,0 +1,36 @@ +#include "oswlib/settings.h" +#include "oswlib/I2C/i2c.h" +#include "oswlib/Sensors/MMA8451.h" + +MMA8451STR MMA8451 = { + MMA8451_SA0_GND, + _i2c_MMA8451_init, + _i2c_MMA8451_ID, +}; + +void _i2c_MMA8451_init(T_UBYTE addr) +{ + MMA8451.address = addr; + I2C.enable(I2C.address); + //I2C.mode(I2C_FAST); +} + +T_UBYTE _i2c_MMA8451_ID( ) +{ + register int j; + + I2C.start(MMA8451.address); + I2C.send( (T_UBYTE) MMA8451_WHO_AM_I ); // Upper data bits (D11.D10.D9.D8.D7.D6.D5.D4) + I2C.end(0); + + //for(j = 0; j < 250; j++); + + I2C.requestFrom(MMA8451.address, 1); + + if(I2C.available()) + return (T_UBYTE)I2C.read(); + else + return 0x99; +} + + diff --git a/Sensors/MMA8451.h b/Sensors/MMA8451.h new file mode 100644 index 0000000..ccbcb05 --- /dev/null +++ b/Sensors/MMA8451.h @@ -0,0 +1,41 @@ +#ifndef OSWARRIOR_MCP4725_H_ + #define OSWARRIOR_MCP4725_H_ + + #define MMA8451_SA0_GND 0x1C // Address if A0 is connected to GND or not connected + #define MMA8451_SA0_VCC 0x1D // Address if A0 is connected to VCC + + #define MMA8451_WHO_AM_I 0x0D // Writes data to the DAC + + #define MMA8451_OUT_X_MSB 0x01 // Writes data to the DAC and the EEPROM (persisting the assigned value after reset) + #define MMA8451_OUT_X_LSB 0x02 // Writes data to the DAC and the EEPROM (persisting the assigned value after reset) + + #define MMA8451_OUT_Y_MSB 0x03 // Writes data to the DAC and the EEPROM (persisting the assigned value after reset) + #define MMA8451_OUT_Y_LSB 0x04 // Writes data to the DAC and the EEPROM (persisting the assigned value after reset) + + #define MMA8451_OUT_Z_MSB 0x05 // Writes data to the DAC and the EEPROM (persisting the assigned value after reset) + #define MMA8451_OUT_Z_LSB 0x06 // Writes data to the DAC and the EEPROM (persisting the assigned value after reset) + + /* + ** =================================================================== + ** Function prototypes + ** =================================================================== + */ + + extern void _i2c_MMA8451_init(T_UBYTE addr); + extern T_UBYTE _i2c_MMA8451_ID(void); + + /* + ** =================================================================== + ** MCP4725 DAC Structure Definition + ** =================================================================== + */ + + typedef struct{ + T_UBYTE address; + void (*init)(T_UBYTE addr); + T_UBYTE (*queryID)(void); + }MMA8451STR; + + extern MMA8451STR MMA8451; + +#endif diff --git a/Sensors/PCF8574.c b/Sensors/PCF8574.c new file mode 100644 index 0000000..5ba4774 --- /dev/null +++ b/Sensors/PCF8574.c @@ -0,0 +1,38 @@ +#include "oswlib/settings.h" +#include "oswlib/I2C/i2c.h" +#include "oswlib/Sensors/PCF8574.h" + +PCF8574STR PCF8574 = { + PCF8574_ADD0, + _i2c_PCF8574_init, + _i2c_PCF8574_write, + _i2c_PCF8574_read, +}; + +void _i2c_PCF8574_init(T_UBYTE addr) +{ + PCF8574.address = addr; + I2C.enable(I2C.address); + //I2C.mode(I2C_FAST); +} + +void _i2c_PCF8574_write(T_UBYTE data) +{ + I2C.start(PCF8574.address); + I2C.send(data); + I2C.end(1); + return; +} + +T_UBYTE _i2c_PCF8574_read( ) +{ + + I2C.requestFrom(PCF8574.address, 1); + + if(I2C.available()) + return I2C.read(); + else + return 0; +} + + diff --git a/Sensors/PCF8574.h b/Sensors/PCF8574.h new file mode 100644 index 0000000..c105b13 --- /dev/null +++ b/Sensors/PCF8574.h @@ -0,0 +1,45 @@ +#ifndef OSWARRIOR_PCF8574_H_ + #define OSWARRIOR_PCF8574_H_ + + + /* + ** =================================================================== + ** Device addressing + ** =================================================================== + */ + + #define PCF8574_ADD0 0x20 // Address if A2 = 0, A1 = 0 y A0 = 0 + #define PCF8574_ADD1 0x21 // Address if A2 = 0, A1 = 0 y A0 = 1 + #define PCF8574_ADD2 0x22 // Address if A2 = 0, A1 = 1 y A0 = 0 + #define PCF8574_ADD3 0x23 // Address if A2 = 0, A1 = 1 y A0 = 1 + #define PCF8574_ADD4 0x24 // Address if A2 = 1, A1 = 0 y A0 = 0 + #define PCF8574_ADD5 0x25 // Address if A2 = 1, A1 = 0 y A0 = 1 + #define PCF8574_ADD6 0x26 // Address if A2 = 1, A1 = 1 y A0 = 0 + #define PCF8574_ADD7 0x27 // Address if A2 = 1, A1 = 1 y A0 = 1 + + /* + ** =================================================================== + ** Function prototypes + ** =================================================================== + */ + + extern void _i2c_PCF8574_init(T_UBYTE); + extern void _i2c_PCF8574_write(T_UBYTE); + extern T_UBYTE _i2c_PCF8574_read(void); + + /* + ** =================================================================== + ** MCP4725 DAC Structure Definition + ** =================================================================== + */ + + typedef struct{ + T_UBYTE address; + void (*init)(T_UBYTE addr); + void (*write)(T_UBYTE); + T_UBYTE (*read)(void); + }PCF8574STR; + + extern PCF8574STR PCF8574; + +#endif