Changeset - 4965f5e09d2b
[Not reviewed]
default
0 4 0
kripperger@CL-SEC241-09.cedarville.edu - 13 years ago 2012-11-08 16:16:16
kripperger@CL-SEC241-09.cedarville.edu
i2c library work
4 files changed with 70 insertions and 7 deletions:
0 comments (0 inline, 0 general)
slave/slave/config.h
Show inline comments
 
@@ -4,13 +4,24 @@
 
 * Created: 10/25/2012 10:00:09 PM
 
 *  Author: mkanning
 
 */
 
 
 
 #ifndef CONFIG_H_
 
 #define CONFIG_H_
 
 
 #define F_CPU 11059200
 
 #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
slave/slave/lib/i2c.c
Show inline comments
 
@@ -144,25 +144,25 @@ void i2c_stop(void)
 
 
}/* i2c_stop */
 
 
 
 
/*************************************************************************
 
  Send one byte to I2C device
 
  
 
  Input:    byte to be transfered
 
  Return:   0 write successful 
 
            1 write failed
 
*************************************************************************/
 
unsigned char i2c_write( unsigned char data )
 
unsigned char i2c_writeX( unsigned char data )
 
{	
 
    uint8_t   twst;
 
    
 
	// send data to the previously addressed device
 
	TWDR = data;
 
	TWCR = (1<<TWINT) | (1<<TWEN);
 
 
	// wait until transmission completed
 
	while(!(TWCR & (1<<TWINT)));
 
 
	// check value of TWI Status Register. Mask prescaler bits
 
	twst = TW_STATUS & 0xF8;
 
@@ -193,12 +193,50 @@ unsigned char i2c_readAck(void)
 
 Read one byte from the I2C device, read is followed by a stop condition 
 
 
 
 Return:  byte read from I2C device
 
*************************************************************************/
 
unsigned char i2c_readNak(void)
 
{
 
	TWCR = (1<<TWINT) | (1<<TWEN);
 
	while(!(TWCR & (1<<TWINT)));
 
	
 
    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
slave/slave/lib/i2c.h
Show inline comments
 
@@ -34,32 +34,34 @@ unsigned char i2c_start(unsigned char ad
 
	//addr is the address and transfer direction of I2C device
 
	//Returns   0   device accessible 
 
	//Returns   1   failed to access device 
 
 
unsigned char i2c_rep_start(unsigned char addr);	//Issues a repeated start condition and sends address and transfer direction
 
	//addr is the address and transfer direction of I2C device
 
	//Returns   0   device accessible
 
	//Returns   1   failed to access device
 
	
 
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
 
 
unsigned char i2c_readAck(void);	//read one byte from the I2C device, request more data from device
 
	//Returns byte read from I2C device
 
 
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
slave/slave/slave.c
Show inline comments
 
@@ -26,29 +26,41 @@
 
 
void micro_setup()
 
{
 
	// Generic microcontroller config options
 
	
 
}
 
 
 
 
int main(void)
 
{
 
	// Initialize
 
	micro_setup();
 
	led_configure();
 
	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
 
		
 
		
 
		
 
    }
 
	
 
	return 0;
 
}
 
\ No newline at end of file
0 comments (0 inline, 0 general)