I'm new to both sdcc and I2C. Trying to write code to read data from 2
I2c devices, master is pic16F887. Main part of code:
void main (void)
{
uint8_t theta, dummy, bitdata, position;
PORTB = 0;
OSCCON = 0b01100001; // 0x7f Set CPU to 4MHz
io_setup();
I2C_Initialise(15) ;
while(1) //repeat endlessly
{
while(PORTB & 1 ) // hold while switch on demo board not
pressed
{
;
}
PORTD = 80;
while((PORTB & 1) == 0) //hold while switch is pressed
{
;
}
//continue when button released
I2C_Start() ;
I2C_Write((0x71 << 1) | 1) ;//send device ID and'read'
bit
bitdata = I2C_Read(1) ; //read first byte
position = I2C_Read(0) ; //read second byte
I2C_Stop() ;
I2C_Start() ;
I2C_Write((0x4f << 1) | 1) ;//send device ID and'read'
bit
theta = I2C_Read(1) ; //read byte 1 / ack
dummy = I2C_Read(0) ; //read byte 2 / nack
I2C_Stop() ;
//PORTA = 0;
PORTD = bitdata ; //3c
msleep(500);
PORTD = position ; //7e
msleep(500);
PORTD = theta; //15 approx
}
}
The device waits until a switch on B0 is pressed and released, then
reads 2 bytes from one device at address 1110001, and 2 (1 plus a dummy)
fron a LM75 temp sensor at 1001111.
The last block of commands just uses PORT D to display the reads
sequentially to see if it all works. Not bothering to display the second
byte of the LM75.
As listed above, it just displays the last reading and ignores the first
two. If the PORTA=0 command is reinserted (it has no purpose in the
prog) everything is displayed OK. Pressing the 'go' button repeats this
result.
With PORTA commented out (or left in) , it will happily display
sequences 'bitrate bitrate theta' , 'position position theta', 'position
bitrate theta' but it refuses with 'bitrate position theta'. Repeat
pressing, repeat output.
I can find no reason for this but am reluctant with my very limited
experience to blame the compiler. . . but could this be the problem?
sdcc is being used as downloaded with no fancy settings.
Any suggestions appreciated. Well, almost . . .
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user