SNooPI
Newbie level 1
I have a project and I can't figure it out why it isn't working the I2C. I need to transfer some data from a PIC16F877A to another using I2C. Here is the code:
Master.h
Master.c
Slave.h
Slave.c
I'm pretty new with this, maybe someone can help me. I have 2 pull-up resistors for both lines. I'm using RS232 for testing/debugging.
This is what I get on RS232:
**broken link removed**
Thanks,
SNooPI
Master.h
Code:
#include <16F877A.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3)
Master.c
Code:
#include "master.h"
byte data = 0;
#int_TIMER0
void TIMER0_isr(void)
{
printf("I'm sending this on I2C: %d \r\n", data);
i2c_start ();
i2c_write (0xA0);
i2c_write (data);
i2c_stop ();
data++;
}
void main()
{
printf("MASTER!\r\n");
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(1){}
}
Slave.h
Code:
#include <16F877A.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Slave,sda=PIN_C4,scl=PIN_C3,address=0xA0, FORCE_HW)
Slave.c
Code:
#include "slave.h"
#int_SSP
void SSP_isr(void)
{
byte incoming, state;
state = i2c_isr_state ();
if (state < 0x80) //Master is sending data
{
printf("Master is sending data\r\n");
incoming = i2c_read ();
printf("Incoming: %d\r\n", incoming);
if (state == 1)
printf("I received address\r\n"); //First received byte is address
if (state == 2)
printf("I received data\r\n"); //Second received byte is data
}
if (state == 0x80) //Master is requesting data
printf("Master is requesting data\r\n");
}
#int_TIMER0
void TIMER0_isr(void)
{
}
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_SSP);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
printf("SLAVE!\r\n");
while(1){}
}
I'm pretty new with this, maybe someone can help me. I have 2 pull-up resistors for both lines. I'm using RS232 for testing/debugging.
This is what I get on RS232:
**broken link removed**
Thanks,
SNooPI
Last edited: