|
|
View previous topic :: View next topic |
Author |
Message |
natapongw
Joined: 19 Apr 2011 Posts: 7
|
16F690 i2c communication problem |
Posted: Tue Apr 19, 2011 10:50 pm |
|
|
I have 2 PIC16F690 to communication with i2c.
My code can interrupt slave PIC because LED light up but the value that I get at master not correct I'm not sure where is problem.
I use 4.7K resistor to pullup at SDA/SCL pin.
and here is my code
MASTER
Code: |
#include <16f690.h>
#device adc=8
#fuses NOWDT, BROWNOUT, NOPUT, MCLR,NOCPD,INTRC,IESO,FCMEN
#use delay (clock=4000000)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B5,bits=8, errors)
#use i2c(Master, sda=PIN_B4, scl=PIN_B6)
void read()
{
int data;
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_write('b');
i2c_stop();
delay_ms(1000);
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
printf("read %x \n\r", data);
}
void main(void){
while (TRUE){
//delay_ms(1000);
read();
}
}
|
Slave
Code: |
#include <16f690.h>
#fuses INTRC, NOWDT, BROWNOUT,NOPROTECT, NOPUT, NOMCLR,NOCPD,IESO,FCMEN
#use delay (clock=4000000)
#use i2c(SLAVE, SDA=PIN_B4, SCL=PIN_B6, address=0xa0,FORCE_HW)
BYTE address,buffer[0x10];
void outpin(void){
output_high(PIN_C0);
delay_us(100);
output_low(PIN_C0);
}
#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]);
outpin();
}
}
void main(){
setup_adc_ports(NO_ANALOGS | VSS_VDD);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED, 0, 1);
setup_comparator(NC_NC_NC_NC);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1){}
}
|
I get only "a1" value whatever send to slave. Please help me and sorry for my poor English. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Wed Apr 20, 2011 4:57 am |
|
|
Get rid of the delay in the slave ISR (outpin).
It you want to be sure something is happening, just use a quick instruction, like 'output_toggle(PIN_C0)'.
Delays in ISR's, 99% of the time _will_ cause problems. The actual transaction to write the byte, does not complete till you leave the ISR. By then the master has already read the port, and got back the last byte sent (0xA1).
You may also find it is more reliable if the slave is running slightly faster (8MHz), or you add a tiny delay between writing the 0xA1, and actually reading the data back.
Your post is good. Gives compiler details, reasonably short example etc.. Beat's 90% of the native English speakers.....
Best Wishes |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Wed Apr 20, 2011 9:19 am |
|
|
Your master is sending out commands/data as fast as it can. It takes a certain amount of time for the slave to respond and receive/send data from/to the master. Try puting some delays in your master code to separate each command to give the slave time to do what it needs to do. You can adjust the delay times to optimize the speed. Also, you can put code in that will check to see if the slave is still busy before you try sending another command by testing if it will ACK when you send the next command. If it does not ACK then you can try re-sending that command until the slave does ACK. But first, try delays to get things working.
Also, I'm not sure if you really need the FORCE_HW in your slave code. I've never needed to insert that before.
Ronald |
|
|
natapongw
Joined: 19 Apr 2011 Posts: 7
|
|
Posted: Thu Apr 21, 2011 1:13 am |
|
|
Thank you very much , I still can't use i2c for PIC to PIC communication so I change to SPI interface. It work correctly when push data from master to slave but I can't get data from slave to master.
Is anyone have some example code or any suggestion. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
gogutier
Joined: 12 Jul 2011 Posts: 4
|
|
Posted: Tue Jul 12, 2011 11:05 pm |
|
|
Dude I have your same problem I think. I'm trying to communicate 2 16f690 via i2c and, on Proteus simulations, I can only write to the Slave but I can't read from it.
I've checked and both of my PICS are revision 6.
Did you get your's to work? or find something useful (i.e. an example code that actually works on 16f690)?
If you want I can post my code but it's very similar to yours.
Thanks in advance.
PD. I'm not an english native speaker so sorry if something is not very clear. |
|
|
|
|
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
|