ncvnmarket
Joined: 24 Oct 2009 Posts: 12
|
i2c problems |
Posted: Sat Oct 24, 2009 7:49 pm |
|
|
Hi guys,
I'd like to send data from master (in one PIC 16f877a) and receive the data from slave (in another PIC 16f877a). I don't know why the code does not work right. I check the output from the master and clk and sda look fine, but when I connect two lines to slave then check the signal again, the clock is low (0) and the data is high (1). Here is my code:
Master:
Code: |
#include <16F877A.h>
#device adc=8
#fuses NOWDT,RC, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, LVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use I2C(master, sda=PIN_C4, scl=PIN_C3)
void sendData(BYTE data);
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
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);
setup_vref(FALSE);
while (1){
delay_ms (5);
sendData(0b11111111);
}
}
void sendData(BYTE data){
i2c_start(); // Start condition
delay_cycles(5);
i2c_write(0xa0);// Device address
delay_cycles(5);
i2c_write(data);// Low byte of command
delay_cycles(5);
i2c_stop();
}
|
Slave:
Code: |
#include <16F877A.h>
#device adc=8
#fuses NOWDT,RC, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, LVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa0)
unsigned int dat;
#INT_SSP
void ssp_interupt ()
{
//clear_interrupt(int_SSP);
//while(!i2c_poll()) ;
dat = i2c_read();
i2c_write(0);
}
void main()
{
set_tris_c(0xFF);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
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);
setup_vref(FALSE);
enable_interrupts(INT_SSP); //enable I2C interrupts
enable_interrupts(GLOBAL);
//dat=0;
while(1){
// while(!i2c_poll()) ;
// dat = i2c_read();
if (dat==0b11111111) {
output_high(PIN_A0);
} else {
output_low(PIN_A0);
}
}
}
|
I want to compare the received data and sending data and ensure that it is same.
Thanks. |
|