View previous topic :: View next topic |
Author |
Message |
Coder Guest
|
i2c SLAVE mode help |
Posted: Sat Apr 17, 2004 2:25 am |
|
|
im trying to receive two bytes from a i2c MASTER mode device. but all i get from the PIC is a constant output of 255 .
i know the PIC i2c addressing is ok because the intterupt service routine runs when the address is right. and doenst otherwise. but after that it shows any data which is received as 255.
the relevent code is as follows
Code: |
#use I2C(slave,sda=PIN_C4,scl=PIN_C3,address=0xb0)
#INT_SSP
i2c_slave_handler(){
while(!i2c_poll());
incoming_i2c_default[0]=i2c_read(1);
while(!i2c_poll());
incoming_i2c_default[1]=i2c_read(1);
for(count=0;count<4;count++){
output_high(PIN_A5);
delay_ms(100);
output_low(PIN_A5);
delay_ms(100);
}
}
void main()
{
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1){
sleep();
LCD_init();
printf(lcd_putc_A,"\fI2C 1st %u \nI2C 2nd %u",incoming_i2c_default[0],incoming_i2c_default[1]);
}
}
|
ive tried using FORCE_HW but with same results... what could be wrong?
any help would be greatly appriciated.
kind regards |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Sat Apr 17, 2004 2:36 am |
|
|
Just a quick note before going through your code: The SLAVE mode should only be used with the built-in SSP. That means you need to specify FORCE_HW. |
|
|
Coder Guest
|
|
Posted: Sat Apr 17, 2004 3:22 am |
|
|
yep , i added FORCE_HW but get the same results.
changed everything i can think of.
the PIC is not hanging nor is it hanging the i2c bus as other devices on the bus communicates ok.
another wierd thing i noticed is that after the first START conditon DATA and STOP condition the PIC stops giving out ACKS. but still responds to a call with its address by spawning the i2c isr.
the MASTER mode is working perfectly.
im actually using both MASTER and SLAVE modes by initializing the MASTER mode only when needed by the processor. and at the end of the routine it reverts back to SLAVE mode with a "#i2c SLAVE " directive.
im using a 16F876 (MASTER, SLAVE) , PCF8574(SLAVE) , and a Motorola MC44BS373CA (SLAVE), PC (MASTER)
all the above works except when the PIC tries to work in SLAVE mode .
PIC16F876 @ 20Mhz
PCW 3.168
any help would be most appriciated ... as im clueless as to whats going wrong.
-coder- |
|
|
Coder Guest
|
|
Posted: Sat Apr 17, 2004 11:06 am |
|
|
anyone ..?
im pretty much all out of ideas at this stage... |
|
|
steve Guest
|
Not sure about the ccs/micochip slave i2c/SP implementation. |
Posted: Thu May 06, 2004 6:53 am |
|
|
If you are happy for your slave to just hang up and wait for master comms then it works probably ok.
If you just use i2c_write() in the slave and sit and wait for the master to send a byte and you can communicate. But if you want the slave to do anything else while waiting for the master then I don't believe it will ever really work properly.
Maybe another opinion in the forum but I have never seen example code that actually works. |
|
|
AK
Joined: 20 Apr 2004 Posts: 33
|
|
Posted: Thu May 06, 2004 7:00 am |
|
|
Do you have the i2c bus lines tied to 5V through 1k resistors. I had a similar problem when I forgot to add the resistors. |
|
|
valemike Guest
|
better to change from slave to master, and vice versa? |
Posted: Mon May 10, 2004 3:58 pm |
|
|
Regarding a PIC slave i2c and a PIC master I2C
So my conclusion is:
1. When you want to send some data, then turn yourself into a Master. When you're done, then turn yourself into a slave.
is it possible to do #use i2c(...) at different points in the program just to change the i2c mode (slave <--> master)?
I don't think I want to tinker with a slave trying to send data, since even Microchip's FAEs tell me that they have problems with the libraries. |
|
|
|