I2CWRITE and 18F4680


Closed Thread
Results 1 to 40 of 41

Hybrid View

  1. #1
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151

    Question I2CWRITE and 18F4680

    Hello All-
    I have what I suspect is a simple error on my part - but I can not seem to see it (trees?)

    I am trying to use a MCP23016 and the I2C commands in PBP on an 18f4680. I have used I2C commands on another proc and it worked great with the RTC that I needed - but, this time I can not seem to get it to work.

    What I have done:
    1. Pulled both the SCL and SDA lines up to +5.
    2. All 3 address lines are grounded.
    3. 3.9k and 33pF used for CLKIN.
    4. All 3 pins (1,8,19) are grounded.
    5. TP is left floating
    6. Written test code to wiggle the chip which is surface mount - I can not seem to get an output from the 23016.
    Observed:
    By a scope on the SDA and SCL lines I found that SDA is LOW all the time and the SCL is HIGH all the time. So I wrote test code to toggle these 2 lines and I observed the 2 lines behaving as I would suspect.

    It seems as if when I use the PBP command I2CWRITE it does not wiggle the 2 lines needed. I believe I must get this fundamental thing to work before I attempt to debug how to communicate with the 23016.

    I have read several posts regarding the 23016 which is why I decided to try this part (hope I get this work ).

    I have set the registers according to other posts found on this forum for this chip (thanks Melanie) but it seems as if this command is not working (which I know is not the case) so I am perplexed.....

    Summary-
    I can write test code to toggle the PORTC.4 pin (SDA) and the PORTC.3 pin (SCL) but I do not see any change when writing in a loop to the 23016 using I2CWRITE. I know the chip is working because I have other LEDs that I toggle in the loop to ensure we are not hung somewhere. It is working at 40 MHz - should I try a slower speed?

    Any ideas?

    Thanks in advance,
    Steve

  2. #2
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ecoli-557 View Post
    I have what I suspect is a simple error on my part - but I can not seem to see it (trees?)
    One error...no code shown...

    It is working at 40 MHz - should I try a slower speed?
    Have you tried? If you're using 40Mhz, chances are that you're using the internal PLL. Changing to 10Mhz would be a simple 4xPLL bit change... By definition, it would be slower.

  3. #3
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default

    I am using an external oscillator and not the internals. I will post the test code - no problem but what I can't get is WHY the pins don't toggle when I use the I2C command - they should shouldn't they?

  4. #4
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by ecoli-557 View Post
    I am using an external oscillator and not the internals.
    I know that. And if you read the datasheet you'll see references to the 4xPLL. You must be using an external 40Mhz clock if you're not using the internal PLL.

    I will post the test code - no problem but what I can't get is WHY the pins don't toggle when I use the I2C command - they should shouldn't they?
    Then post the code...

  5. #5
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Post

    Code I am using is shown below- The remarked lines are for FULL use.........
    When I manually toggle both I2C pins by the same method I use for the 'heartbeat' LED, they work as they should. (Had to cut code to 10,000 chars <grin>).
    '---------------------------------------------------------------------------------------
    'Define configuration bits - 'Set for using PIC18F4680 - 'Program using the USB MELABS programmer
    '*** NOTICE ***
    'The INC file in PBP has been modified to allow TOTAL CONFIGURATION of the fuses!

    @ __CONFIG _CONFIG1H, _OSC_ECIO_1H & _FCMEN_ON_1H
    'External oscillator, RA6 GIO
    'Fail-safe clk monitor ENABLED
    @ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOREN_BOHW_2L
    'Power up Timer ENABLED
    'Brown-out reset ENABLED in h-ware only
    @ __CONFIG _CONFIG2H, _WDT_OFF_2H
    'Watchdog DISABLED
    @ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _PBADEN_OFF_3H
    'MCLR is DISABLED, RE3 is INPUT
    'PORTB is DIGITAL on reset
    @ __CONFIG _CONFIG4L, _LVP_ON_4L & _STVREN_ON_4L & _XINST_ON_4L & _DEBUG_OFF_4L
    'Single supply ICSP ENABLED
    'Stack Overflow reset is ENABLED
    'Enhanced CPU is ENABLED
    'Background debugger DISABLED
    @ __CONFIG _CONFIG5L, _CP0_ON_5L & _CP1_ON_5L & _CP2_ON_5L & _CP3_ON_5L
    'Code Protect Block 0, 1, 2, 3
    '---------------------------------------------------------------------------------------
    'Define the oscillator or crystal used
    DEFINE OSC 40 '40 MHz oscillator, one clk input
    INCLUDE "MODEDEFS.BAS" 'Include Shiftin/out modes if we need them
    '---------------------------------------------------------------------------------------
    define LCD_DREG PORTA
    define LCD_DBIT 0
    define LCD_RSREG PORTA
    define LCD_RSBIT 6
    define LCD_EREG PORTA
    define LCD_EBIT 5
    define LCD_BITS 4 define LCD_LINES 4 define LCD_COMMANDUS 4000
    define LCD_DATAUS 100
    '---------------------------------------------------------------------------------------
    'Lets setup the MCP23016s
    ' define I2C_HOLD 1
    define I2C_SLOW
    '---------------------------------------------------------------------------------------
    'OKAY, Lets set up the registers.....
    CMCON=%00000111 'Disables comparators, Port A
    ADCON0=%00000000 'Turns off ADC, Port A
    ADCON1=%00001111
    CMCON =%00000111 'Turns OFF comparators
    CVRCON=%00000000 'DISABLES comparator voltage module
    ECCP1CON=%00000000 'DISABLES PWMs and such
    '---------------------------------------------------------------------------------------
    'Direction registers
    TRISA = %10000000 'Set PORTA to all output
    TRISB = %11111111 'set all the pins of PORTB to INPUT
    TRISC = %10000111 'Set RC6 (TX), RC3 (SCL), RC4 (SDA)
    'Set RC7 (RX), RC0-RC2 are 3 address lines
    TRISD = %00000000 'Set PORTD to outputs
    TRISE = %00000000 'Set PORTE to outputs except RE3 which can ONLY be input
    '---------------------------------------------------------------------------------------
    'Additional I/O Definitions
    fault var PORTE.1 'FAULT output, low active (LED and Sonalert)
    heart var PORTE.0 'Heartbeat to know we are online
    CTS Var PORTE.2 'Goes high to xmit on RS485
    idata var PORTC.4 'I2C data pin - put 4.7k pullup!
    iclock var PORTC.3 'I2C clock pin - put 4.7k pullup!
    'Setup MCP23016; GP1.0-GP1.7 as inputs, GP0.0-GP0.7 as outputs
    '---------------------------------------------------------------------------------------
    'Variable List
    p_addr var byte 'Address of this processor
    K var byte 'Hex convertor variable
    i var byte 'Loop variable
    s var byte 'Seconds placeholder
    m var byte 'Minutes placeholder
    timers var byte[8] 'An array of 8 bytes to function as timers
    door var byte 'Loop counter
    DPI var byte
    locks var byte
    loop var byte
    i2c_addr var byte 'Address variable for the MCP23016
    i2cdoor var byte[8] 'An array of 8 bytes for I2C doors
    i2cdpi var byte[8] 'An array of 8 bytes for I2C Door Position
    i2cswitch var byte
    i2cled var byte
    '################################################# ############
    '# We are going to start this Pig!
    '################################################# ############
    init:
    pause 1000 'Hold here for a bit while things IPL......
    heart=1 'Turns off HEARTBEAT led
    fault=1 'Turns off FAULT led
    for i2c_addr=0 to 7
    ' i2c_addr=$20 'address 0 for the test
    i2cwrite idata,iclock,$06,i2c_addr,[$00,$FF] 'Sets GP0.0-GP0.7 as OUTPUTS
    pause 20
    i2cwrite idata,iclock,$07,i2c_addr,[$FF,$00] 'Sets GP1.0-GP1.7 as INPUTS
    pause 20
    next i2c_addr
    '################################################# ############
    '# We go around the rest of the subroutines
    '################################################# ############
    Initialize:
    goto startmain 'Jumps around subroutines

    '################################################# ############
    '# Subroutines
    '################################################# ############
    dly300: pause 300
    return

    initlcd:lcdout $fe,1 'Inits the lcd
    return

    get_addr:
    p_addr = PORTC & %00000111
    return

    chirp: for i=1 to 3
    low fault
    pause 50
    high fault
    pause 50
    next i
    return


    errori2c:
    lcdout $FE,2," "
    lcdout $FE,$C0," "
    lcdout $FE,$94,"No ACK from I2C dev "
    lcdout $FE,$D4," "
    pause 2000
    return
    '################################################# ############
    ' Hokay boyz, here we go......
    '################################################# ############
    startmain:
    'This is where it breaks down. I do not get any signals that change at the DATA and CLOCK pins on the proc.
    'DATA pin stays low and the CLOCK pin stays high.
    'I changed this short test area to pulse both pins in a tight loop
    'and that was just fine.
    'It is as if the I2C portion died! But, I am sure something I have done.......
    'I have checked and double-checked the I2C statements and they look fine.....

    check: gosub get_addr 'Just as a check of what addr the board is....for diagnostic
    loop=loop+1 'loop counter for heartbeat led 'pulse'

    ' i2c_addr=$20
    ' i2cread idata,iclock,$01,i2c_addr,[i2cswitch],errori2c
    pause 20 'Pause cause apparently we need to
    '' next i2c_addr 'Go get the next set of switches
    i2c_addr=0
    i2cwrite idata,iclock,0,0,[loop],errori2c
    pause 10 'Display for a while
    if loop=<10 then low heart 'A way to turn off the heartbeat pulse

    if loop=>11 then high heart 'A way to turn on heartbeat pulse

    if loop=20 then loop=0 'Resets us so we know we are alive
    goto check

    end

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    @ __CONFIG _CONFIG4L, _LVP_ON_4L & _STVREN_ON_4L & _XINST_ON_4L & _DEBUG_OFF_4L
    'Single supply ICSP ENABLED
    'Stack Overflow reset is ENABLED
    'Enhanced CPU is ENABLED
    Unless your programmer use this pin to program your PIC, high unlikely, you want to use LVP_OFF, unless it may not work as expected.

    Second test, use the internal OSC or lower value for the external.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    Join Date
    Sep 2007
    Location
    Waco, Texas
    Posts
    151


    Did you find this post helpful? Yes | No

    Default

    Thanks to both.
    Mr. E, I have changed the code to:
    @ __CONFIG _CONFIG4L, _LVP_OFF_4L & _STVREN_ON_4L & _XINST_ON_4L & _DEBUG_OFF_4L

    AND changed the oscillator to 20 MHz:
    DEFINE OSC 20

    and now BOTH lines are low all the time.......

    This is confounding, I am certain it will be a simple thing but I can not see it..... however, I do smell a lot of pine....... <grin>.

    Again, any help no matter how critical would be appreciated. Datasheet reading have proved as helpful as the tea readings.......

Members who have read this thread : 0

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