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

One of the Slave codes and the Master code

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



Joined: 19 May 2009
Posts: 21

View user's profile Send private message

One of the Slave codes and the Master code
PostPosted: Tue May 19, 2009 2:42 pm     Reply with quote

SLAVE CODE
Code:

#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#elif defined(__PCH__)
#include <18F4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#endif

#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=194,force_hw)

BYTE address, buffer[0x10];
 BYTE incoming, state;

#INT_SSP
void  SSP_isr(void)
{
output_high(pin_d6);

clear_interrupt(int_SSP);
 

   state = i2c_isr_state();

   if(state < 0x80)                     //Master is sending data
   {
      incoming = i2c_read();
      if(state == 1)                     //First received byte is address
         address = incoming;
      if(state == 2)                     //Second received byte is data
         buffer[address] = incoming;
   }
   if(state == 0x80)                     //Master is requesting data
   {
      i2c_write(79);
   }
}

void main ()
{
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_SSP);

   while (TRUE)
   {
   set_tris_c(225);
   output_high(pin_d4);
   delay_ms(120);
   output_low(pin_d4);
   delay_ms(120);
   }
}

MASTER CODE
Code:

/#include <E:\My Files\My_custom_i2c_library.h>

#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#elif defined(__PCH__)
#include <18F4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#endif


#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3,force_hw)
/*
BOOLEAN slave_ready(BYTE );

void write_to_slave(BYTE, BYTE);

BYTE read_from_slave(BYTE);

void ssp_interrupt ();
*/

byte address1 = 194;
/*
byte address2 = 196;
byte address2 = 198;
byte address2 = 200;
byte address2 = 202;
byte address2 = 204;
byte address2 = 206;
byte address2 = 208;
byte address2 = 210;
byte address2 = 212;
*/
byte data1 =0;
/*
byte data2 =0;
byte data3 =0;
byte data4 =0;
byte data5 =0;
byte data6 =0;
byte data7 =0;
byte data8 =0;
byte data9 =0;
byte data10 =0;
*/

BOOLEAN slave_ready(BYTE );

void write_to_slave(BYTE, BYTE);

BYTE read_from_slave(BYTE);

void ssp_interrupt ();

VOID MAIn (void)
{
while (true)
{
data1 = read_from_slave(address1);
printf("data read from sensor 1 is %4u",(int32) data1);
delay_ms(120);
}
/*
do
   {
 
   while(!ext_eeprom_ready());
      i2c_start();
      state = i2c_write((0xa0|(BYTE)(address>>7))&0xfe);
      if(state == 0)
      {
         state = i2c_write(address);
         if(state == 0)
         {
            i2c_start();
            state = i2c_write((0xa0|(BYTE)(address>>7))|1);
            if(state == 0)
            {
               data=i2c_read(0);
               i2c_stop();
               printf("data received is %4u", data);
            }
         }
      }
      if(state == 1)
      {
         i2c_stop();
      }
   }while(state != 0);
   */
}


BOOLEAN slave_ready(BYTE address)
{
   int ack;
   i2c_start();            // If the write command is acknowledged,
   ack = i2c_write(address);  // then the device is ready.
   i2c_stop();
   return !ack;
}

void write_to_slave(BYTE address, BYTE data)
{
   while(!slave_ready(address))
   i2c_start();
   i2c_write(address&0xfe);
   i2c_write(data);
   i2c_stop();
}

BYTE read_from_slave(BYTE address)
{
   BYTE data;

  // while(!slave_ready(address))
   i2c_start();
   i2c_write(address|1);
   data=i2c_read(0);
   i2c_stop();
   return(data);
}

void ssp_interrupt ()
{
   BYTE incoming, state,buffer[16],address;

   state = i2c_isr_state();

   if(state < 0x80)                     //Master is sending data
   {
      incoming = i2c_read();
      if(state == 1)                     //First received byte is address
         address = incoming;
      if(state == 2)                     //Second received byte is data
         buffer[address] = incoming;
   }
   if(state == 0x80)                     //Master is requesting data
   {
      i2c_write(buffer[address]);
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 19, 2009 2:55 pm     Reply with quote

Why did you start a new thread to post your code ?
You have an existing thread on i2c slave problems.
You should post the code in that thread.
Guest








PostPosted: Tue May 19, 2009 3:36 pm     Reply with quote

I will soon post the original versions of the code that did not work. That does not have any threading issues and yet did not work
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 19, 2009 3:51 pm     Reply with quote

I don't mean 'thread' with regard to programming.
I mean 'thread' as a Topic posted on this forum.
You don't need 2 topics about i2c slaves.

Put all of your new i2c posts in the original topic, here:
http://www.ccsinfo.com/forum/viewtopic.php?t=38961
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