I'm using two 18F4550 PICs with GCB version 98.01. I'm trying to make one PIC, configured as the HI2C MASTER, communicate with a second PIC configured as an HI2C SLAVE. Is this even possible?
As a simple test I wrote a little program where the MASTER PIC addresses the SLAVE PIC and then sends the numbers 1 3 5 7 9 11. The LCD connected to the MASTER PIC shows these numbers are being sent .
I used 14 (b'1110) for the SLAVE address which puts the required zero in the R/W bit.
I wrote another little program where the SLAVE PIC receives the address and the above numbers from the MASTER. The problem is the LCD attached to the SLAVE PIC prints out 1 14 14 3 5 7 9 11. i.e. It prints the first "1" then the SLAVE address (14) TWICE followed by the numbers 3 5 7 9 11.
This the software for the MASTER PIC:
'HI2C Settings
#define HI2C_BAUD_RATE 100
#define HI2C_DATA PORTB.0
#define HI2C_CLOCK PORTB.1
'
Dir HI2C_DATA In
Dir HI2C_CLOCK In
HI2CMode Master
'
Data_Output = 1 ;Startwith 1
Slave_Address = 14 ;0000 1110 0Ehex
wait 500 ms
'
Main Program #############################################################
DoI2C_Again:
HI2CSTART
HI2CSend Slave_Address ; Send 14
HI2CSend Data_Output ;1 3 5 9 11
HI2CSTOP
'
Print Data_Output ;Prints out 1 3 5 7 9 11 which is correct
Print " "
'
If Data_Output >= 10 then ;Restart count
Data_Output = 1
wait 100 ms
CLS
Goto DoI2C_Again
End If
'
Data_Output = Data_Output + 2
wait 1 ms
Goto DoI2C_Again
Here is the software for the SLAVE PIC:
'H'I2C Settings
#define HI2C_BAUD_RATE 100
#define HI2C_DATA PORTB.0
#define HI2C_CLOCK PORTB.1
'
Dir HI2C_DATA In
Dir HI2C_CLOCK In
'
Dim Data_Input (10)
'
HI2CMode Slave
'
HI2CSetAddress 14; Address b111 plus R/W = 0, ‘0’ in LSB indicates that the Master will write information to a selected slave
'
' General Call Enable bit (Slave mode only) TRIED THIS BUT IT DID NOT HELP
' BSF SSPCON2, GCEN ;1 = Enable interrupt when a general call address (0000h) is received in the SSPSR'
wait 500 ms
CLS
'Main Program ############################################################
'
Num_Bytes_to_Receive = 7
DoI2C_Again:
'SLAVE should receive 14 1 3 5 7 9 11 from MASTER.
' But the LCD displays 1 14 14 3 5 7 9 11.
' Puts first Data_Input "1" then Slave_Addr "14" TWICE followed by 3 5 7 9 11.
HI2CSTART
HI2CReceive Slave_Addr
For xx = 1 to Num_Bytes_to_Receive
HI2CReceive Data_Input(xx)
Next
HI2CStop
'
Print Slave_Addr ;Should print 14 BUT prints 1 instead
print " "
'
For xx = 1 to Num_Bytes_to_Receive ;Should print 1 3 5 7 9 11 BUT prints 1 14 14 3 5 7 9 11 instead
Print Data_Input(xx)
print " "
Next
'
If Data_Input(Num_Bytes_to_Receive) >= 10 then
wait 1 s
CLS
locate 0, 0
End If
'
Goto DoI2C_Again
Anyone see what I'm doing wrong?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Did you look at the demo I2C MASTER/SLAVE programs? they make very easy by exposing an incoming queue of bytes. The code is based on the APPNOTE on 'how to' make Master and Slave work.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm using two 18F4550 PICs with GCB version 98.01. I'm trying to make one PIC, configured as the HI2C MASTER, communicate with a second PIC configured as an HI2C SLAVE. Is this even possible?
As a simple test I wrote a little program where the MASTER PIC addresses the SLAVE PIC and then sends the numbers 1 3 5 7 9 11. The LCD connected to the MASTER PIC shows these numbers are being sent .
I used 14 (b'1110) for the SLAVE address which puts the required zero in the R/W bit.
I wrote another little program where the SLAVE PIC receives the address and the above numbers from the MASTER. The problem is the LCD attached to the SLAVE PIC prints out 1 14 14 3 5 7 9 11. i.e. It prints the first "1" then the SLAVE address (14) TWICE followed by the numbers 3 5 7 9 11.
This the software for the MASTER PIC:
'HI2C Settings
#define HI2C_BAUD_RATE 100
#define HI2C_DATA PORTB.0
#define HI2C_CLOCK PORTB.1
'
Dir HI2C_DATA In
Dir HI2C_CLOCK In
HI2CMode Master
'
Data_Output = 1 ;Startwith 1
Slave_Address = 14 ;0000 1110 0Ehex
wait 500 ms
'
Main Program #############################################################
DoI2C_Again:
HI2CSTART
HI2CSend Slave_Address ; Send 14
HI2CSend Data_Output ;1 3 5 9 11
HI2CSTOP
'
Print Data_Output ;Prints out 1 3 5 7 9 11 which is correct
Print " "
'
If Data_Output >= 10 then ;Restart count
Data_Output = 1
wait 100 ms
CLS
Goto DoI2C_Again
End If
'
Data_Output = Data_Output + 2
wait 1 ms
Goto DoI2C_Again
Here is the software for the SLAVE PIC:
'H'I2C Settings
#define HI2C_BAUD_RATE 100
#define HI2C_DATA PORTB.0
#define HI2C_CLOCK PORTB.1
'
Dir HI2C_DATA In
Dir HI2C_CLOCK In
'
Dim Data_Input (10)
'
HI2CMode Slave
'
HI2CSetAddress 14; Address b111 plus R/W = 0, ‘0’ in LSB indicates that the Master will write information to a selected slave
'
' General Call Enable bit (Slave mode only) TRIED THIS BUT IT DID NOT HELP
' BSF SSPCON2, GCEN ;1 = Enable interrupt when a general call address (0000h) is received in the SSPSR'
wait 500 ms
CLS
'Main Program ############################################################
'
Num_Bytes_to_Receive = 7
DoI2C_Again:
'SLAVE should receive 14 1 3 5 7 9 11 from MASTER.
' But the LCD displays 1 14 14 3 5 7 9 11.
' Puts first Data_Input "1" then Slave_Addr "14" TWICE followed by 3 5 7 9 11.
HI2CSTART
HI2CReceive Slave_Addr
For xx = 1 to Num_Bytes_to_Receive
HI2CReceive Data_Input(xx)
Next
HI2CStop
'
Print Slave_Addr ;Should print 14 BUT prints 1 instead
print " "
'
For xx = 1 to Num_Bytes_to_Receive ;Should print 1 3 5 7 9 11 BUT prints 1 14 14 3 5 7 9 11 instead
Print Data_Input(xx)
print " "
Next
'
If Data_Input(Num_Bytes_to_Receive) >= 10 then
wait 1 s
CLS
locate 0, 0
End If
'
Goto DoI2C_Again
Anyone see what I'm doing wrong?
Did you look at the demo I2C MASTER/SLAVE programs? they make very easy by exposing an incoming queue of bytes. The code is based on the APPNOTE on 'how to' make Master and Slave work.