I2CREAD/WRITE Clarification
+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    1,053

    Default I2CREAD/WRITE Clarification

    Hi
    Iím having some trouble interpreting the manual for these two commands.
    Iíve always fully populated the command arguments when using it, and incremented the address until maximum.

    For reading registers from the FM radio chip Iím playing with, Darrel did this:
    Code:
    i2cread portb.1,portb.7,$20,[Si_REGS(10),Si_REGS(11),Si_REGS(12),Si_REGS(13),Si_REGS(14),Si_REGS(15),_
    Si_REGS(0),Si_REGS(1),Si_REGS(2),Si_REGS(3),Si_REGS(4),Si_REGS(5),Si_REGS(6),Si_REGS(7),Si_REGS(8),Si_REGS(9)],SiError
    Now I do understand why the incoming bytes are ordered the way they are.
    The chip sends them in this order, ie. 0x0A-0x0F, 0x00-0x09, so heís receiving them into their correct RAM locations.

    What I donít understand is what PBP does when you omit an address argument.
    It could send no address at all, or it could begin at zero, and if so, the zero could be either a byte or a word.
    I suspect it sends no address, and the chip begins reading register 0x0A this way.

    Iíve tried every which way to roll this into a loop though, and the code is smaller every time, but fails every time.
    I suspect preceding commands would send no address either, so one example is:
    Code:
    for cnt = 10 to 15
    i2cread portb.1,portb.7,$20,[Si_REGS(cnt)],SiError
    next cnt
    for cnt = 0 to 9
    i2cread portb.1,portb.7,$20,[Si_REGS(cnt)],SiError
    next cnt
    No such luck!

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,685

    Default Re: I2CREAD/WRITE Clarification

    the way the data sheet reads its not possible to read one register at a time . a read starts at reg 0a
    and proceeds sequentially threw the rest of the regs for that transaction. all read transactions begin at reg 0a
    a chip that can have random reads has an additional parameter added after the address in the i2cread command
    eg I2Cread SDA,SCL,rtcadd,rtcloc,[secs,minu,hrs,Dom,dow,mon,year]
    For read operations, the device acknowledge is
    followed by an eight bit data word shifted out on falling
    SCLK edges. An internal address counter automatically
    increments to allow continuous data byte reads, starting
    with the upper byte of register 0Ah, followed by the
    lower byte of register 0Ah, and onward until the lower
    byte of the last register is reached. The internal address
    counter then automatically wraps around to the upper
    byte of register 00h and proceeds from there until
    continuous reads cease.
    After each byte of data is read,
    the controller IC must drive an acknowledge (SDIO = 0)
    if an additional byte of data will be requested. Data
    transfer ends with the STOP condition. After every
    STOP condition, the internal address counter is reset.
    For details on timing specifications and diagrams, refer
    to Table 7, “2-Wire Control Interface
    Characteristics1,2,3,” on page 10, Figure 5, “2-Wire
    Control Interface Read and Write Timing Parameters,”
    on page 11 and Figure 6, “2-Wire Control Interface
    Read and Write Timing Diagram,” on page 11
    .
    Last edited by richard; - 19th April 2019 at 07:17.
    This is more entertaining than Free to Air TV

  3. #3
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    1,053

    Default Re: I2CREAD/WRITE Clarification

    Wouldn’t it only look like a seperate read because the control code is included each time?
    Maybe there’s a big list of RAM locations in it’s asm that could be looped.

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    1,685

    Default Re: I2CREAD/WRITE Clarification

    nope , i2c is transactional . it begins with a i2c start and terminates with an i2c stop you cannot break the sequence
    Last edited by richard; - 19th April 2019 at 07:43. Reason: added pic
    This is more entertaining than Free to Air TV

  5. #5
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    1,053

    Default Re: I2CREAD/WRITE Clarification

    outside PBP should be able to.

    Code:
     MOVLW 0x6         
     MOVWF 0x36        
     MOVLW 0x2         
     MOVWF 0x34        
     MOVLW 0x6         
     MOVWF 0x37        
     MOVLW 0x80        
     MOVWF 0x35        
    
     MOVLW 0x20        ;send control byte
     CALL 0x1d         
     BTFSC STATUS, 0   
     GOTO 0x413        ;probably the error label lcation
    
     CALL 0x4          ;get last byte of first word
     BTFSC STATUS, 0   
     GOTO 0x413        
     MOVWF 0x4d        
     CALL 0x4          ;get first byte of first word
     BTFSC STATUS, 0   
     GOTO 0x413        
     MOVWF 0x4c        
    
     CALL 0x4          ;get last byte of second word
     BTFSC STATUS, 0   
     GOTO 0x413        
     MOVWF 0x4f        
     CALL 0x2          'get first byte of second word
     BTFSC STATUS, 0   
     GOTO 0x413        
     MOVWF 0x4e
    Code:
    i2cread portb.1,portb.7,$20,[Si_REGS(10),Si_REGS(11)],SiError

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    1,685

    Default Re: I2CREAD/WRITE Clarification

    i'm saying the chip does not support random reads it would need to if you break a transaction with a stop condition
    random reads look like this

    if you just want to save space i wonder if str works with i2c , might be worth a try
    regs var byte[16] ' or whatever
    i2cread portb.1,portb.7,$20,[str REGS/16]
    This is more entertaining than Free to Air TV

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    1,685

    Default Re: I2CREAD/WRITE Clarification

    This is more entertaining than Free to Air TV

  8. #8
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    1,053

    Default Re: I2CREAD/WRITE Clarification

    Maybe sending the array as a string would work for a byte array, but for a word array Iíd expect it to shuffle all high and low bytes.
    It could be possible to shuffle them everywhere else in code to accommodate that.

    This works.
    Iím not sure if it saves code space yet, but I will be able to.


    Code:
    asm
     MOVLW 0x6         ;i2c read
     MOVWF 0x36        
     MOVLW 0x2         
     MOVWF 0x34        
     MOVLW 0x6         
     MOVWF 0x37        
     MOVLW 0x80        
     MOVWF 0x35        
     MOVLW 0x20        ;send control code
     CALL 0x1d         
     BTFSC STATUS, 0   
     GOTO _SiError
    endasm
    
    for cnt = 10 to 15
    @ CALL 0x4          
    @ BTFSC STATUS, 0   
    @ GOTO _SiError   
    @ MOVWF _arg		;0x4d
    Si_REGS(cnt) = arg
    Si_REGS(cnt) = Si_REGS(cnt) << 8
    @ CALL 0x4          
    @ BTFSC STATUS, 0   
    @ GOTO _SiError      
    @ MOVWF _arg		;0x4c        
    Si_REGS(cnt) = Si_REGS(cnt) + arg
    next cnt
    
    for cnt = 0 to 8
    @ CALL 0x4          
    @ BTFSC STATUS, 0   
    @ GOTO _SiError   
    @ MOVWF _arg		;
    Si_REGS(cnt) = arg
    Si_REGS(cnt) = Si_REGS(cnt) << 8
    
    @ CALL 0x4          
    @ BTFSC STATUS, 0   
    @ GOTO _SiError      
    @ MOVWF _arg		;   
    Si_REGS(cnt) = Si_REGS(cnt) + arg
    next cnt
    
    
    asm     
     CALL 0x4          
     BTFSC STATUS, 0   
     GOTO 0x419        
     MOVWF 0x4b        
    
     CALL 0x2          
     BTFSC STATUS, 0   
     GOTO 0x419        
     MOVWF 0x4a        
    endasm
    Code:
    i2cread portb.1,portb.7,$20,[Si_REGS(10),Si_REGS(11),Si_REGS(12),Si_REGS(13),Si_REGS(14),Si_REGS(15),_
    Si_REGS(0),Si_REGS(1),Si_REGS(2),Si_REGS(3),Si_REGS(4),Si_REGS(5),Si_REGS(6),Si_REGS(7),Si_REGS(8),Si_REGS(9)],SiError

  9. #9
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    2,891

    Default Re: I2CREAD/WRITE Clarification

    Well, may be I am missing Breks point, but why bother to do that?

    Ioannis

  10. #10
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    1,053

    Default Re: I2CREAD/WRITE Clarification

    To save program memory. Not in the i2c routines PBP puts a the start of the program, but for the calls to them.

    I2CREAD/WRITE puts the same chunk of code to send the control byte which can be split to a subroutine if it’s always the same.
    Long lists of array elements like in the first post can be cycled in a couple of loops (because that example is not entirely in order).
    If all of the I2CREAD/WRITE commands use the same pins on the pic, some code can be saved selecting them.

    If the last real I2CREAD or I2CWRITE command is removed from the PBP program,
    PBP stops putting the i2c routines in the program, and that becomes a bit more of a hassle.

  11. #11
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    2,891

    Default Re: I2CREAD/WRITE Clarification

    Oh I see. Thanks for the explanation.

    Ioannis

Similar Threads

  1. DT Instant Interrupts - Clarification?
    By Aussie Barry in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 26th December 2013, 12:45
  2. Replies: 4
    Last Post: - 7th April 2013, 00:46
  3. Pro upgrade clarification
    By fowardbias in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th January 2010, 21:17
  4. Newbee Hardware PWM clarification needed
    By emmett brown in forum General
    Replies: 3
    Last Post: - 9th July 2006, 09:18
  5. I2cwrite - I2cread
    By savnik in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 5th July 2006, 01:12

Members who have read this thread : 17

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts