i2c communications

Discussion in 'Embedded Systems and Microcontrollers' started by ayhampic, Aug 19, 2011.

  1. ayhampic

    Thread Starter New Member

    Aug 12, 2011
    19
    0
    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
    Code ( (Unknown Language)):
    1.  
    2. Device = 18F4550                                              
    3. Xtal = 20  
    4. HBStart
    5. SSPBUF=0
    6.  
    7. TXSTA.5=1 ' setting Transmit Enable Bit
    8. RCSTA.7=1
    9. RCSTA.4=1
    10. Hserial_Baud 19200 ' Setting Baud rate
    11.  
    12.  
    13. Symbol SMP = SSPSTAT.7       '1:enable high speed , 0: slow speed
    14. Symbol CKE = SSPSTAT.6       '0 always for me
    15. Symbol DATA_ADDR = SSPSTAT.5 '[s]1:last byte was data, 0:last byte was address
    16. Symbol P = SSPSTAT.4         'I2C Stop detection
    17. Symbol S = SSPSTAT.3         'I2C Start detection
    18. Symbol R_W = SSPSTAT.2       '[s]1:read 0:write operation [m]1:inprogress 0:may not
    19. Symbol UA = SSPSTAT.1        '1:sspadd need update [10bit only]
    20. Symbol BF = SSPSTAT.0        'Buffer Full Status flag
    21.  
    22.  
    23. Symbol WCOL  = SSPCON1.7  'Write Collision Detectection
    24. Symbol SSPOV = SSPCON1.6  'Receive Overflow Indicator bit
    25. Symbol SSPEN = SSPCON1.5  'mssp Enable bit
    26. Symbol CKP   = SSPCON1.4  'SCK Release Control bit    ???????????????????????? 1
    27. Symbol SSPM3 = SSPCON1.3  '1111:[S10+INT],1110:[S7+INT],1011:[S IDLE]
    28. Symbol SSPM2 = SSPCON1.2  '1000:MASTER F/4(SSPADD+1)
    29. Symbol SSPM1 = SSPCON1.1  '0111[S10 NO INT]
    30. Symbol SSPM0 = SSPCON1.0  '0110[S 7BIT]
    31.  
    32.  
    33. Symbol GCEN    = SSPCON2.7 '[S] General call enable bit
    34. Symbol ACKSTAT = SSPCON2.6 '1:ack NOT receied,0:ack received (Master Transmitonly)
    35. Symbol ACKDT   = SSPCON2.5 '1:ack 0:nack  (Master Receive mode only)
    36. Symbol ACKEN   = SSPCON2.4 'Acknowledge Sequence Enable bit (Master Receive mode only)
    37. Symbol RCEN    = SSPCON2.3 '1:Receive Enable 0:disabled (Master receive mode only)
    38. Symbol PEN     = SSPCON2.2 'Stop Condition Enable bit (Master mode only)
    39. Symbol RSEN    = SSPCON2.1 'Repeated Start Condition Enabled bit (Master mode only)
    40. Symbol SEN     = SSPCON2.0 'Start Condition Enabled/Stretch Enabled bit
    41.  
    42.  
    43. Symbol S_GCEN    = SSPCON2.7 '[S] General call enable bit
    44. Symbol S_ACKSTAT = SSPCON2.6 'not used in slave
    45. Symbol S_ACKDT   = SSPCON2.5 '1:enable address masking,0:disabled
    46. Symbol S_ACKEN   = SSPCON2.4 '1:Enable address masking,0:disabled
    47. Symbol S_RCEN    = SSPCON2.3 '1:Enable address masking,0:disabled
    48. Symbol S_PEN     = SSPCON2.2 '1:Enable address masking,0:disabled
    49. Symbol S_RSEN    = SSPCON2.1 '[7bit]1:masking of sspadd<1> [10]1:masking of <0:1>
    50. Symbol S_SEN     = SSPCON2.0 'enable clock stretch 0:disable
    51.  
    52.  
    53. Dim i As Byte
    54. Dim t As Byte
    55. Dim DataOut As String * 30
    56. Declare Slow_Bus On
    57.  
    58. SSPADD  = 49     '100kHz
    59. SSPSTAT =  0b00000000
    60. SSPCON2 =  0b00001000
    61. SSPCON1 =  0b00101000
    62. DelayMS 500
    63. Output PORTB.7
    64.    
    65.  
    66. i=-1
    67. main:
    68.  Toggle PORTB.7
    69.  DelayMS 100
    70.  HBusOut 0b10100000,["master say hello"]
    71.  Toggle PORTB.7
    72.  DelayMS 500
    73. GoTo main
    74.  
    75. [/S][/S][/s][/s]


    slave :
    Code ( (Unknown Language)):
    1.  
    2. Device = 18F4550                                              
    3. Xtal = 20  
    4. Output PORTB.7
    5.  
    6. TXSTA.5=1 ' setting Transmit Enable Bit
    7. RCSTA.7=1
    8. RCSTA.4=1
    9. Hserial_Baud 19200 ' Setting Baud rate
    10.  
    11. LCD_Interface 4
    12. LCD_DTPin PORTB.4
    13. LCD_RSPin PORTB.3
    14. LCD_ENPin PORTB.2
    15.  
    16.  
    17. 'ADCON1=0eh
    18. 'Declare Hbus_Bitrate 100
    19. Input PORTB.0
    20. Input PORTB.1
    21.  
    22. 'master
    23. 'SSPADD = 49
    24. SSPADD = 0b11110000
    25.  
    26. Symbol SMP = SSPSTAT.7       '1:enable high speed , 0: slow speed
    27. Symbol CKE = SSPSTAT.6       '0 always for me
    28. Symbol DATA_ADDR = SSPSTAT.5 '[s]1:last byte was data, 0:last byte was address
    29. Symbol P = SSPSTAT.4         'I2C Stop detection
    30. Symbol S = SSPSTAT.3         'I2C Start detection
    31. Symbol R_W = SSPSTAT.2       '[s]1:read 0:write operation [m]1:inprogress 0:may not
    32. Symbol UA = SSPSTAT.1        '1:sspadd need update [10bit only]
    33. Symbol BF = SSPSTAT.0        'Buffer Full Status flag
    34.  
    35. 'master:
    36. 'SSPSTAT = 0b00000000
    37. SSPSTAT = 0b00000000
    38. Symbol WCOL  = SSPCON1.7  'Write Collision Detectection
    39. Symbol SSPOV = SSPCON1.6  'Receive Overflow Indicator bit
    40. Symbol SSPEN = SSPCON1.5  'mssp Enable bit
    41. Symbol CKP   = SSPCON1.4  'SCK Release Control bit    ???????????????????????? 1
    42. Symbol SSPM3 = SSPCON1.3  '1111:[S10+INT],1110:[S7+INT],1011:[S IDLE]
    43. Symbol SSPM2 = SSPCON1.2  '1000:MASTER F/4(SSPADD+1)
    44. Symbol SSPM1 = SSPCON1.1  '0111[S10 NO INT]
    45. Symbol SSPM0 = SSPCON1.0  '0110[S 7BIT]
    46. 'MASTER
    47. 'SSPCON1=0b00101000
    48. SSPCON1=0b00101110
    49. Symbol GCEN    = SSPCON2.7 '[S] General call enable bit
    50. Symbol ACKSTAT = SSPCON2.6 '1:ack receied,0:ack not received (Master Transmitonly)
    51. Symbol ACKDT   = SSPCON2.5 '1:ack 0:nack  (Master Receive mode only)
    52. Symbol ACKEN   = SSPCON2.4 'Acknowledge Sequence Enable bit (Master Receive mode only)
    53. Symbol RCEN    = SSPCON2.3 '1:Receive Enable 0:disabled (Master receive mode only)
    54. Symbol PEN     = SSPCON2.2 'Stop Condition Enable bit (Master mode only)
    55. Symbol RSEN    = SSPCON2.1 'Repeated Start Condition Enabled bit (Master mode only)
    56. Symbol SEN     = SSPCON2.0 'Start Condition Enabled/Stretch Enabled bit
    57. 'SSPCON2=0b000000000      '4
    58.  
    59. 'slave mode
    60. Symbol S_GCEN    = SSPCON2.7 '[S] General call enable bit
    61. Symbol S_ACKSTAT = SSPCON2.6 'not used in slave
    62. Symbol S_ACKDT   = SSPCON2.5 '1:enable address masking,0:disabled
    63. Symbol S_ACKEN   = SSPCON2.4 '1:Enable address masking,0:disabled
    64. Symbol S_RCEN    = SSPCON2.3 '1:Enable address masking,0:disabled
    65. Symbol S_PEN     = SSPCON2.2 '1:Enable address masking,0:disabled
    66. Symbol S_RSEN    = SSPCON2.1 '[7bit]1:masking of sspadd<1> [10]1:masking of <0:1>
    67. Symbol S_SEN     = SSPCON2.0 'enable clock stretch 0:disable
    68. SSPCON2=0b000000000 'enable stretch
    69.  
    70. Symbol SSPIF         PIR1.3     'Parallel rad/write finish FLAG
    71. Symbol SSPIE         PIE1.3    
    72. Symbol GIE_GIEH      INTCON.7   'GLOBAL Interrupts enable
    73. Symbol PEIE_GIEL     INTCON.6   'SIDE DEVICES INTERRUPT ENABLE
    74.  GIE_GIEH=1
    75.  PEIE_GIEL=1
    76.  SSPIE=1
    77.  
    78. On_Interrupt GoTo u:
    79. Dim DataIn[30] As Byte
    80.  
    81. Declare Slow_Bus On
    82. SSPADD  = 0b10100000
    83. SSPSTAT = 0b00000000
    84. SSPCON1 = 0b00111110    
    85. SSPCON2 = 0b00000000
    86. Dim I As Byte
    87. I=-1
    88. Dim T As Byte
    89. DelayMS 500
    90. main:
    91.        ' HSerOut [Bin8 SSPADD,"-",Bin8 SSPSTAT,"-",Bin8 SSPCON1,"-",Bin8 SSPCON2, 13]
    92.        ' HSerOut ["main-", "-" ,Hex SSPBUF,"-" ,Bin1 SSPIF ,Bin1 BF ,Bin1 SSPOV,Bin1 WCOL,Bin1 S,Bin1 P,13]
    93.  
    94.         DelayMS 100
    95.         HSerOut [Str DataIn,13]
    96.         Clear DataIn
    97.         DelayMS 100
    98.         Toggle PORTB.7
    99. GoTo main
    100.  
    101. u:
    102. If SSPIF =1   Then
    103.    T= SSPBUF
    104.    If S=1 And T <> SSPADD  Then
    105.         Inc I
    106.         DataIn[I] = T
    107.         SSPIF =0
    108.         SSPOV = 0
    109.         WCOL = 0
    110.     End If
    111.     If P=1 Then P=0 :I=-1
    112.     SSPIF=0
    113.     End If  
    114.  
    115. Context Restore
    116.  
    117. [/I][/S][/S][/s][/s]


    please any help , or code would be appreciable .
     
Loading...