Hi Ryan, Thanks for the help on this. The issue was the I2c device did not acknowledge the data with an acknowledge bit so i just bit bashed it to get around the issue. The code if anyone is interested: #include #define I2C_MASTER_DIR P1DIR #define I2C_MASTER_OUT P1OUT #define I2C_MASTER_IN P1IN //port pins #define IC2_MASTER_SCL BIT7 #define I2C_MASTER_SDA BIT6 unsigned char ok,ok1,rxdata[5]={0,0,0,0,0}; unsigned char *xdata; int j,h,x; unsigned char buf; //Declarations //sends a start condition //will set sda and scl high and delay before start void i2cm_start( void ); //send stop condition //will set sda low before delay and stop void i2cm_stop( void ); //Output one byte //assumes sda and scl low and leaves sda, scl low if ack. //returns true if no ack from device unsigned char i2cm_out( register unsigned int data ); //input count of bytes into buf[ ] //Assumes scl low and leaves scl low //sends ack to device until last byte then no ack void i2cm_in( unsigned char *d1, int count ); void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer PM5CTL0 &= ~LOCKLPM5; //Unlock the IO while(1) { for(h=0;h<10;h++); /******WRITE*1*********/ i2cm_start(); i2cm_out(0x80); //Slave Address (8Bit not 7Bit like I2C device)address<<1 i2cm_out(0x00); //Register Address i2cm_out(0x0F); //Data i2cm_stop(); /*******READ*1*********/ i2cm_start(); i2cm_out(0x80+0x01); //Slave Address (8Bit not 7Bit like I2C device)address<<1 i2cm_in( buf, 1 ); //input the config byte to 'buf' i2cm_stop(); /*************************/ } //Send data byte out on bang i2c, return false if ack //Assumes start has been set up or a next byte //so both lines are assumed low // **Lower byte of data is sent** unsigned char i2cm_out( register unsigned int data ) { volatile unsigned int i= 0; //will be register //output eight bits of 'data' for(i=0 ; i < 8; ++i ) { //send the data bit if( data & 0x80 ) I2C_MASTER_OUT|= I2C_MASTER_SDA; else I2C_MASTER_OUT&= ~I2C_MASTER_SDA; //Set Clock High I2C_MASTER_OUT|= IC2_MASTER_SCL; //Set Clock Low asm(" NOP"); asm(" NOP"); //delay( 0x4 ); I2C_MASTER_OUT&= ~IC2_MASTER_SCL; //shift next data bit data= data << 1; } I2C_MASTER_DIR&= ~I2C_MASTER_SDA; //Set Clock High I2C_MASTER_OUT|= IC2_MASTER_SCL; //get the ack bit and leave sda in last state unsigned char ack= I2C_MASTER_IN & I2C_MASTER_SDA; if( ack ) I2C_MASTER_OUT|= I2C_MASTER_SDA; else I2C_MASTER_OUT&= ~I2C_MASTER_SDA; //take the pin back for output I2C_MASTER_DIR|= I2C_MASTER_SDA; //Set Clock Low I2C_MASTER_OUT&= ~IC2_MASTER_SCL; return ack; } //Assumes the IC2_MASTER_SCL is low void i2cm_in( unsigned char *d1, int count ) { unsigned int i= 0; unsigned char data; x=count;//unsigned char data; while(x) { data= 0; I2C_MASTER_DIR&= ~I2C_MASTER_SDA; for( i=0; i < 8; i++ ) { //Set Clock High I2C_MASTER_OUT|= IC2_MASTER_SCL; //shift the bit over data= data << 1; if( I2C_MASTER_IN & I2C_MASTER_SDA ) data|= 0x01; //Set Clock Low I2C_MASTER_OUT&= ~IC2_MASTER_SCL; } //put the input data byte into the buffer, inc buffer pointer //*bufff= data; //bufff++; //No Ack after last byte if(x) I2C_MASTER_OUT&= ~I2C_MASTER_SDA; else I2C_MASTER_OUT|= I2C_MASTER_SDA; //take sda to output ack I2C_MASTER_DIR|= I2C_MASTER_SDA; //Set Clock High I2C_MASTER_OUT|= IC2_MASTER_SCL; //Set Clock Low asm(" NOP"); asm(" NOP"); //delay( 0x4 ); I2C_MASTER_OUT&= ~IC2_MASTER_SCL; *d1= data; d1++; x=x-1; } } void i2cm_start( void ) { I2C_MASTER_DIR|=I2C_MASTER_SDA; I2C_MASTER_DIR|=IC2_MASTER_SCL; I2C_MASTER_OUT|= I2C_MASTER_SDA; I2C_MASTER_OUT|= IC2_MASTER_SCL; asm(" NOP");//for(h=0;h<21;h++); asm(" NOP"); asm(" NOP"); //delay( 0x20 ); I2C_MASTER_OUT&= ~I2C_MASTER_SDA; asm(" NOP");//for(h=0;h<21;h++); asm(" NOP"); asm(" NOP"); //delay( 0x20 ); I2C_MASTER_OUT&= ~IC2_MASTER_SCL; } //Assumes the clock is low void i2cm_stop( void ) { I2C_MASTER_OUT&= ~I2C_MASTER_SDA; asm(" NOP");//for(h=0;h<21;h++); asm(" NOP"); asm(" NOP"); // delay( 0x250 ); I2C_MASTER_OUT|= IC2_MASTER_SCL; asm(" NOP");//for(h=0;h<21;h++); asm(" NOP"); asm(" NOP"); //delay( 0x250 ); I2C_MASTER_OUT|= I2C_MASTER_SDA; } Cheers Ed
↧