|
|
View previous topic :: View next topic |
Author |
Message |
funmix
Joined: 27 Mar 2007 Posts: 33
|
Simple I2C communication between PICs |
Posted: Fri Mar 30, 2007 11:42 pm |
|
|
i am implementing two way communication via the I2C bus.
The data on the input to the master Port B (DIP switch) is transferred to display on the slave's Port D(LEDs) and vice versa.
I found that the LED is not lighten up.
Can someone point out what error i did in my software?
i2c_master.c
Code: |
#include <16F877.h>
#use delay(clock=20000000)
#fuses HS,WDT
#use rs232(baud =19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=,bits=8)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3,RESTART_WDT,FORCE_HW)
#int_TIMER1
TIMER_isr()
{
i2c_start();//set i2c to start condition
i2c_write(0xa0);//send slave address
i2c_write(input_b());//send master data from port b bits
i2c_start();//restart to change data direction
i2c_write(0xa1);//address with direction bit changed
output_d(~i2c_read(0));//get slavedata and display
i2c_stop();//stop i2c
}
void main()
{
setup_counters(RTCC_INTERNAL,WDT_1152MS);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);//pull ups for the input pins
port_b_pullups(TRUE);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(1)
{
RESTART_WDT();//use WDT in case I2C hangs up to faulty slave
}
}
|
i2c_slave.c
Code: |
#include <16F877.h>
#use delay(clock=20000000)
#fuses HS,NOWDT
#use rs232(baud =19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=,bits=8)
#use i2c(SLAVE,sda=PIN_C4,SCL=PIN_C3,address=0xa0)
enum {address,data};
int mode;
#int_SSP
SSP_isr()
{
int rec;//check for valid rec received. the interrupt occurs, but no
//valid character is received when setting up for the read
if (i2c_poll())
{
rec=i2c_read();//get data received
//rec contains address on first transmission of the interchange
if(mode ==address) mode = data;//so ignore and set for data byte
else //must be data byte
{
output_D(~rec);//do output master data on LEDs
mode=address;//go back to waiting for address
}
}
//get to 'else' only if second command byte of interchange, so load
//i2c output register for read operation
else i2c_write(input_b());//load for read
}
void main()
{
port_b_pullups(true);//pull ups on input bits
enable_interrupts(INT_SSP);//interrupt mode required
enable_interrupts(global);
mode=address;//set starting mode
while(1)//main does nothing
;
}
|
assuming my hardware is not causing a problem |
|
|
Ttelmah Guest
|
|
Posted: Sat Mar 31, 2007 2:45 am |
|
|
Look at the I2C slave example.
Seriously, your 'slave', makes the _assumption_ that it is going to remain in sync with the master, and that no transactions are missed at boot up. Not a reliable thing to do. The slave needs to be looking at the I2C control bits, to find out what the interrupt is saying. You also shouldn't use I2C_poll to distinguish the data direction. The buffer full bit (which this reflects), does not get set on a slave device, when an address byte for a read is received. This is why it is _vital_ to check the address/data flag in the I2C registers.
Best Wishes |
|
|
funmix
Joined: 27 Mar 2007 Posts: 33
|
|
Posted: Sun Apr 01, 2007 6:32 pm |
|
|
Hi all,
I found two bugs in the I2C multibyte master and slave providing in code library
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 |
|
|
funmix
Joined: 27 Mar 2007 Posts: 33
|
|
Posted: Mon Apr 02, 2007 10:14 am |
|
|
I understand that the maximum data can read from is FFFF.
for the problem 2,
When i replace 18F4520 as slave, the value of I2Cread is not correct.
Whatever value/integer i key in, the output is the same = FFFF.
Why does this occur? |
|
|
|
|
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
|