MASTER AND SLAVE I2C 18F4550 question

Status
Not open for further replies.

ayhampic

Member level 1
Joined
Aug 3, 2011
Messages
40
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,599
hi,

I have a Project which uses a two pic18f4550 as master and slave in order to communicate to each other , When the master send a string , the slave received it correctly .And that based on the interrupts , But the problem is : the slave accept any transition from the master even if the address was wrong ?

my questions are :

are the addressing and the acceptance of the address done in the hardware or in the software ?
now how could I send a text from a slave to the master ??

my code is :
master 18f4550 , 100khz
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 '[s]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       '[s]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 '[S] 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 '[S] 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
and the slave 7bit 0b1010000 , interrupt-able :

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 '[s]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       '[s]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 '[S] 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 '[S] 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[I] = T
        SSPIF =0
        SSPOV = 0
        WCOL = 0
    End If
    If P=1 Then P=0 :I=-1
    SSPIF=0
    End If   

Context Restore


regards
ayham
 

Reference: Microchip Appnote: **broken link removed**

are the addressing and the acceptance of the address done in the hardware or in the software ?

The slave address must be setup by loading the SSPADD register.


There is a source code example of establishing communication between two PICs using the I2C in the PDF.

Code for example described in PDF

I have additional examples in C, if you would like to see them as well?

BigDog
 

thanks bigdogguru ,

please check the attachment it is an small picture, and yes I would like to have an extra examples .
I really appreciate you help .

---------- Post added at 21:55 ---------- Previous post was at 21:53 ----------

it would be possible to achieve a 2way communication between pics using i2c ?
 

it would be possible to achieve a 2way communication between pics using i2c ?

Certainly the Microchip appnote details just such a design and steps you through the setup process.

I post other examples shortly.

BigDog

---------- Post added at 22:05 ---------- Previous post was at 21:58 ----------

Here are additional examples:

I2C communication between PICs

Using the I2C Bus

Pic to Pic I2C

These should get you started in the right direction, read the Microchip appnote carefully and if you have any additional questions just post them.

BigDog
 

in you opinion which is the best programming language for PICs 18family and the newer versions ?
is proton compiler a good choice ?
regards .
 

in you opinion which is the best programming language for PICs 18family and the newer versions ?
is proton compiler a good choice ?

Proton is a good language if you like and are familiar with BASIC. However, I am partial to C and Assembly for embedded programming. I use both Hi-Tech's and Microchip's C compilers for my commerical jobs. I've heard CCS is quite good, but I have yet to try it.

BigDog
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…