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

PIC as I2C slave. HW problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
fuzzy



Joined: 26 Feb 2005
Posts: 64

View user's profile Send private message

PIC as I2C slave. HW problem
PostPosted: Thu Mar 03, 2005 12:59 pm     Reply with quote

Hi, I'm trying to connect 2 PIC as master and slave on an I2c bus. master has to control 2 slaves a I2c device and the other PIC. when I connect the pic, self powered on another board, pullup voltage of the master goes down to 1,3V from 5V and the master can't use the bus. this bus instead is ok before connecting ather pic.

there is a particular way to set tris port? Should i pullup also the second pic pins?
another question, how do i set slave addres? wich sequence of bit should i use? is the last bit important? i know it's used to set write or read mode.
thanks
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Mar 03, 2005 3:34 pm     Reply with quote

Make sure the pins are configured as Inputs. Being pulled to 1.3v would indicate that there is some sort of contention between the two PIC's. I don't mess with the Tris setting. I just let the compiler do the work for me. As long as you have one resistor on each line, SCL & SDA, the bus should work fine. Make each resistor about 4K to start. Unless you have a long distance between the two PIC's then you might need a bus driver to make sure the signals are clean. The LTC1694-1, by Linear, is a good one.

The Slave address is set in the #use_i2c line. Make sure it's an Even number. Make sure that the Grounds, of both power supplies, are tied together. If they are isolated from each other the PIC's will have a hard time communicating with each other.

Ronald
fuzzy



Joined: 26 Feb 2005
Posts: 64

View user's profile Send private message

PostPosted: Thu Mar 03, 2005 3:49 pm     Reply with quote

I made a lot of attempts: TRISC as input or output also port C3 and C4 set to 1. but the problem is the same. i think it's a software setting because connection is very short(5cm). the power supply have got same ground.... could it be a problem if each ground is protected by a diode?

used another pic16f876 as slave and i noticed in this case only one PIN goes to 1,7V the other PIN is 5v.

could it be a failed pic? they are both new devices!!!
valemike
Guest







PostPosted: Thu Mar 03, 2005 5:39 pm     Reply with quote

Can you try holding the slave PIC in reset by holding MCLR low (convenient if you have a pushbutton)? Then find out if your voltages still drop. If the voltages remain fine, then it's definitely a slave PIC TRIS setting, it's gotta be! (By holding the PIC's MCLR line low, you are guaranteed that the pins are inputs.)

Don't know about your diode thing.
fuzzy



Joined: 26 Feb 2005
Posts: 64

View user's profile Send private message

PostPosted: Thu Mar 03, 2005 6:35 pm     Reply with quote

if I hold mclr low voltage rises to 5V. what do you suggest me to do? i tryed all possible settings for TRISC.3 e TRISC.4
wich is the correct setting? should I set 1, PORTC.3 and 4???
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Mar 04, 2005 9:27 am     Reply with quote

I've done a 'value = input_c()' to set the pins to be inputs before. That way I don't need to mess with the set_tris_c(). The pins will remain in that state until changed.

You say that you are protecting the grounds with diodes? I'm not sure how you are doing that but the grounds of Each supply need to be connected directly with nothing in line (resistors or diodes).

Do you have programs running in each pic? Maybe post your code so we can see how you're setting things up.

Ronald
fuzzy



Joined: 26 Feb 2005
Posts: 64

View user's profile Send private message

PostPosted: Fri Mar 04, 2005 1:17 pm     Reply with quote

Thank you for your help. I put value=input(c3) only and it works correctly now.
now i've got another problem, my slave pic doesn't recognize mastres commands. i send data using tjis code:

Code:
 i2c_start();
           i2c_write(0x4e);
           i2c_write(rele1);
           i2c_write(rele2);
           i2c_stop();
             delay_ms(8);


and i try to receive data:
Code:


#use I2C(slave, sda=PIN_C4, scl=PIN_C3, address=0x4e, FORCE_HW)

#INT_SSP
void ssp_interupt ()
{


   a=i2c_read();
   b=i2c_read();
   

 
     output_high(pin_a2);
     delay_ms(200);
     delay_ms(200);
     delay_ms(200);
     output_low(pin_a2);
     delay_ms(200);
     delay_ms(200);
 


}

why doesn't it works?

i can't understand if i doesn't enter ISR or it doesn't decode address.

valemike
Guest







PostPosted: Fri Mar 04, 2005 1:40 pm     Reply with quote

Look at

http://www.ccsinfo.com/forum/viewtopic.php?t=21456

for some working slave code. Don't forget to change the defined slave_address accordingly. You should at least be able to get it to receive the three bytes.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Fri Mar 04, 2005 3:55 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?p=40688#40688
fuzzy



Joined: 26 Feb 2005
Posts: 64

View user's profile Send private message

PostPosted: Sat Mar 05, 2005 9:19 am     Reply with quote

I loaded snibbe's slave code changing only slaves' address. Nut it doesn't run. it seems pic doesn't enter I2C's ISR. I can say this because in ISR i added a code to flash 2 leds and they don't flash!!
[/code]

#INT_SSP
void ssp_interupt ()
{
output_high(pin_c2);
delay_ms(200);
delay_ms(200);
delay_ms(200);
output_low(pin_c2);



i2c_interrupt_handler();



output_high(PIN_C0);
delay_ms(250);
delay_ms(250);
delay_ms(250);
delay_ms(250);
output_low(PIN_C0);


}



I2C bus runs correctly because i can drive correctly other slave devices on it.

my master use a code similar to snibbe's one to write:
Code:

           i2c_start();
           i2c_write(0x4e);
           i2c_write(byte1);
           i2c_write(byte2);
           i2c_stop();
             delay_ms(8);




and it will always send only this two bytes after address

Why doesn't it enter ISR in your opinion?
[/code]
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Sat Mar 05, 2005 10:17 am     Reply with quote

Did you make the slave's address 0x4E?
What PIC are you using?
Guest








PostPosted: Sat Mar 05, 2005 10:30 am     Reply with quote

Code:

#INT_SSP
void ssp_interupt ()
{
    i2c_interrupt_handler();
}


You should take out the the LED blinking in your #int_ssp routine. Reason is because you delay_ms(200) three times (for a total of 600ms). The master code doesn't want to wait for this delay and tries to send the next i2c instruction, while the slave is still executing the long delay. This results in overruns, and then the PIC's i2c module will need to be reset. You should defer your blinking til when you get out of the isr, such as in the main() routine.

Do you use a debugger such as an icd? If so, you should set a breakpoint in the slave at the i2c_interrupt_handler(). Verify that the master's call i2c_write() results in the slave's i2c_interrupt_handler() getting called. Don't worry about LED blinking at the moment til you verify that you truly are getting into the slave's ISR.

On a different topic, i noticed you call delay_ms(200) three times. If you are using a constant "200" in your call to delay_ms(), then you can simply write any number between 0 - 65535. Only if you use a variable in the argument, such as delay_ms(x) are you limited to an 8-bit value 0-255.
fuzzy



Joined: 26 Feb 2005
Posts: 64

View user's profile Send private message

PostPosted: Sat Mar 05, 2005 11:43 am     Reply with quote

I tryed as you said, I took off delay and i toggled a bit everytime isr is called in this wai it will switch the led in main every Isr. I've got same problem. i cant understand why.

thank you for your help about time delay.... i didn't know it!!
valemike
Guest







PostPosted: Sat Mar 05, 2005 3:34 pm     Reply with quote

Okay, i was "Guest" in the above post.

In your master code...
declare these variables...

unsigned int ack1, ack2, ack3

i2c_start();
ack1 = i2c_write(0x4e);
ack2 = i2c_write(byte1);
ack3 = i2c_write(byte2);
i2c_stop();
delay_ms(8);

Find the contents of ack1, ack2, and ack3 after going thru the program once. If they are are '0', then the slave ACK'ed okay. If they are '1', then the slave is still having problems.

What is in your circuit? Do you have access to a pair of PICDEM boards?
fuzzy



Joined: 26 Feb 2005
Posts: 64

View user's profile Send private message

PostPosted: Sun Mar 06, 2005 7:53 am     Reply with quote

Hi Valemike,

thanks for your help.

ack1, ack2 and ack3 are all 1 after first transmission.

this is my complete I2C transmission master code:
Code:

i2c_start();
           i2c_write(0b01000000);
           i2c_write(led1);
           i2c_write(led2);
           i2c_stop();
             delay_ms(8);

              i2c_start();
              delay_ms(1);
            ack1=i2c_write(0x4e);
              delay_ms(1);
            ack2=i2c_write(rele1);
              delay_ms(1);
            ack3=i2c_write(rele2);
              delay_ms(1);
              i2c_stop();
              delay_ms(8);
 
               i2c_start();
           i2c_write(0b01000010);
           i2c_write(rele3);
           i2c_write(0b00000000);
           i2c_stop();
             delay_ms(8);


first and last devices receives correctly. the second one is PIC 16F876.

I haven't got PICDEM boards.

MY slave PIC has all I/o ports,but c3 and c4, connected to uln2003 to drive some relays.

where is the problem in your opinion????
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 1, 2  Next
Page 1 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