I2CRead & I2CWrite not working as expected


Closed Thread
Results 1 to 31 of 31

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    Quote Originally Posted by CuriousOne View Post
    Well, if there's syntax error, compiler should warn about it, right?
    no the compiler produces code to I2CREAD DPIN,CPIN from device $A0 @eeprom address B0,X then read nothing as asked for
    there is no error

    I tried to add brackets and now it starts working, but in the weird way.


    I2CWRITE DPIN,CPIN,$A0,B0,[127]
    Writes 127 into eeprom
    but if I substitute variable there, no matter, byte or word
    after reading, I'm getting zeroes.
    produce a compliable example that demonstrates the problem
    Warning I'm not a teacher

  2. #2
    Join Date
    Feb 2013
    Posts
    1,132


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    Code:
    DPIN Var PORTA.2       ' I2C data pin
    CPIN Var PORTA.1       ' I2C clock pin
    B0   Var word
    B1   Var byte
    B2   Var byte
    X var byte
    x=0
    
    
       For B0 = 0 To 15    
       b1=b2
         I2CWRITE DPIN,CPIN,$A0,B0,[99]  
         Pause 10        '
    pause 100 
       Next B0
    
    
    mainloop:
    
    
       For B0 = 0 To 15   
         I2CREAD DPIN,CPIN,$A0,B0,[X]  
         lcdout $fe, $1, dec b0, " ", dec X 
         pause 1000
       Next B0
     
       Goto mainloop
     
       End

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,650


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    Code:
    DPIN Var PORTA.2       ' I2C data pin
    CPIN Var PORTA.1       ' I2C clock pinB0   Var wordB1   Var byteB2   Var byteX var bytex=0   For B0 = 0 To 15       b1=b2     I2CWRITE DPIN,CPIN,$A0,B0,[99]       Pause 10        'pause 100    Next B0mainloop:   For B0 = 0 To 15        I2CREAD DPIN,CPIN,$A0,B0,[X]       lcdout $fe, $1, dec b0, " ", dec X      pause 1000   Next B0    Goto mainloop  
    End
    so we guess whats wrong then

    DPIN,CPIN are analog
    the chip[undefined] has no porta
    the vcc is too low
    the clock is too fast
    the lcd is not setup properly
    the config settings are incorrect
    the interrupt in the unseen part of the code is ruining the timing
    what's the point
    Last edited by richard; - 10th October 2021 at 08:38.
    Warning I'm not a teacher

  4. #4
    Join Date
    May 2013
    Location
    australia
    Posts
    2,650


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    this

    Code:
    '*    pic16f1825      :  eprom addr =$a0   
    #CONFIG
                 __config        _CONFIG1,    _FOSC_INTOSC & _CP_OFF & _WDTE_OFF &  _PWRTE_ON  &  _MCLRE_ON  & _CLKOUTEN_OFF
                  __config      _CONFIG2, _PLLEN_ON & _LVP_OFF
    #ENDCONFIG
    
    
    DEFINE OSC 32
    OSCCON=$70
    ANSELA=0
    ANSELC=0
    trisa.0=0
    DEFINE I2C_SLOW 1 
    ANSELA = 0
    B0   Var word
    X var byte
    SCL var Porta.2                     
    SDA var Porta.1
    lata.0=1
    DEFINE DEBUG_REG PORTA
    DEFINE DEBUG_BIT 0      ;  if not used for pwr  
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 0     
    pause 2000
    Debug 13,10,"Start",13 ,10
    clear
    For B0 = 0 To 15 
     I2CWRITE sda,scl,$A0,B0,[99]  
     Pause 10        '
    Next B0
    pause 100
    mainloop:
        For B0 = 0 To 15   
         I2CREAD sda,scl,$A0,B0,[X]  
         debug 13,10, dec b0, " ", dec X 
        Next B0 
        pause 1000
    Goto mainloop
    end
    produces as expected this
    Code:
    Start
    
    
    0 99
    1 99
    2 99
    3 99
    4 99
    5 99
    6 99
    7 99
    8 99
    9 99
    10 99
    11 99
    12 99
    13 99
    14 99
    15 99
    Warning I'm not a teacher

  5. #5
    Join Date
    Feb 2013
    Posts
    1,132


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    Pins are configured properly, LCD also working fine.

    Code:
    TRISA=%00000000  'SET A TO OUTPUT   
    TRISC=%00001101   
    TRISB=%00011000   
    ANSELH=%00000000   ' ADC OFF B
    ANSEL=%000000000 ' turn off ADC
    ADCON1=%10000000  'adc justify
    OSCCON=%01110101  'SET FREQUENCY TO 8MHZ
    WPUB=%00000000    'turn off Pullups
    CM1CON0=0         'DISABLE COMPARATORS
    CM2CON0=0
    This code works, if there is a number [99] or whatever in the write loop
    If I modify that part to look like
    I2CWRITE DPIN,CPIN,$A0,B0,[B1]
    or any other variable instead of B1
    all zeros get written into there, instead of actual variable value.

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,650


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    This code works, if there is a number [99] or whatever in the write loop
    If I modify that part to look like
    I2CWRITE DPIN,CPIN,$A0,B0,[B1]
    or any other variable instead of B1
    all zeros get written into there, instead of actual variable value.
    what code ?
    minimal, complete and verifiable example MCVE , its not hard

    this code
    Code:
    '*    pic16f1825      :  eprom addr =$a0   #CONFIG
                 __config        _CONFIG1,    _FOSC_INTOSC & _CP_OFF & _WDTE_OFF &  _PWRTE_ON  &  _MCLRE_ON  & _CLKOUTEN_OFF
                  __config      _CONFIG2, _PLLEN_ON & _LVP_OFF
    #ENDCONFIG
    
    
    DEFINE OSC 32
    OSCCON=$70
    ANSELA=0
    ANSELC=0
    trisa.0=0
    DEFINE I2C_SLOW 1 
    ANSELA = 0
    B0   Var word
    b2 var byte
    X var byte
    SCL var Porta.2                     
    SDA var Porta.1
    lata.0=1
    DEFINE DEBUG_REG PORTA
    DEFINE DEBUG_BIT 0      ;  if not used for pwr  
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 0     
    pause 2000
    Debug 13,10,"Start",13 ,10
    clear
    For B0 = 0 To 15 
      b2=99+b0  
     I2CWRITE sda,scl,$A0,B0,[b2]  
     Pause 10        '
    Next B0
    pause 100
    mainloop:
        For B0 = 0 To 15 
      
         I2CREAD sda,scl,$A0,B0,[X]  
         debug 13,10, dec b0, " ", dec X 
        Next B0 
        pause 1000
    Goto mainloop
    end

    produces as expected




    Code:
    Start
    
    0 99
    1 100
    2 101
    3 102
    4 103
    5 104
    6 105
    7 106
    8 107
    9 108
    10 109
    11 110
    12 111
    13 112
    14 113
    15 114
    Warning I'm not a teacher

  7. #7
    Join Date
    Feb 2013
    Posts
    1,132


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    The code which I've posted, which writes 99 into eeprom.
    I replace 99 with any other value - it works.
    I put variable name into brackets - it writes zeroes into eeprom.
    no matter what type of variable or what value do it has.

Similar Threads

  1. I2cwrite - I2cread
    By savnik in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 5th July 2006, 01:12
  2. Problem with I2Cread and I2CWRITE function
    By Tony85 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th June 2006, 20:03
  3. Problem with I2Cread and I2CWRITE function
    By Tony85 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 6th June 2006, 18:32
  4. PFC8583 Connection and I2Cwrite I2cread!
    By uludere72 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 27th February 2006, 13:32
  5. I2CWRITE and I2CREAD
    By Tomas in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 23rd April 2004, 02:30

Members who have read this thread : 1

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