CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

interface between two PIC's using I2C

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
qpnikepor
Guest







interface between two PIC's using I2C
PostPosted: Mon Jan 11, 2010 10:28 pm     Reply with quote

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?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 11, 2010 11:10 pm     Reply with quote

Quote:
#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]);
}

}

Look at the way CCS does this routine in Ex_Slave.c. Try to follow their
style more closely. See what happens. Here's the file location:
Quote:
c:\Program Files\picc\Examples\Ex_Slave.c
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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