I2CWRITE/I2CREAD access hardware in the different way? (PCA9685 issues)


+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2013
    Posts
    1,158

    Default I2CWRITE/I2CREAD access hardware in the different way? (PCA9685 issues)

    Hello.
    Trying to interact with PCA9685. No luck so far. Here's the code:

    Code:
    #config
    CONFIG RETEN = OFF	
    CONFIG INTOSCSEL = HIGH
    CONFIG SOSCSEL = DIG
    CONFIG XINST = OFF	    ;Enabled
    CONFIG FOSC = INTIO1
    CONFIG PLLCFG = OFF
    CONFIG FCMEN = OFF	    ;Disabled
    CONFIG PWRTEN = OFF	    ;Disabled
    CONFIG BOREN = OFF	    ;Disabled in hardware, SBOREN disabled
    CONFIG WDTEN = OFF	    ;WDT disabled in hardware; SWDTEN bit disabled
    CONFIG CANMX = PORTB
    CONFIG MCLRE = OFF
    
    
    #endconfig
    
    
    OSCTUNE.6 = 1 ; Enable 4x PLL
    OSCCON = %01110000
    ANCON1=0 'DISABLE ADC D3-D2-D1-D0-   b
    ANCON0=%00000000                      'a
    ADCON0=0
    ADCON1=%11110000
    TRISC=%00000000 'set PORTC as output all
    TRISD=%00000000 'set PORTD as output all
    TRISB=%00000000 'set PORTB as output all
    TRISA=%00000000 'set PORTA as output all
    TRISE=%00000000 'set PORTE as output all
    define OSC 64
    
    
    SCL_PIN VAR PORTC.0   ' I2C clock pin
    SDA_PIN VAR PORTC.1   ' I2C data pin
    
    
    PCA9685_CTRL_W CON $80   ' 8-bit write control byte
    MODE1_REG      CON $00
    MODE2_REG      CON $01
    PRE_SCALE_REG  CON $FE
    LED0_ON_L      CON $06
    
    
    channel VAR BYTE          ' <-- define channel variable
    
    
    
    
    ' ----- Initialization -----
    ' Put device to sleep to set prescale
    I2CWRITE SDA_PIN, SCL_PIN, PCA9685_CTRL_W, MODE1_REG, [$10]   ' SLEEP=1
    PAUSE 5
    
    
    ' Set prescale for ~1 kHz PWM (example value)
    I2CWRITE SDA_PIN, SCL_PIN, PCA9685_CTRL_W, PRE_SCALE_REG, [$06]
    
    
    ' Wake up, enable auto-increment
    I2CWRITE SDA_PIN, SCL_PIN, PCA9685_CTRL_W, MODE1_REG, [$20]   ' AI=1, SLEEP=0
    PAUSE 5
    
    
    ' Set outputs inverted + totem-pole
    I2CWRITE SDA_PIN, SCL_PIN, PCA9685_CTRL_W, MODE2_REG, [$14]
    
    
    ' Force all channels full-off (outputs HIGH, LEDs off)
    FOR channel = 0 TO 15
        I2CWRITE SDA_PIN, SCL_PIN, PCA9685_CTRL_W, (LED0_ON_L + channel*4), [$00,$00,$00,$10]
    NEXT channel
    PAUSE 10
    
    
    ' ----- Main loop -----
    MainLoop:
        FOR channel = 2 TO 5
            ' Turn channel fully ON (use "full on": ON_H bit4 = 1)
            I2CWRITE SDA_PIN, SCL_PIN, PCA9685_CTRL_W, (LED0_ON_L + channel*4), [$00,$10,$00,$00]
            PAUSE 500
    
    
            ' Turn channel OFF again (full off: OFF_H bit4 = 1)
            I2CWRITE SDA_PIN, SCL_PIN, PCA9685_CTRL_W, (LED0_ON_L + channel*4), [$00,$00,$00,$10]
            PAUSE 200
        NEXT channel
    GOTO MainLoop
    I decided to check the signals with scope, and they're heavily distorted. (image 1) I have 10K pull-up resistors on both SDA and SCL pin. So to test the issues, I've added this simple code:

    asda: 'small test for waveform shape
    high SDA_PIN
    pauseus 1
    low SDA_PIN
    pauseus 1
    goto asda

    When I run this code, the shape of the signal is perfect, as you can see from the 2nd image (and frequency is much higher, so definitely no capacitive loading issues). So what can be the reason I2CWRITE making so badly shaped signal?

    Name:  20251219195456.png
Views: 22
Size:  50.7 KB Name:  20251219195412.png
Views: 18
Size:  54.6 KB

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


    Did you find this post helpful? Yes | No

    Default Re: I2CWRITE/I2CREAD access hardware in the different way? (PCA9685 issues)

    what pic chip ?
    how is pc9685 wired up [sketch]
    i2c pins should not be set as outputs
    Warning I'm not a teacher

  3. #3
    Join Date
    Aug 2011
    Posts
    459


    Did you find this post helpful? Yes | No

    Default Re: I2CWRITE/I2CREAD access hardware in the different way? (PCA9685 issues)

    In your "working" example 2 you're actively driving the pins high and low.
    I2C is an open-drain/collector bus where the high level speed is determined in part by the pullup values.

    To reduce the rise time, lower the values to 4.7K or thereabouts

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


    Did you find this post helpful? Yes | No

    Default Re: I2CWRITE/I2CREAD access hardware in the different way? (PCA9685 issues)

    Chip is 18F45K80. I changed pull-up values to 5.1K, not much difference in shape of signals, but at least now it works.
    So in case of I2CWRITE, pins should be configured as input?

Similar Threads

  1. I2cwrite - I2cread
    By savnik in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 5th July 2006, 02:12
  2. I2CREAD and I2CWRITE problems
    By yourmomOS in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th April 2006, 10:58
  3. I2Cwrite and I2CRead speed!
    By Najim in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 2nd November 2005, 11:54
  4. I2Cwrite and I2CRead speed!
    By Najim in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 2nd November 2005, 11:50
  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 : 6

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