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

I2C send several data

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



Joined: 22 Feb 2009
Posts: 1

View user's profile Send private message Send e-mail MSN Messenger

I2C send several data
PostPosted: Sun Feb 22, 2009 6:34 pm     Reply with quote

Hi everybody, i'm just starting in embedded system.

I have a little problem, i try send one data , and it's size is a int16 bit,
but this code work when i send one data of int8 bit.

Please help me : that my code.

Code:
////// Master///////
#include <16F883.h>
#device ADC=10

#fuses INTRC,NOWDT,MCLR,DEBUG,NOPROTECT,NOLVP,NOBROWNOUT,NOPUT,NOCPD
#use delay(clock=8000000)   // (8000000)8MHz Clock ,31kHz

#use i2c(MULTI_MASTER, sda=PIN_C4,scl=PIN_C3,force_hw )//


//#use i2c(MULTI_MASTER, sda=PIN_C4,scl=PIN_C3, force_hw)

#define  LED         PIN_B4
#define   t 0x08

#define end_1 0xA0     //adresse de l'esclave

int8 val,val1,val2,val3;


//fonction d'envoie de donnée à l'esclave
void envoie_i2c(BYTE end_disp, BYTE adr, int8 data )
{

   i2c_start();               //commence la transmission

   i2c_write(end_disp);           //choisie l'addresse de l'esclave avec qui communiqué

   i2c_write(adr);           //envoie l'adresse de la donnée

   i2c_write(data);           //envoie la donnée

   i2c_stop();                //arrêt de la communication

   delay_ms(100);             //delay     
}

void main()
{
//   set_tris_b(0xFF);
      set_tris_c(0b10111111);

   setup_oscillator(OSC_1MHZ,0);   //(8MHZ,31kHZ)set oscillator to 8Mhz


   val = 200;
   val1 = 155;
   val2 = 230;
   val3 = 45;

/*

   val = 0x1211;
   val1 = 0x3421;
   val2 = 0x4312;
   val3 = 0x2143;

*/

   output_high(LED);
   delay_ms( 500);
   output_low(LED);
   delay_ms(500);

   while(TRUE)
   {       

         envoie_i2c(end_1, t,val );  //envoie une donnée a l'esclave     
               output_high(LED);
         delay_ms(1000);


         envoie_i2c(end_1, t,val1 );  //envoie une donnée a l'esclave     
               output_low(LED);
         delay_ms(1000);

         envoie_i2c(end_1, t,val2 );  //envoie une donnée a l'esclave     
               output_high(LED);
         delay_ms(1000);

         envoie_i2c(end_1, t,val3 );  //envoie une donnée a l'esclave     
               output_low(LED);
         delay_ms(1000);


   }


}



Code:
////////////////////////////////////
/////SLAVE//////////////////////////


#include <16F877.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection

#use delay(clock=4000000)

#use i2c(SLAVE,sda=PIN_C4,scl=PIN_C3,address=0xA0, force_hw)

#include "C:\mplab_projet\flex_lcd_aba.c"



#define  LED         PIN_B3

int state, address, buffer[0x08];          // address and array of Bytes, change size to suit your needs

short bit1 ;

#INT_SSP NOCLEAR
void ssp_interupt (void)
{
clear_interrupt(int_SSP);

   state = i2c_isr_state();
   
   if(state < 0x80)                 //le maitre est entrain d'envoyé des donnée
   {
      if(state == 0)             //premier octet réçue est l'adresse de l'esclave
      {      //   output_high(LED);   
      }
      if(state == 1)                   //second octet réçue est l'adresse
      {         
         address = i2c_read();
      }
      if(state == 2)                   //troisieme octet reçu est la donnée
      {           
        *( (int8* )( &buffer[address + (state-2)]) ) = i2c_read();

     output_high(LED);

     bit1= true;


      }

     
   }

/*   
   if(state == 0x80)                //master is requesting data
   {
      i2c_write (buffer[address]);  //send requested data

   }
*/
   
}


void main()
{
   set_tris_c(0xFF);

   bit1 = false;

   enable_interrupts(INT_SSP);      //enable I2C interrupts
   enable_interrupts(GLOBAL);


   buffer[0x00] = 0b10000000;
   buffer[0x01] = 0b10000001;
   
   buffer[0x02] = 0b01000010;
   buffer[0x03] = 0b01000011;

   buffer[0x04] = 0b10000000;
   buffer[0x05] = 0b10000001;
   
   buffer[0x06] = 0b01000010;
   buffer[0x07] = 0b01000011;

 


   lcd_init();
   delay_ms(10);
   printf(lcd_putc,"\fcommunication I2C\n");//

   printf(lcd_putc,"val :     ");
 

   while(TRUE)
      {   

      delay_ms(100);

      if(bit1) // attend la donnée
      {
         lcd_gotoxy(9,2);
         printf(lcd_putc,"%u ",buffer[address]); //Lu
         
         bit1=false;
      }
      else
      {
      }   

    }



}
thank you
Ttelmah
Guest







PostPosted: Mon Feb 23, 2009 3:08 am     Reply with quote

Haven't looked at your code.
The PIC I2C hardware, _only_ supports 8bit transfers.
However the answer is that you can't send a 16bit value, as _one_ operation. Intead you send the two bytes that make up this value, one after the other.
Look at the make8 function (which allows you to access the separate bytes), and the make16 function (which allows you to put the two bytes back together to make a 16bit value.
A search here will find _hundreds_ of examples, dealing with the same problem over many different transfer media (RS232, I2C etc.).

Best Wishes
Guest








PostPosted: Mon Feb 23, 2009 5:11 am     Reply with quote

Thank you to reply me:

i see what do you mean : i already try , but it send send some value , not exactelly what i send , and i don't realy know , if i code it correctelly. just look after.

master :
#include <16F883.h>
#device ADC=10

#fuses INTRC,NOWDT,MCLR,DEBUG,NOPROTECT,NOLVP,NOBROWNOUT,NOPUT,NOCPD
#use delay(clock=8000000) // (8000000)8MHz Clock ,31kHz

#use i2c(MULTI_MASTER, sda=PIN_C4,scl=PIN_C3,force_hw )//


//#use i2c(MULTI_MASTER, sda=PIN_C4,scl=PIN_C3, force_hw)

#define LED PIN_B4
#define t 0x08

#define end_1 0xA0 //adresse de l'esclave

int16 val,val1,val2,val3;


//fonction d'envoie de donnée à l'esclave
void envoie_i2c(BYTE end_disp, BYTE adr, int16 data )
{

i2c_start(); //commence la transmission

i2c_write(end_disp); //choisie l'addresse de l'esclave avec qui communiqué

i2c_write(adr); //envoie l'adresse de la donnée


i2c_write( data>>8 ); //envoie la donnée

i2c_write( data ); //envoie la donnée

i2c_stop(); //arrêt de la communication

delay_ms(100); //delay


/*
i2c_write( data ); //envoie la donnée

i2c_write( data>>8 ); //envoie la donnée

nb1= data >> 8;


i2c_write( data ); //envoie la donnée

i2c_write( nb1 ); //envoie la donnée

i2c_write( *((int8*)(&data)+0) ); //envoie la donnée

i2c_write( *((int8*)(&data)+1) ); //envoie la donnée
*/

}

void main()
{
// set_tris_b(0xFF);
set_tris_c(0b10111111);

setup_oscillator(OSC_1MHZ,0); //(8MHZ,31kHZ)set oscillator to 8Mhz


val = 2000;
val1 = 1550;
val2 = 2300;
val3 = 455;

output_high(LED);
delay_ms( 500);
output_low(LED);
delay_ms(500);

while(TRUE)
{

envoie_i2c(end_1, t,val ); //envoie une donnée a l'esclave
output_high(LED);
delay_ms(1000);


envoie_i2c(end_1, t,val1 ); //envoie une donnée a l'esclave
output_low(LED);
delay_ms(1000);

envoie_i2c(end_1, t,val2 ); //envoie une donnée a l'esclave
output_high(LED);
delay_ms(1000);

envoie_i2c(end_1, t,val3 ); //envoie une donnée a l'esclave
output_low(LED);
delay_ms(1000);


}


}


////////////////////////////////////////////////////////////////////

slave :
#include <16F877.h>
#device adc=10

#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection

#use delay(clock=4000000)

#use i2c(SLAVE,sda=PIN_C4,scl=PIN_C3,address=0xA0, force_hw)

#include "C:\mplab_projet\flex_lcd_aba.c"



#define LED PIN_B3

int8 state, address ; // address and array of Bytes, change size to suit your needs

int16 buffer[0x08];

short bit1 ;

#INT_SSP NOCLEAR
void ssp_interupt (void)
{
clear_interrupt(int_SSP);

state = i2c_isr_state();

if(state < 0x80) //le maitre est entrain d'envoyé des donnée
{
if(state == 0) //premier octet réçue est l'adresse de l'esclave
{ // output_high(LED);
}
if(state == 1) //second octet réçue est l'adresse
{
address = i2c_read();
}
if(state == 2) //troisieme octet reçu est la donnée
{
*( (int8* )( &buffer[address + (state-2)]) ) = i2c_read();

}

if(state == 3) //troisieme octet reçu est la donnée
{

*( (int8* )( & buffer[address + (state-2) ]) )= i2c_read();

output_high(LED);

bit1= true;

}


}

/*
if(state == 0x80) //master is requesting data
{
i2c_write (buffer[address]); //send requested data

}
*/

}


void main()
{
set_tris_c(0xFF);

bit1 = false;

enable_interrupts(INT_SSP); //enable I2C interrupts
enable_interrupts(GLOBAL);


buffer[0x00] = 0b10000000;
buffer[0x01] = 0b10000001;

buffer[0x02] = 0b01000010;
buffer[0x03] = 0b01000011;

buffer[0x04] = 0b10000000;
buffer[0x05] = 0b10000001;

buffer[0x06] = 0b01000010;
buffer[0x07] = 0b01000011;




lcd_init();
delay_ms(10);
printf(lcd_putc,"\fcommunication I2C\n");//

printf(lcd_putc,"val : ");


while(TRUE)
{

delay_ms(100);

if(bit1) // attend la donnée
{
lcd_gotoxy(9,2);
printf(lcd_putc,"%u ",buffer[address]); //Lu

bit1=false;
}
else
{
}

}



}
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