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 slave transmitting data
Goto page Previous  1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PICdawg



Joined: 13 Dec 2007
Posts: 17

View user's profile Send private message

PostPosted: Fri Dec 14, 2007 1:26 pm     Reply with quote

Yes, I forgot that the s/w implementation is only for the master.

Also, I believe the problem in my implementation may be that my read did not specify the NAK parameter. After some review of the i2c spec, pic datasheet and the ccs manual, I think I understand why it should be there and when. I'll try that first!

Is it fair to say that if you are reading multiple data bytes, that your code needs to add the NAK parameter of 0 just to the last read call? I'm assuming this is how the master-receiver signals the slave that the read sequence is finished.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 14, 2007 5:50 pm     Reply with quote

Quote:
Is it fair to say that if you are reading multiple data bytes, that your code needs to add the NAK parameter of 0 just to the last read call?

That's correct.
PICdawg



Joined: 13 Dec 2007
Posts: 17

View user's profile Send private message

PostPosted: Mon Dec 17, 2007 10:31 am     Reply with quote

Thanks. I did add the NAK parameter to my master-receiver code and my demo program(s) came to life! My slave is running essentially the example slave program. Very simple master program.

I will try to use robotam's prgrams listed on the 1st page of this discussion and see if I can get to work in my proto environment using two 876a's.
robotam



Joined: 13 Nov 2007
Posts: 15

View user's profile Send private message

PostPosted: Mon Dec 17, 2007 10:36 am     Reply with quote

PICdawg wrote:
Thanks. I did add the NAK parameter to my master-receiver code and my demo program(s) came to life! My slave is running essentially the example slave program. Very simple master program.

I will try to use robotam's prgrams listed on the 1st page of this discussion and see if I can get to work in my proto environment using two 876a's.


Hi PICdawg

can you please explain what did you change in your code?
PICdawg



Joined: 13 Dec 2007
Posts: 17

View user's profile Send private message

PostPosted: Mon Dec 17, 2007 7:51 pm     Reply with quote

See my private email to you. Rather than fill up this with the code I answered your question and sent you the code I used for my demo.
robotam



Joined: 13 Nov 2007
Posts: 15

View user's profile Send private message

PostPosted: Thu Dec 27, 2007 2:39 am     Reply with quote

Hello,
I changed my dspic with a 18f as a slave and it works perfectly.i guess there is some modifications or configuration in the dspic.
hansimg



Joined: 24 May 2010
Posts: 8

View user's profile Send private message

PostPosted: Mon May 24, 2010 1:31 pm     Reply with quote

Hey guys, I'm trying to communicate 2 pics 16f877, so I found this topic, and do exactly like you said. But it didn't work. When I try to simulate it in Proteus, the SCL line is kept in 0 and the program does nothing.

Here my master code:
Code:

#include <16F877.H>
#fuses XT, NOWDT, PROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
   
//====================================
void main()
{
int8 data;

// Write the letter 'B' to the slave board.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_write('B');
i2c_stop();

// Read from the slave board and display the data.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
printf("read %c \n\r", data);
     
while(1);     
}     


And the slave code:

Code:
///////////////////////////////////////////////////////////////////////////
////                         EX_SLAVE.C                                ////
////                                                                   ////
////  This program uses the PIC in I2C slave mode to emulate the       ////
////  24LC01 EEPROM. You can write to addresses 00h to 0Fh with it.    ////
////                                                                   ////
////  This program is to be used in conjunction with the ex_extee.c    ////
////  sample.  Use the "#include <2402.C>" or "#include <2401.c>".     ////
////  Only 16 bytes of address space are implemented, however.         ////
////                                                                   ////
////  If using a compiler version before 2.639 add "*0x14 = 0x3E;" to  ////
////  the begining of main(), and add "NOFORCE_SW" as the last         ////
////  parameter in the #use i2c directive.                             ////
////                                                                   ////
////  Jumpers:                                                         ////
////     PCM,PCH    pin C7 to RS232 RX, pin C6 to RS232 TX             ////
////                                                                   ////
////  This example will work with the PCM and PCH compilers.  The      ////
////  following conditional compilation lines are used to include a    ////
////  valid device for each compiler.  Change the device, clock and    ////
////  RS232 pins for your hardware if needed.                          ////
///////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2003 Custom Computer Services           ////
//// This source code may only be used by licensed users of the CCS    ////
//// C compiler.  This source code may only be distributed to other    ////
//// licensed users of the CCS C compiler.  No other use,              ////
//// reproduction or distribution is permitted without written         ////
//// permission.  Derivative programs created using this software      ////
//// in object code form are not restricted in any way.                ////
///////////////////////////////////////////////////////////////////////////

#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 <18F452.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=0xa0)

BYTE address, buffer[0x10];

#INT_SSP
void ssp_interupt (){
BYTE incoming, state;

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]);
    }
}

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

   while (TRUE) {}
}


Please, help.

Cya
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 24, 2010 1:42 pm     Reply with quote

Put 4.7K pull-up resistors on SCL and SDA.
hansimg



Joined: 24 May 2010
Posts: 8

View user's profile Send private message

PostPosted: Mon May 24, 2010 1:51 pm     Reply with quote

I already do it. =/
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 24, 2010 2:07 pm     Reply with quote

Post your Proteus schematic on a free image hosting website,
and then post a link to the image in this thread.
hansimg



Joined: 24 May 2010
Posts: 8

View user's profile Send private message

PostPosted: Mon May 24, 2010 2:17 pm     Reply with quote

PCM_PROGRAMMER thanx for your attention, this is my schematic
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Mon May 24, 2010 2:40 pm     Reply with quote

No clock source shown on either chip.
You are specifying 'XT', with a 4MHz delay, which implies an external 4MHz crystal for the master, and HS, and a 20MHz delay, which implies an external 20MHz crystal for the slave. Where are these?.

Best Wishes
hansimg



Joined: 24 May 2010
Posts: 8

View user's profile Send private message

PostPosted: Mon May 24, 2010 3:00 pm     Reply with quote

I Change my schematic to this:


But it's still don't works... =/
X1 -> 4mhz and X2->20mhz
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 24, 2010 3:19 pm     Reply with quote

It could be that Proteus doesn't correctly simulate a software i2c master.
Try adding FORCE_HW to the master:
Quote:

#use i2c(Master, sda=PIN_C4, scl=PIN_C3, FORCE_HW)

The slave code doesn't need it. The slave always uses hardware i2c.


If that doesn't fix the problem, then it could be caused by your version
of Proteus.
hansimg



Joined: 24 May 2010
Posts: 8

View user's profile Send private message

PostPosted: Mon May 24, 2010 3:40 pm     Reply with quote

I add force_hw to the master, then the problem was change, now the scl don't go to slow, and the program run until the end, but it's not writing on the slave and neither read.

I receive it on therminal:
"Read -1"

On debug "data" assumes 0xff value
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4  Next
Page 3 of 4

 
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