CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

declaration, combining bytes or formating error?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

declaration, combining bytes or formating error?
PostPosted: Thu Mar 16, 2006 5:49 pm     Reply with quote

I'm reading the data output from an SRF08 in 2 bytes, the high byte first and the low byte second. I'm combining the two bytes and saving the value in an int16 array[17]. When I print the values using "%lu" to format the output I only get the low byte or only printing one byte, I'm not sure which.

Here is my code:
Code:
#include <16F877A.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

main()
{
int i;
int16 range[17];
while(1)
 {
  i2c_start();                    //initate start condition
  i2c_write(0xE0);                //device address
  i2c_write(0x00);                //register address
  i2c_write(0x50);                //set units to inches
  i2c_stop();

  delay_ms(105);                   //wait for returning ping

  i2c_start();                    //initiate a new start condition
  i2c_write(0xE0);                //device address
  i2c_write(0x02);                //address of high byte register
  i2c_start();
  i2c_write(0xE1);                //device address but read
  for (i=0;i<17;i++)
  {
  range[i] =i2c_read(1) << 8;         //read first byte and shift left
  if(i<16)
  range[i] += i2c_read(1);           //read second byte and add to 1st
  else
  range[i] += i2c_read(0);
  }
  i2c_stop();
  for (i=0;i<17;i++)
  printf("range in inches = %lu\n\r", range[i]);
  delay_ms(1000);
 }
}


Thanks, Harry
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 17, 2006 1:36 am     Reply with quote

You should be able to fix it by casting the i2c_read(1) result to a 16-bit
value before doing the left shift. I think the problem is that CCS
can't be depended upon to do automatic type promotions, as is done
by MSVC, etc. Sometimes CCS does them and sometimes they don't.
Quote:

range[i] =(int16)i2c_read(1) << 8;
if(i<16)
range[i] += i2c_read(1);
else
range[i] += i2c_read(0);
}
i2c_stop();
for (i=0;i<17;i++)
printf("range in inches = %lu\n\r", range[i]);
delay_ms(1000);
}

If you want to be really safe, you could cast the other two i2c_read()
lines to (int16) as well.
-------------------------------------

In the other thread with the translated driver code, I initially had
a cast to int16 in there, but I decided check the .LST file and see if
it worked without it, and it did, so I took it out of the code that I posted.
But what if your compiler version needs it ? If you want to be really
safe you could change the SRF08 driver code to this:
Code:
range[i] = ((int16)i2c_read()) << 8;   
range[i] +=  (int16)i2c_read(0);     

See if it improves your results.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Mar 17, 2006 10:08 am     Reply with quote

Quote:
range[i] =i2c_read(1) << 8; //read first byte and shift left

This line will read data from the i2c bus and then shift it eight places which will then be all 0's. This is _then_ stored in range[].

Try reading the data, storing it into range[] and then shifting range[]. Now read the second byte from the i2c bus and store that into the lower half of range[].

Ronald
Harry Mueller



Joined: 17 Oct 2005
Posts: 116

View user's profile Send private message

PostPosted: Fri Mar 17, 2006 1:08 pm     Reply with quote

Thanks PCM and Ronald,

I've tried both solutions and they both work!

Regards, Harry
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group