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 CCS Technical Support

Help in using I2C

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







Help in using I2C
PostPosted: Sat Aug 08, 2009 3:30 am     Reply with quote

Hello every one...

I want a help in I2C, I tried to make a connection between PIC16F877A and 24AA26 by I2C, to save a float data on it, but there is an error.
And thanks alot....

Please check the following code :

Code:
#use i2c(master,scl=PIN_C3,sda=PIN_C4,force_hw)
#INT_SSP

void I2C_wrt(long int address,float data1)
   {

   i2c_start();
   i2c_write(0b00000000); // call address for slave (in general)
   i2c_write(address);
   i2c_write(data1);  // Data1 to device
   i2c_stop();// Stop condition

   }


float I2C_rd(void)
   {

   float data;
   i2c_start();
   i2c_write(0b00000000);   // general call address for slave
   i2c_write(0b00000000);
   i2c_start();   // send a restart sequence
   i2c_write(0b00000001);
   data = i2c_read(0);   // Data to PIC16F877
   i2c_stop();   // Stop condition
   return(data);

   }
adfkk
Guest







Answer
PostPosted: Sat Aug 08, 2009 3:52 am     Reply with quote

Your data type to be stored is float which I think is 4 bytes long.
Thus, you have to divide your signle float data into four 8-bit variable(for example, int).

After you have divided the float into 4 int, store them in 4 different EEPROm address.

Hope this helps
Hussien
Guest







PostPosted: Sat Aug 08, 2009 4:49 am     Reply with quote

Thank for your reply...

I tried to convert the float into int but no result...the dividing code is:
Code:
float *data1;   
int data2[],data3[],data4[],data5[];
   int j;
for (j=0;j<=8;j++)
      {
      data2[j]=data1[j];
      }
   for (j=9;j<=16;j++)
      {
      data2[j]=data1[j];
      }
   for (j=17;j<=24;j++)
      {
      data2[j]=data1[j];
      }
      for (j=25;j<=32;j++)
      {
      data2[j]=data1[j];
      }


I don't know if the last code is right; because I'm beginner in ccs...

thanks
Ttelmah
Guest







PostPosted: Sat Aug 08, 2009 7:15 am     Reply with quote

No.
Nothing to do with CCS. This wouldn't work in any C....
If you have a pointer to a value, and increment it, it advances to the next value.
To use pointer arithmetic to get the bytes, you need to convert the pointer type, it an int*.
Then, you have declared data2 (and the other data variables), as a pointer to an integer (in C, an array, and a pointer, are basically the same thing), but have not allocated any storage to hold the array. You then start writing vues into the addresses held by these pointers, destroying random areas of your memory...

There are multiple ways to do this:

1) Look at the internal 'make8' and 'make32' functions. These allow you to extract the 8bit values, and put them back together again.

2) Use pointer arithmetic, but convert the pointer types.
Code:

float val;

int8 * ptr;

ptr=&val;


You can then access ptr[0]...ptr[3] as the four bytes of 'val'.

3) Use a union. A search here will find a lot of examples.
Advantage is that this is nearly 'generic' C, rather than make8, being CCS specific.

Best Wishes
Hussien
Guest







PostPosted: Sat Aug 08, 2009 10:31 am     Reply with quote

Thank alot....
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