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

WORKING I2C MASTER AND SLAVE LOGGER : MULTI-BYTE
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Ken481



Joined: 27 Mar 2006
Posts: 5

View user's profile Send private message

RE: I2C works fine when ICD is plugged in, but not without
PostPosted: Mon Mar 27, 2006 2:04 pm     Reply with quote

The slave target is in fact running at 20Mhz. The line : #use delay(clock=10000000, restart_wdt) is actually : #use delay(clock=20000000, restart_wdt) in the working version of the program.
domdom



Joined: 06 Sep 2006
Posts: 29

View user's profile Send private message

PostPosted: Fri Sep 08, 2006 5:24 am     Reply with quote

if not mistaken, the master and slave source code only work for eeprom right?
if i trying to communicate between 4 to 5 PIC (16f877a), what modification should i do in the code?
ferrarilib



Joined: 11 Jun 2005
Posts: 38

View user's profile Send private message

PostPosted: Mon Nov 27, 2006 3:54 am     Reply with quote

with 2 pic 877a works fine to 8Mhz with delay time .
but
PIC_SSPCON1 = 0x36; /* 0011 0101 */
isnt 0x36 is 0x35
PIC_SSPCON1 = 0x36; /* 0011 0110 */

Very Happy
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Thu Mar 22, 2007 9:59 am     Reply with quote

I'm using the original code posted on this thread and so far it works great. What I do is a read from the device and it sends back some data. Now what I want to do is add the ability to change the I2C address. A company called www.robot-electronics.co.uk sells a bunch of I2C devices and they all use the same procedure for changing the address, so I thought I would use the same thing so it would work for master code already written. To change the address you write the slave address, then a register number (o) then a command. You do this 3 times then write the new address. Here is the master code I have that works with tall those devices.

Code:

    i2c_start();
    i2c_write(currAddress);     //Address
    i2c_write(0x00);            //Command Register
    i2c_write(0xA0);            //First Command for changing address
    i2c_stop();
    //delay_ms(5);
    i2c_start();
    i2c_write(currAddress);     //Address
    i2c_write(0x00);            //Command Register
    i2c_write(0xAA);            //Second Command for changing address
    i2c_stop();
    //delay_ms(5)
    i2c_start();
    i2c_write(currAddress);     //Address
    i2c_write(0x00);            //Command Register
    i2c_write(0xA5);            //third Command for changing address
    i2c_stop();
    //delay_ms(5)
    i2c_start();
    i2c_write(currAddress);     //Address
    i2c_write(0x00);            //Command Register
    i2c_write(newAddress);      //new address
    i2c_stop();



My question is how to handle this in the slave.

I'm assuming that in slave_buffer[] I will end up with something like [0,0xa0,0,0xaa,0,0xa5,0,"new address"]

Is this what I should be expecting? I guess what I'm confused over is terminology. Documentation always says things like "write this number to this register", but in this case I would just be gathering the numbers in a buffer and then it is up to me to store them in a register or whatever correct? I just want to make sure there is no compiler magic going on that will try to do something with the numbers coming in. By compiler magic I mean like when you set the address in code and the chip only responds to that address with me having to do additional work there.

Thanks
Ringo
[/code]
_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Thu Mar 22, 2007 10:20 am     Reply with quote

I think I figured it out as I was hitting send. It shoudl look more like this. Let me know if I'm way off base.
Code:

        case 0x29:   /* 0010 1001 */
            /* Point to the buffer */
            this_byte = read_i2c();  /* Get the byte from the SSP */
            slave_buffer[buffer_index] = this_byte; /* Put it into the buffer */
            buffer_index++; /* Increment the buffer pointer */
            /* Get the current buffer index */
            /* Subtract the buffer length */
            /* Has the index exceeded the buffer length? */
            if (buffer_index >= RX_BUF_LEN)
            {
               buffer_index = 0; /* Yes, clear the buffer index. */
            }
            debug_state = 2;
            if(addressState==0)
            {
                if((slave_buffer[0]==0) &&(slave_buffer[1]==0xa0))
                    addressState=1;// move to the next state
            }
            else if(addressState==1)
            {
                if((slave_buffer[0]==0) &&(slave_buffer[1]==0xaa))
                    addressState=2;// move to the next state
                else
                    addressState=0;
            }
            else if(addressState==2)
            {
                if((slave_buffer[0]==0) &&(slave_buffer[1]==0xa5))
                    addressState=3;// move to the next state
                else
                    addressState=0;
            }
            else if(addressState==3)
            {
                if(slave_buffer[0]==0)
                    NODE_ADDR=slave_buffer[1];
                addressState=0;
            }
           
           
            break;

_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Mon Mar 26, 2007 7:39 pm     Reply with quote

I'm working from the original code posted to this thread. I'm using a 167876. I'm trying to write the address then 2 bytes. Here is the code from the int
Code:

    switch (temp_sspstat)
    {
        /* Write operation, last byte was an address, buffer is full */
        case 0x09:   /* 0000 1001 */
            /* Clear the receive buffer */
            for (x=0; x<RX_BUF_LEN>= RX_BUF_LEN)
            {
               buffer_index = 0; /* Yes, clear the buffer index. */
            }
            debug_state = 2;
            if(addressState==0)
            {
                debug_state = 21;
                if((slave_buffer[0]==0) &&(slave_buffer[1]==0xa0))
                    addressState=1;// move to the next state
                    break;
            }
            else if(addressState==1)
            {
                debug_state = 22;
                if((slave_buffer[0]==0) &&(slave_buffer[1]==0xaa))
                    addressState=2;// move to the next state
                else
                    addressState=0;
                    break;
            }
            else if(addressState==2)
            {
                debug_state = 23;
                if((slave_buffer[0]==0) &&(slave_buffer[1]==0xa5))
                    addressState=3;// move to the next state
                else
                    addressState=0;
                    break;
            }
            else if(addressState==3)
            {
                debug_state = 24;
                if(slave_buffer[0]==0)
                {
                    NODE_ADDR=slave_buffer[1];
                    PIC_SSPADD = NODE_ADDR;
                    addressState=0;
                    for(i=1;i<5;i++)
                    {
                        output_low(PIN_b6);
                        output_low(pin_b7);
                        delay_ms(250);
                        output_high(pin_b6);
                        output_high(pin_b7);
                        delay_ms(250);
                    }
                }
            }
            output_low(PIN_B6);
            delay_ms(2);
            output_high(PIN_B6);
           
           
            break;



Here is my master code
Code:


    i2c_start();
    i2c_write(currAddress);     //Address
    i2c_write(0x00);            //Command Register
    i2c_write(0xA0);            //Address
    i2c_stop();
    //delay_ms(5);
    i2c_start();
    i2c_write(currAddress);     //Address
    i2c_write(0x00);            //Command Register
    i2c_write(0xAA);            //Address
    i2c_stop();
    //delay_ms(5)
    i2c_start();
    i2c_write(currAddress);     //Address
    i2c_write(0x00);            //Command Register
    i2c_write(0xA5);            //Address
    i2c_stop();
    //delay_ms(5)
    i2c_start();
    i2c_write(currAddress);     //Address
    i2c_write(0x00);            //Command Register
    i2c_write(newAddress);      //new address
    i2c_stop();


From the Printf Statements it appears that the code is never entering case #29. What am I doing wrong? all I get printed out is "debug state = 1" which means it saw the address, but not the bytes after it. Any ideas will be greatly appreciated.
I'm using address 80 (decimal) by the way in case it matters.
Thanks
Ringo
_________________
Ringo Davis
funmix



Joined: 27 Mar 2007
Posts: 33

View user's profile Send private message

PostPosted: Sun Apr 01, 2007 3:36 am     Reply with quote

Hi all,

I found two bugs in the original thread code.

1. When i press 'w', then i key in the value 'askd'. When i press read (tend to read it back), the value display is 'BC4D'. 100% correct for only numbers, not for alphabet. Can i know how to solve this?

2. Using the original thread code is no problem for 16877A. But when i replace both master and slave to 18F4520, it happens that when i press 'w' and key in the value '1234' , the value return is 'FFFF'

p/s: I have changed the special function register for 18F4520.


Appreciate if someone can help to solve this
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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