Aaron Replogle

Reading from an IIC slave in Master Receive Mode - HELP!

Discussion created by Aaron Replogle on Nov 5, 2006
Latest reply on Nov 6, 2006 by Aaron Replogle
I am struggling with this a lot. I've been able to write to my slave using the master tx mode, but reading from it is a real struggle. Here is what is happening:

I write a command to it, then do a repeated start signal so that I can give it its address with a read bit. Then, I go over to master receive mode and attempt to read two bytes. I am following the directions in the MC9S12E128 data sheet (that's what I'm using). Every time I try to read, the first byte i get is the slave address(why??). Then I get the correct LSB, followed by 0xFF (NOT the correct MSB).

If anyone knows the basic commands for this, please help!! I am using the IBIF flag to denote end of byte, clearing it, and going again. Here is my code:

Code:
void READ_FUEL_GAUGE (byte cmd) {MSBYTE = 0xAA;LSBYTE = 0xAA;SLAVE_ADDR = 0xFF;__asm BSET IBCR,#$80; //enables IIC__asm BSET IBCR,#$30; //enables master transmit mode__asm MOVB #$40,IBFD; //sets freq to 100 kwhile ((IBSR & 0x20) == 0x20); //while bus is busy....__asm MOVB #$8E,IBDR; //initiate start/slave addresswhile ((IBSR & 0x01) == 0x01); //wait for ackwhile ((IBSR & 0x20) == 0x00); //wait to ensure we have the buswhile ((IBSR & 0x02) == 0x00); //ensure previous byte is done__asm BSET IBSR,#$02; //clear the flag__asm MOVB cmd,IBDR; //send bytewhile ((IBSR & 0x01) == 0x01); //wait for ackwhile ((IBSR & 0x02) == 0x00);__asm BSET IBSR,#$02;__asm BSET IBCR,#$04; //restart signal__asm MOVB #$8F,IBDR; //initiate start/slave addresswhile ((IBSR & 0x01) == 0x01); //wait for ackwhile ((IBSR & 0x02) == 0x00);__asm BSET IBSR,#$02;__asm BCLR IBCR,#$10; //sets master rcv modeSLAVE_ADDR = IBDR; //why do i get slave addr here—–˜while ((IBSR & 0x02) == 0x00); //had these before__asm BSET IBSR,#$02;__asm BSET IBCR,#$08;LSBYTE = IBDR;while ((IBSR & 0x02) == 0x00);__asm BSET IBSR,#$02;__asm BCLR IBCR,#$20; //initiate stop conditionMSBYTE = IBDR;__asm BCLR IBCR,#$80; //turn off IIC}

 
(Alban moved from CW board + code format)

Message Edited by Alban on 2006-11-05 11:26 AM

Outcomes