[email protected]
Joined: 16 May 2006 Posts: 39
|
16F689 I2C |
Posted: Fri Feb 12, 2010 7:26 pm |
|
|
I am using 16F689 to build 2 boards to test the I2C operation but found that it is not working.
Master
Code: |
#include <16F689.h>
#device *=16
#device adc=8
#FUSES NOWDT
#FUSES MCLR, HS, NOBROWNOUT
#use delay(clock=20000000)
#use i2c(master,sda=PIN_B4,scl=PIN_B6)
#users232(baud=19200,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8,errors, stream=PC)
main()
{
int c;
delay_ms(2000 );
fprintf(PC,"Ready..\n\r");
while (true) {
i2c_start();
i2c_write(0xa0);
// delay_ms(100);
i2c_write(50);
// delay_ms(100);
i2C_start();
i2c_write(0xa1);
// delay_ms(100);
c = i2c_read(0);
fprintf(PC, "%c\n\r", c );
}
}
|
Slave
Code: |
#include <16F689.h>
#device *=16
#device adc=8
#FUSES NOWDT
#FUSES MCLR, HS, NOBROWNOUT
#use delay(clock=20000000)
#use i2c(Slave,sda=PIN_B4,scl=PIN_B6,address=0xa0,force_hw)
int n;
#int_SSP
SSP_isr()
{
int state;
state = i2c_isr_state();
if ( state >= 0x80 )
i2c_write( n );
else if (state > 0 ) {
output_toggle( pin_c4 );
n = i2c_read();
}
else {
output_toggle( pin_c3 );
}
}
|
Problem
1. Master never work with force_hw
2. Slave - no data was read BUT the READ ADDRESS MATCH (pin_c3 toggled).
3. Slave - no data was read because PIN_C4 not toggled.
Work around
1. I put some extra delay_ms() instruction but no help.
2. Slave can write data (Master can read).
Any idea? |
|