bungee-
Joined: 27 Jun 2007 Posts: 206
|
I2C master - slave communication |
Posted: Sun Jul 04, 2010 1:07 pm |
|
|
Hi all!
I'm trying to communicating between two PIC's with I2C protocol and I'm having troubles...
Circuit is simple as it is could be. Two PIC16F886 each have LCD connected on portB, SCL and SDA are connected together and 4k7 pullup resistors are on them. This is it. Hardware is actually put together on protoboard.
Code for slave is taken out from EX code and modified a little. Everything stops at master i2c_stop() command at the end of code. If I disconnect slave then master go over that point. I was looking what happens with oscilloscope and SDA line stays low at that point of program.
Here is code
MASTER:
Code: | #include <16F886.h>
#FUSES NOWDT,INTRC_IO,PUT,NOMCLR,NOPROTECT,NOCPD,NOBROWNOUT,NOIESO
#FUSES NOFCMEN,NOLVP,NODEBUG,NOWRT,BORV40
#use delay(clock=8000000)
#define I2C_SCL PIN_C3
#define I2C_SDA PIN_C4
//#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3,force_hw)
#include "flex_lcd.c"
void setup()
{
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(false);
setup_oscillator(OSC_8MHZ);
lcd_init();
}
void main()
{
int8 data,t;
setup();
lcd_putc("Start... \n");
while (1)
{
delay_ms(500);
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_write('A');
i2c_stop();
delay_ms(1);
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data=i2c_read();
i2c_stop(); //<--- IT STOPS HERE!!!
printf(lcd_putc," Read: %u \n",data);
}
} |
SLAVE:
Code: | #include <16F886.h>
#FUSES NOWDT,INTRC_IO,PUT,NOMCLR,NOPROTECT,NOCPD,NOBROWNOUT,NOIESO
#FUSES NOFCMEN,NOLVP,NODEBUG,NOWRT,BORV40
#use delay(clock=8000000)
#use i2c(Slave,Slow,sda=PIN_C4,scl=PIN_C3,force_hw,address=0xA0)
#include "flex_lcd.c"
byte address, buffer[0x10];
BYTE incoming, state, ping;
#int_SSP
void SSP_isr(void)
{
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]);
ping=1;
}
}
void setup()
{
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);
setup_oscillator(OSC_8MHZ);
lcd_init();
ping=0;
}
void main()
{
int t;
setup();
Printf(lcd_putc,"Ready ...\n");
while(1)
{
if (ping==1)
{
lcd_putc('.');
ping=0;
}
}
} |
Please can somebody give me a fresh perspective, I'm stuck in a loop thank's! |
|