I2CRead & I2CWrite not working as expected


Closed Thread
Results 1 to 31 of 31

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1,079


    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 ?

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


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    Maybe read routine is faulty?

  3. #3
    Join Date
    Feb 2013
    Posts
    1,079


    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.

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


    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: 5522
Size:  191.5 KB



    Warning I'm not a teacher

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


    Did you find this post helpful? Yes | No

    Default Re: I2CRead & I2CWrite not working as expected

    I will use everything different - LCD, PIC, EEPROM and will check again...

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 : 8

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