View previous topic :: View next topic |
Author |
Message |
fuzzy
Joined: 26 Feb 2005 Posts: 64
|
PIC as I2C slave. HW problem |
Posted: Thu Mar 03, 2005 12:59 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Mar 03, 2005 3:34 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
fuzzy
Joined: 26 Feb 2005 Posts: 64
|
|
Posted: Thu Mar 03, 2005 3:49 pm |
|
|
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!!! |
|
![](templates/subSilver/images/spacer.gif) |
valemike Guest
|
|
Posted: Thu Mar 03, 2005 5:39 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
fuzzy
Joined: 26 Feb 2005 Posts: 64
|
|
Posted: Thu Mar 03, 2005 6:35 pm |
|
|
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??? |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri Mar 04, 2005 9:27 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
fuzzy
Joined: 26 Feb 2005 Posts: 64
|
|
Posted: Fri Mar 04, 2005 1:17 pm |
|
|
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.
|
|
|
![](templates/subSilver/images/spacer.gif) |
valemike Guest
|
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
![](templates/subSilver/images/spacer.gif) |
fuzzy
Joined: 26 Feb 2005 Posts: 64
|
|
Posted: Sat Mar 05, 2005 9:19 am |
|
|
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] |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sat Mar 05, 2005 10:17 am |
|
|
Did you make the slave's address 0x4E?
What PIC are you using? |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Sat Mar 05, 2005 10:30 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
fuzzy
Joined: 26 Feb 2005 Posts: 64
|
|
Posted: Sat Mar 05, 2005 11:43 am |
|
|
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!! |
|
![](templates/subSilver/images/spacer.gif) |
valemike Guest
|
|
Posted: Sat Mar 05, 2005 3:34 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
fuzzy
Joined: 26 Feb 2005 Posts: 64
|
|
Posted: Sun Mar 06, 2005 7:53 am |
|
|
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???? |
|
![](templates/subSilver/images/spacer.gif) |
|