qpnikepor Guest
|
interface between two PIC's using I2C |
Posted: Mon Jan 11, 2010 10:28 pm |
|
|
Hi
I am trying to implement a simple network where the master sends two bytes indicating what characters to display.
The slave receives them and displays them.
Slave
Code: |
#include<16f886.h>
#include<def_16f886.h>
#use delay(clock = 4000000)
#include<flex_lcd.h>
#fuses intrc_io, noprotect, nowdt
#use i2c(slave, sda = pin_c4, scl = pin_c3, address = 0xa0, fast, force_hw)
int8 address = 0xa0;
int8 buffer[5] = {0, 0, 0, 0, 0};
int8 buffer1;
int8 incoming = 0;
int8 first_byte = 0;
int8 second_byte = 0;
int8 state;
#int_ssp
void ssp_interupt ()
{
state = i2c_isr_state();
if(state < 0x80){ //Master is sending the data
if(state==1) first_byte = i2c_read();
if(state==2) second_byte = i2c_read();
}
if(state == 0x80) //Master is requesting data
{
i2c_write(buffer[address]);
}
}
void main(void){
lcd_init();
lcd_send_byte(0,ALL_CLEAR);
delay_ms(20);
enable_interrupts(int_ssp);
enable_interrupts(global);
while(1){
lcd_gotoxy(1,1);
lcd_putc(first_byte);
lcd_gotoxy(1,2);
lcd_putc(second_byte);
}
}
|
Master
Code: |
#include<18F2510.h>
#include<def_18f2510.h>
#fuses intrc_io, NOBROWNOUT
#use delay(clock = 4000000)
#use i2c(master, SCL = pin_a4, SDA = pin_a5, fast)
void main(void){
while(1){
delay_ms(1);
i2c_start();
i2c_write(0xa0);
i2c_write('S');
i2c_write('B');
i2c_stop();
delay_ms(1000);
}
}
|
I looked at all of the posts here about I2C slave and came up with code above. However, it doesn't seem to be working.
Anyone with idea? |
|