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,696


    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

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


    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.

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


    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

  4. #4
    Join Date
    Feb 2013
    Posts
    1,158


    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.

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


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    Here is full complete code.

    Code:
    ;----[16F886 Hardware Configuration]--------------------------------------------
    #CONFIG
    cfg1 = _INTRC_OSC_NOCLKOUT    ; INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
    cfg1&= _WDT_ON                ; WDT enabled
    cfg1&= _PWRTE_OFF             ; PWRT disabled
    cfg1&= _MCLRE_OFF             ; RE3/MCLR pin function is digital input, MCLR internally tied to VDD
    cfg1&= _CP_OFF                ; Program memory code protection is disabled
    cfg1&= _CPD_OFF               ; Data memory code protection is disabled
    cfg1&= _BOR_OFF               ; BOR disabled
    cfg1&= _IESO_ON               ; Internal/External Switchover mode is enabled
    cfg1&= _FCMEN_ON              ; Fail-Safe Clock Monitor is enabled
    cfg1&= _LVP_OFF               ; RB3 pin has digital I/O, HV on MCLR must be used for programming
    cfg1&= _DEBUG_OFF             ; In-Circuit Debugger disabled, RB6/ICSPCLK and RB7/ICSPDAT are general purpose I/O pins
      __CONFIG _CONFIG1, cfg1
    
    
    cfg2 = _BOR40V                ; Brown-out Reset set to 4.0V
    cfg2&= _WRT_OFF               ; Write protection off
      __CONFIG _CONFIG2, cfg2
    
    
    #ENDCONFIG
    
    
    'chip configs
    TRISA=%00000000  'SET A TO OUTPUT   1=input
    TRISC=%00001101   'set half C for in/out
    TRISB=%00011000   'set PortB to output
    ANSELH=%00000000   ' ADC OFF B
    ANSEL=%000000000 'configure PortA as digital except first 2
    ADCON1=%10000000  'adc justify
    OSCCON=%01110101  'SET FREQUENCY TO 8MHZ
    WPUB=%00000000    'turn off Pullups
    CM1CON0=0         'DISABLE COMPARATORS
    CM2CON0=0         'SAME HERE
    'CCP1CON=%01000000 ' configure pwm
    'PSTRCON=%00010110 'disable C pwm
    
    
    DEFINE OSC 8   
    DEFINE ADC_BITS 10
    DEFINE ADC_CLOCK 3
    DEFINE ADC_SAMPLEUS 50
    'lcd config
    pause 200
    ' Set LCD Data port 
    DEFINE LCD_DREG PORTC  
    DEFINE LCD_DBIT 4  
    DEFINE LCD_RSREG PORTB  
    DEFINE LCD_RSBIT 0  
    DEFINE LCD_EREG PORTB  
    DEFINE LCD_EBIT 1  
    DEFINE LCD_BITS 4  
    DEFINE LCD_LINES 2  
    DEFINE LCD_COMMANDUS 1500  
    DEFINE LCD_DATAUS 44
    define i2c_slow 1
    
    
    
    
    DPIN Var PORTA.2       ' I2C data pin
    CPIN Var PORTA.1       ' I2C clock pin
    B   Var word
    B1   Var byte
    B2   Var byte
    
    
    For B = 0 To 15 
      b1=99+b 
     I2CWRITE dpin,cpin,$A0,B,[b1]  
     Pause 10        '
    Next 
    pause 100
    
    
    mainloop:
    
    
       For B = 0 To 15   
         I2CREAD DPIN,CPIN,$A0,B,[b2]  
         lcdout $fe, $1, dec b, " ", dec b2 
         pause 1000
       Next 
     
       Goto mainloop
     
       End
    And funny thing happens - the returned value of B2, as shown on display, is 114! for all 16 locations.
    looks like 1st loop executed before writing to EEPROM ?

  6. #6
    Join Date
    Feb 2013
    Posts
    1,158


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    Maybe read routine is faulty?

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


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    yes it is read routine - it reads eeprom only once.

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,696


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    works perfectly on simulator

    had to increase some delays for slow old lcd
    pause 1000
    ' Set LCD Data port
    DEFINE LCD_DREG PORTC
    DEFINE LCD_DBIT 4
    DEFINE LCD_RSREG PORTB
    DEFINE LCD_RSBIT 0
    DEFINE LCD_EREG PORTB
    DEFINE LCD_EBIT 1
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 2
    DEFINE LCD_COMMANDUS 2000
    DEFINE LCD_DATAUS 44
    lcdout $fe, $1, "ready " ;added to prove lcd working
    pause 1000


    Name:  sch.jpg
Views: 15718
Size:  191.5 KB



    Warning I'm not a teacher

Similar Threads

  1. I2cwrite - I2cread
    By savnik in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 5th July 2006, 02:12
  2. Problem with I2Cread and I2CWRITE function
    By Tony85 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th June 2006, 21:03
  3. Problem with I2Cread and I2CWRITE function
    By Tony85 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 6th June 2006, 19:32
  4. PFC8583 Connection and I2Cwrite I2cread!
    By uludere72 in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 27th February 2006, 14:32
  5. I2CWRITE and I2CREAD
    By Tomas in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 23rd April 2004, 03: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