i2c communications

Thread Starter

ayhampic

Joined Aug 12, 2011
19
hi ,

have anyone achieved an I2C between a master and slave , I have searched on the net but not satisfied .

please anyone have a 2way communication between a master and slave ?

I have written the following code in Proton , and it could move a message from the master to the slave , but i'm incapable of sending a message in the opposite way (from slave to master)

i have another question , where the address of the slave is checked (in hardware or in the software ) because the slave here is accepting every address ?
master
Rich (BB code):
Device = 18F4550                                              
Xtal = 20   
HBStart
SSPBUF=0

TXSTA.5=1 ' setting Transmit Enable Bit
RCSTA.7=1
RCSTA.4=1
Hserial_Baud 19200 ' Setting Baud rate


Symbol SMP = SSPSTAT.7       '1:enable high speed , 0: slow speed
Symbol CKE = SSPSTAT.6       '0 always for me
Symbol DATA_ADDR = SSPSTAT.5 '1:last byte was data, 0:last byte was address
Symbol P = SSPSTAT.4         'I2C Stop detection
Symbol S = SSPSTAT.3         'I2C Start detection
Symbol R_W = SSPSTAT.2       '1:read 0:write operation [m]1:inprogress 0:may not
Symbol UA = SSPSTAT.1        '1:sspadd need update [10bit only]
Symbol BF = SSPSTAT.0        'Buffer Full Status flag


Symbol WCOL  = SSPCON1.7  'Write Collision Detectection
Symbol SSPOV = SSPCON1.6  'Receive Overflow Indicator bit 
Symbol SSPEN = SSPCON1.5  'mssp Enable bit
Symbol CKP   = SSPCON1.4  'SCK Release Control bit    ???????????????????????? 1
Symbol SSPM3 = SSPCON1.3  '1111:[S10+INT],1110:[S7+INT],1011:[S IDLE]
Symbol SSPM2 = SSPCON1.2  '1000:MASTER F/4(SSPADD+1)
Symbol SSPM1 = SSPCON1.1  '0111[S10 NO INT] 
Symbol SSPM0 = SSPCON1.0  '0110[S 7BIT]


Symbol GCEN    = SSPCON2.7 ' General call enable bit
Symbol ACKSTAT = SSPCON2.6 '1:ack NOT receied,0:ack received (Master Transmitonly)
Symbol ACKDT   = SSPCON2.5 '1:ack 0:nack  (Master Receive mode only)
Symbol ACKEN   = SSPCON2.4 'Acknowledge Sequence Enable bit (Master Receive mode only)
Symbol RCEN    = SSPCON2.3 '1:Receive Enable 0:disabled (Master receive mode only)
Symbol PEN     = SSPCON2.2 'Stop Condition Enable bit (Master mode only) 
Symbol RSEN    = SSPCON2.1 'Repeated Start Condition Enabled bit (Master mode only) 
Symbol SEN     = SSPCON2.0 'Start Condition Enabled/Stretch Enabled bit


Symbol S_GCEN    = SSPCON2.7 ' General call enable bit
Symbol S_ACKSTAT = SSPCON2.6 'not used in slave
Symbol S_ACKDT   = SSPCON2.5 '1:enable address masking,0:disabled
Symbol S_ACKEN   = SSPCON2.4 '1:Enable address masking,0:disabled
Symbol S_RCEN    = SSPCON2.3 '1:Enable address masking,0:disabled
Symbol S_PEN     = SSPCON2.2 '1:Enable address masking,0:disabled
Symbol S_RSEN    = SSPCON2.1 '[7bit]1:masking of sspadd<1> [10]1:masking of <0:1> 
Symbol S_SEN     = SSPCON2.0 'enable clock stretch 0:disable


Dim i As Byte
Dim t As Byte
Dim DataOut As String * 30
Declare Slow_Bus On

SSPADD  = 49     '100kHz
SSPSTAT =  0b00000000
SSPCON2 =  0b00001000
SSPCON1 =  0b00101000
DelayMS 500
Output PORTB.7
    
 
i=-1
main:
 Toggle PORTB.7
 DelayMS 100
 HBusOut 0b10100000,["master say hello"]
 Toggle PORTB.7
 DelayMS 500
GoTo main
 


slave :
Rich (BB code):
Device = 18F4550                                               
Xtal = 20   
Output PORTB.7

TXSTA.5=1 ' setting Transmit Enable Bit
RCSTA.7=1
RCSTA.4=1
Hserial_Baud 19200 ' Setting Baud rate

LCD_Interface 4
LCD_DTPin PORTB.4
LCD_RSPin PORTB.3
LCD_ENPin PORTB.2


'ADCON1=0eh
'Declare Hbus_Bitrate 100
Input PORTB.0
Input PORTB.1

'master
'SSPADD = 49
SSPADD = 0b11110000

Symbol SMP = SSPSTAT.7       '1:enable high speed , 0: slow speed
Symbol CKE = SSPSTAT.6       '0 always for me
Symbol DATA_ADDR = SSPSTAT.5 '1:last byte was data, 0:last byte was address
Symbol P = SSPSTAT.4         'I2C Stop detection
Symbol S = SSPSTAT.3         'I2C Start detection
Symbol R_W = SSPSTAT.2       '1:read 0:write operation [m]1:inprogress 0:may not
Symbol UA = SSPSTAT.1        '1:sspadd need update [10bit only]
Symbol BF = SSPSTAT.0        'Buffer Full Status flag

'master:
'SSPSTAT = 0b00000000
SSPSTAT = 0b00000000
Symbol WCOL  = SSPCON1.7  'Write Collision Detectection
Symbol SSPOV = SSPCON1.6  'Receive Overflow Indicator bit 
Symbol SSPEN = SSPCON1.5  'mssp Enable bit
Symbol CKP   = SSPCON1.4  'SCK Release Control bit    ???????????????????????? 1
Symbol SSPM3 = SSPCON1.3  '1111:[S10+INT],1110:[S7+INT],1011:[S IDLE]
Symbol SSPM2 = SSPCON1.2  '1000:MASTER F/4(SSPADD+1)
Symbol SSPM1 = SSPCON1.1  '0111[S10 NO INT] 
Symbol SSPM0 = SSPCON1.0  '0110[S 7BIT]
'MASTER
'SSPCON1=0b00101000
SSPCON1=0b00101110
Symbol GCEN    = SSPCON2.7 ' General call enable bit
Symbol ACKSTAT = SSPCON2.6 '1:ack receied,0:ack not received (Master Transmitonly)
Symbol ACKDT   = SSPCON2.5 '1:ack 0:nack  (Master Receive mode only)
Symbol ACKEN   = SSPCON2.4 'Acknowledge Sequence Enable bit (Master Receive mode only)
Symbol RCEN    = SSPCON2.3 '1:Receive Enable 0:disabled (Master receive mode only)
Symbol PEN     = SSPCON2.2 'Stop Condition Enable bit (Master mode only) 
Symbol RSEN    = SSPCON2.1 'Repeated Start Condition Enabled bit (Master mode only) 
Symbol SEN     = SSPCON2.0 'Start Condition Enabled/Stretch Enabled bit
'SSPCON2=0b000000000      '4
 
'slave mode
Symbol S_GCEN    = SSPCON2.7 ' General call enable bit
Symbol S_ACKSTAT = SSPCON2.6 'not used in slave
Symbol S_ACKDT   = SSPCON2.5 '1:enable address masking,0:disabled
Symbol S_ACKEN   = SSPCON2.4 '1:Enable address masking,0:disabled
Symbol S_RCEN    = SSPCON2.3 '1:Enable address masking,0:disabled
Symbol S_PEN     = SSPCON2.2 '1:Enable address masking,0:disabled
Symbol S_RSEN    = SSPCON2.1 '[7bit]1:masking of sspadd<1> [10]1:masking of <0:1> 
Symbol S_SEN     = SSPCON2.0 'enable clock stretch 0:disable
SSPCON2=0b000000000 'enable stretch

Symbol SSPIF         PIR1.3     'Parallel rad/write finish FLAG
Symbol SSPIE         PIE1.3     
Symbol GIE_GIEH      INTCON.7   'GLOBAL Interrupts enable
Symbol PEIE_GIEL     INTCON.6   'SIDE DEVICES INTERRUPT ENABLE
 GIE_GIEH=1
 PEIE_GIEL=1
 SSPIE=1
 
On_Interrupt GoTo u:
Dim DataIn[30] As Byte

Declare Slow_Bus On
SSPADD  = 0b10100000
SSPSTAT = 0b00000000
SSPCON1 = 0b00111110     
SSPCON2 = 0b00000000
Dim I As Byte
I=-1 
Dim T As Byte
DelayMS 500
main:
       ' HSerOut [Bin8 SSPADD,"-",Bin8 SSPSTAT,"-",Bin8 SSPCON1,"-",Bin8 SSPCON2, 13]
       ' HSerOut ["main-", "-" ,Hex SSPBUF,"-" ,Bin1 SSPIF ,Bin1 BF ,Bin1 SSPOV,Bin1 WCOL,Bin1 S,Bin1 P,13]

        DelayMS 100
        HSerOut [Str DataIn,13]
        Clear DataIn
        DelayMS 100
        Toggle PORTB.7
GoTo main

u:
If SSPIF =1   Then
   T= SSPBUF
   If S=1 And T <> SSPADD  Then 
        Inc I
        DataIn = T
        SSPIF =0
        SSPOV = 0
        WCOL = 0
    End If
    If P=1 Then P=0 :I=-1
    SSPIF=0
    End If   

Context Restore
 


please any help , or code would be appreciable .
 
Top