# HG changeset patch # User kripperger@CL-SEC241-09.cedarville.edu # Date 2012-11-08 16:16:16 # Node ID 4965f5e09d2b94fbfb3b436401994e3954b1465f # Parent 11be7f0ce05b52ae8b40fc6533ff6966e082299b i2c library work diff --git a/slave/slave/config.h b/slave/slave/config.h --- a/slave/slave/config.h +++ b/slave/slave/config.h @@ -13,4 +13,15 @@ #define USART_BAUDRATE 19200 + //I2C Addresses + #define BOARDTEMP_ADDR 0xA5 //THIS VALUE IS WRONG + + #define PRESSURE_ADDR 0xA1 //THIS VALUE IS WRONG + + #define HUMID_ADDR 0xA4 //THIS VALUE IS WRONG + + #define RTC_ADDR 0xA4 //DEBUG //THIS VALUE IS WRONG + + + #endif /* CONFIG_H_ */ \ No newline at end of file diff --git a/slave/slave/lib/i2c.c b/slave/slave/lib/i2c.c --- a/slave/slave/lib/i2c.c +++ b/slave/slave/lib/i2c.c @@ -153,7 +153,7 @@ void i2c_stop(void) Return: 0 write successful 1 write failed *************************************************************************/ -unsigned char i2c_write( unsigned char data ) +unsigned char i2c_writeX( unsigned char data ) { uint8_t twst; @@ -202,3 +202,41 @@ unsigned char i2c_readNak(void) return TWDR; }/* i2c_readNak */ + + + +/************************************************************************* + Write one byte to the I2C device, read is followed by a stop condition + + Return: void +*************************************************************************/ +void i2c_write(unsigned char addr, unsigned char reg, unsigned char data) +{ + i2c_start_wait(addr+I2C_WRITE); // set device address and write mode + i2c_writeX(reg); // write register address + i2c_writeX(data); // write value data to register + i2c_stop(); // set stop condition = release bus + +}/* i2c_write */ + + + +/************************************************************************* + Read one byte from the I2C device, read is followed by a stop condition + + Return: byte read from I2C device +*************************************************************************/ +uint8_t i2c_read(unsigned char addr, unsigned char reg) +{ + uint8_t data; + + i2c_start_wait(addr+I2C_WRITE); // set device address and write mode + i2c_writeX(reg); // write register address + + i2c_rep_start(addr+I2C_READ); // set device address and read mode + data = i2c_readNak(); // read one byte + i2c_stop(); + + return data; + +}/* i2c_read */ \ No newline at end of file diff --git a/slave/slave/lib/i2c.h b/slave/slave/lib/i2c.h --- a/slave/slave/lib/i2c.h +++ b/slave/slave/lib/i2c.h @@ -43,7 +43,7 @@ unsigned char i2c_rep_start(unsigned cha void i2c_start_wait(unsigned char addr); //Issues a start condition and sends address and transfer direction. If device is busy, use ack polling to wait until device ready //addr is the address and transfer direction of I2C device -unsigned char i2c_write(unsigned char data); //Send one byte to I2C device +unsigned char i2c_writeX(unsigned char data); //Send one byte to I2C device //data is the byte to be transfered //Returns 0 write successful //Returns 1 write failed @@ -54,12 +54,14 @@ unsigned char i2c_readAck(void); //read unsigned char i2c_readNak(void); //read one byte from the I2C device, read is followed by a stop condition //Returns byte read from I2C device -unsigned char i2c_read(unsigned char ack); //read one byte from the I2C device. Implemented as a macro, which calls either i2c_readAck or i2c_readNak -#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak(); +unsigned char i2c_readX(unsigned char ack); //read one byte from the I2C device. Implemented as a macro, which calls either i2c_readAck or i2c_readNak +#define i2c_readX(ack) (ack) ? i2c_readAck() : i2c_readNak(); //When ack is 1: send ack, request more data from device //When ack is 0: send nak, read is followed by a stop condition //Returns byte read from I2C device +void i2c_write(unsigned char addr, unsigned char reg, unsigned char data); //Does complete write. Can be used as stand alone function. +unsigned char i2c_read(unsigned char addr, unsigned char reg); #endif /* I2C_H_ */ \ No newline at end of file diff --git a/slave/slave/slave.c b/slave/slave/slave.c --- a/slave/slave/slave.c +++ b/slave/slave/slave.c @@ -35,17 +35,29 @@ void micro_setup() int main(void) { // Initialize - micro_setup(); - led_configure(); - //serial_setup(); // Config serial ports - uint8_t moduleID = io_getModuleId(); //Slave Module ID from rotary dip switch + micro_setup(); // Generic microcontroller config options + led_configure(); // + i2c_init(); // Setup I2C + //serial_setup(); // Config serial ports + uint8_t moduleID = io_getModuleId(); // Slave Module ID from rotary dip switch + + + uint8_t temp; //DEBUG + while(1) { - //serial_SendCommand('0','A',0,0); + //serial_SendCommand('0','A',0,0); //DEBUG: EXAMPLE + + + + i2c_write(RTC_ADDR, 0x05, 0x3A); //DEBUG: EXAMPLE + + temp = i2c_read(RTC_ADDR, 0x24); //DEBUG: EXAMPLE + }