|
|
View previous topic :: View next topic |
Author |
Message |
umut
Joined: 31 Mar 2010 Posts: 2
|
i2c problem with 2 pic16f877 |
Posted: Wed Mar 31, 2010 11:31 am |
|
|
I want to use one of them master and second is slave. Master takes data from rs232 and send them to the slave. I addressed the slave but I did not communicate properly. Could you help me? My code is:
Slave code is
Code: |
#use_i2c(slave,sda=pin_c4,scl=pin_c3,slow=100000,address=0xA0,force_hw)
BYTE address, buffer[0x8];
#int_SSP
void ssp_interupt (){
BYTE state,incoming;
//clear_interrupt(int_ssp);
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
//incoming = i2c_read();
if(state==0){
}
if(state == 1) //First received byte is address
{
address = i2c_read();
//data = incoming;
}
if(state == 2) //Second received byte is data
{
buffer[address] = i2c_read();
// f[address]=incoming;
//data = incoming;
}
}
if(state >= 0x80) //Master is requesting data
{
i2c_write(buffer[address]);
}
}
|
Master code is
Code: |
#include <16f877.h>
#use rs232 (baud=9600, xmit=pin_C6, rcv=pin_C7, parity=N, stop=1)
#use i2c(master,sda=pin_c4, scl=pin_c3,slow=100000,force_hw)
#define end_1 0xA0
#define end_1 0xB0
#int_rda
void serihaberlesme ()
{
disable_interrupts(int_rda); // interrupt pasif ediliyor
m=0;
printf("\n\r m>"); // BU KISIMDA rs 232den veriler alınıyor
d=gethex();
//putc(m);
printf("\n\radres>");
b=gethex();
//putc(adres);
printf("\n\rdata>");
c=gethex();
//putc(data);
enable_interrupts(int_rda);
}
void main ()
{
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);
enable_interrupts(int_rda);
enable_interrupts(GLOBAL);
delay_ms(1000);
i2c_start();
i2c_write(0xA0);
i2c_write(0x02);
i2c_write(0x08);
i2c_stop();
while (!m){
if (d==0)
{
i2c_start();
i2c_write(end_1);
i2c_write(0x02);
i2c_write(0x08);
// i2c_read();
i2c_stop();
}
else if(d==1){
i2c_start();
i2c_write(end_2);
i2c_write(c);
i2c_stop();
}
m=1;
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 31, 2010 2:07 pm |
|
|
First, try to get a simple i2c slave program working.
When you get that working, then try to make your own program work.
Use the CCS example file code, Ex_Slave.c, for the Slave PIC.
It's in this directory:
Quote: |
c:\Program Files\picc\Examples\Ex_Slave.c
|
Use the code in this post for the Master PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=32368&start=3 |
|
|
umut
Joined: 31 Mar 2010 Posts: 2
|
|
Posted: Thu Apr 01, 2010 6:07 am |
|
|
thank you for your help |
|
|
|
|
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
|