Hi all. I need to synchronise an array from one PIC to another (one way only), so I have been working to make a simplified version of the code seen in this thread with just the bare essentials to send the array from slave to master.
It mostly works, but there is one little bug. I am sending the array over and over again - the test array is 6 bytes and I have assigned test values of 1 2 3 4 5 and 6.
On the 1st i2cread the master receives 123456. On the 2nd i2cread it gets 234561, and the third 345612. It seems that every time I do an i2cread, the following byte goes missing. The first time I get 123456, the next 1 goes into oblivion and therefore the next packet is 234561.
I know I can get around this by sending a dummy byte after the array, knowing that it will be lost, but I'd like to know why this is happening if anyone has any ideas?
Master PIC (18F2620, 40Mhz HSPLL):
Code:
DEFINE OSC 40
DEFINE I2C_HOLD 1
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
PAUSE 100
LCDOUT $FE,1,$FE,2
ADCON1=%00001111
CMCON=%00000111
scl VAR PORTC.3
sda VAR PORTC.4
i2c_address VAR BYTE
i2c_data VAR BYTE[6]
i2c_address=$0A
main:
I2CREAD sda,scl,i2c_address,[STR i2c_data\6]
LCDOUT $FE,$80,DEC i2c_data[0]
LCDOUT " ", DEC i2c_data[1]
LCDOUT " ", DEC i2c_data[2]
LCDOUT " ", DEC i2c_data[3]
LCDOUT " ", DEC i2c_data[4]
LCDOUT " ", DEC i2c_data[5]
PAUSE 1000
GOTO main
Slave PIC (18F2620, 40Mhz HSPLL):
Code:
DEFINE OSC 40
INCLUDE "DT_INTS-18.bas"
INCLUDE "ReEnterPBP-18.bas"
ADCON1=%00001111
CMCON=%00000111
i2c_buffer_full VAR SSPSTAT.0
i2c_read VAR SSPSTAT.2
i2c_release_scl VAR SSPCON1.4
i2c_data VAR BYTE[6]
i2c_data_index VAR BYTE
dummy VAR BYTE
SSPADD=$0A ' I2C Address: $0A
SSPCON1=$36
SSPCON2.0=1
i2c_data[0]=1
i2c_data[1]=2
i2c_data[2]=3
i2c_data[3]=4
i2c_data[4]=5
i2c_data[5]=6
i2c_data_index=0
ASM
INT_LIST macro
INT_Handler SSP_INT, _i2c_int_handler, PBP, yes
endm
INT_CREATE
INT_ENABLE SSP_INT
endasm
main:
' main program goes here
goto main
i2c_int_handler:
IF i2c_read then
dummy=SSPBUF
IF i2c_buffer_full=0 THEN
SSPBUF=i2c_data[i2c_data_index]
i2c_data_index=i2c_data_index+1
IF i2c_data_index=6 THEN i2c_data_index=0
ENDIF
ENDIF
i2c_release_scl=1
@ INT_RETURN
Bookmarks