I2C Communication between PIC18F4550 and AS5600


+ Reply to Thread
Results 1 to 15 of 15
  1. #1
    Join Date
    Mar 2024
    Posts
    5

    Default I2C Communication between PIC18F4550 and AS5600

    Hello,
    I am trying to communicate between PIC and AS5600 chip. I am not sure if the commands I send are correct. The only data I receive back from AS5600 is "6" for B0 and "6" for B1.

    Here is part of my code:

    define LOADER_USED 1
    define OSC 24
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 20h
    DEFINE HSER_BAUD 38400
    DEFINE HSER_CLROERR 1
    Define LCD_DREG PORTD
    Define LCD_DBIT 4
    Define LCD_RSREG PORTD
    Define LCD_RSBIT 1
    Define LCD_EREG PORTD
    Define LCD_EBIT 0
    define LCD_BITS 4
    define LCD_LINES 2
    define CCP2_REG PORTC
    define CCP2_BIT 1
    ADCON1 = 15 'make all inputs and outputs digital


    TRISA = %11111111 'set up port A as input
    TRISC = %10000000 'set up port C as output/input (Rx-1,Tx-0)
    TRISB = %11111111 'set up port B as input
    TRISD = %00000000 'set up port D as output
    TRISE = %00000000 'set up port E as output


    PORTC = %01000100
    DEFINE I2C_SCL PORTB,1
    DEFINE I2C_SDA PORTB,0
    DEFINE I2C_SLOW 1
    DEFINE I2C_HOLD 1
    DEFINE CHAR_PACING 102

    start:
    B0 = 0
    B1 = 0
    pause 100
    i2cwrite PORTB.0,PORTB.1,$36,$0E,[0]
    pause 10
    i2cwrite PORTB.0,PORTB.1,$36,$0E,[1]
    pause 10
    I2CREAD PORTB.0,PORTB.1,$36,$0E,[B0]
    pause 10
    I2Cwrite PORTB.0,PORTB.1,$36,$0F,[0]
    pause 10
    I2Cwrite PORTB.0,PORTB.1,$36,$0F,[1]
    pause 10
    I2CREAD PORTB.0,PORTB.1,$36,$0F,[B1]
    pause 10
    LCDOut $FE, 1, dec B1
    LCDOut $FE, $C0, dec B0
    goto start

    Any help you can provide in this matter will be appreciated.

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,935


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    Well, from the datasheet I could not understand much about how to read data from the chip.

    What are the $0F and $0E after $36 in your commands?

    Also, are you sure you have connected correctly data and clock of the I2C bus from PIC to AS5600?

    Ioannis

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


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    the data sheet address is a 7 bit address pbp uses 8 bit address
    what are ic2writes for ?
    they seem to be of no value that i can see
    you defined sda and scl why not use them ?

    more like

    start:
    I2CREAD SDA,SCL,$6C,$0E,[B0,B1]
    LCDOut $FE, 1, dec B1
    LCDOut $FE, $C0, dec B0
    goto start
    Warning I'm not a teacher

  4. #4
    Join Date
    Mar 2024
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    $36 is I2C address for AS5600
    $0F and $0E address for low and high bytes from encoder.
    I am pretty sure the data and clock are connected correctly. Also I pull them hight with 4.7k resistor.

  5. #5
    Join Date
    Mar 2024
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    what are ic2writes for ?
    I thought that I need to send trigger first to AS5600 (Master mode), but I might be wrong.

    Can I ask you why did you use
    $6C in read command? Shouldn't it be $36?


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


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    $36 is I2C address for AS5600
    the data sheet address $36 is a 7 bit address pbp uses an 8 bit address scheme


    I thought that I need to send trigger first to AS5600 (Master mode), but I might be wrong.
    You may well have to "configure" the device but for an i2c read transaction that is an unnecessary step

    $0F and $0E address for low and high bytes from encoder.
    both registers can be read in a single transaction as i indicated
    Warning I'm not a teacher

  7. #7
    Join Date
    Mar 2024
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    Quote Originally Posted by richard View Post
    the data sheet address $36 is a 7 bit address pbp uses an 8 bit address scheme

    Does it mean that I can not communicate or I need to adjust the address?


    You may well have to "configure" the device but for an i2c read transaction that is an unnecessary step

    ok


    both registers can be read in a single transaction as i indicated
    ok


    Thank you.

  8. #8
    Join Date
    Mar 2024
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    I have used this one:
    I2CREAD SDA,SCL,$6C,$0E,[B0,B1]
    and it works. Thank you very much for your help

  9. #9
    Join Date
    Jan 2010
    Posts
    20


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    Glad to hear you got this working. I'll throw out a quick start I've been trying lately when interfacing to new chips for a basic frame work that gets me down the road quickly. I've been trying out OpenAi's Free chat bot.
    This is what I entered in for your pic and the AS5600.

    ChatBot Query: Write a PicBasic Pro program to read/write data from AS5600L-ASOM with the PIC18F4550 .

    Output: To interface the AS5600L-ASOM magnetic rotary position sensor with the PIC18F4550 microcontroller using PICBASIC PRO, you need to set up I2C communication. The AS5600L-ASOM uses the I2C protocol to communicate with the microcontroller.

    Here’s a sample program to read the angle data from the AS5600L-ASOM and write it to an output.

    PICBASIC PRO Code
    Code:
    ' Define I2C pins
    DEFINE I2C_SDA PORTC.4
    DEFINE I2C_SCL PORTC.3
    
    ' AS5600L-ASOM I2C address (default 0x36 shifted for 7-bit addressing)
    AS5600_ADDR CON $36
    
    ' AS5600L-ASOM registers
    AS5600_ANGLE_H CON $0E
    AS5600_ANGLE_L CON $0F
    
    ' Variables
    angleH VAR BYTE
    angleL VAR BYTE
    angle VAR WORD
    
    ' Initialize I2C
    I2CINIT
    
    ' Main Program
    main:
        ' Read the high byte of the angle register
        I2CREAD AS5600_ADDR, AS5600_ANGLE_H, [angleH]
        PAUSE 10
    
        ' Read the low byte of the angle register
        I2CREAD AS5600_ADDR, AS5600_ANGLE_L, [angleL]
        PAUSE 10
    
        ' Combine high and low bytes to get the full angle value
        angle = angleH << 8
        angle = angle | angleL
    
        ' Convert angle to degrees (angle is 0-4095 representing 0-360 degrees)
        angle = (angle * 360) / 4096
    
        ' Add your code here to use the angle value (e.g., display on an LCD, send via serial, etc.)
        ' For example, send angle via UART
        SEROUT2 PORTC.6, 84, ["Angle: ", DEC angle, 13, 10]
    
        PAUSE 1000  ' Delay 1 second before next read
    
        GOTO main
    
    END
    Explanation
    Define I2C Pins: Set the SDA and SCL pins for I2C communication.
    AS5600L-ASOM I2C Address: The I2C address for the AS5600L-ASOM is 0x36. Note that some libraries expect the 7-bit address shifted for 8-bit communication.
    AS5600L-ASOM Registers: Define the registers for reading the angle.
    Variables: Declare variables to store the high and low bytes of the angle, and the combined angle value.
    Initialize I2C: Use I2CINIT to initialize the I2C communication.

    Main Program Loop:
    Read the high byte and low byte of the angle register.
    Combine the high and low bytes to get the full 12-bit angle value.
    Convert the angle value to degrees.
    Use the angle value as needed (e.g., send via UART for display).

    Notes
    Ensure pull-up resistors (typically 4.7k ohms) are connected to the SDA and SCL lines.
    Adjust the SEROUT2 command for your specific UART configuration if needed.
    The angle value from the AS5600L-ASOM is 12 bits, representing a range of 0-4095, which maps to 0-360 degrees.
    This code provides a basic framework for interfacing the AS5600L-ASOM with a PIC18F4550 microcontroller using PICBASIC PRO. You can expand and modify it based on your specific requirements.

    It's not perfect but can get some project started quickly as a reference.

  10. #10
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,935


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    Well, many times I tried to make ChatGPT understand that variable definition is special in PBP and different from other dialects of Basic.

    And it finally got it!!! Amazing... this thing is indeed learning!

    Of course needs more details on the prompt to make a useful program. Or at the end instead of detailed prompt, just write the program yourself

    Ioannis

  11. #11
    Join Date
    May 2013
    Location
    australia
    Posts
    2,451


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    And it finally got it!!! Amazing... this thing is indeed learning!
    i would not say that, the code given will never work and has at least three fundamental flaws
    number one the i2c address is incorrect
    Warning I'm not a teacher

  12. #12
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,557


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    Number two: With angle being 16bits and the raw value returned by the sensor ranging between 0 and 4095 this will not work great:
    Code:
    ' Convert angle to degrees (angle is 0-4095 representing 0-360 degrees)
        angle = (angle * 360) / 4096

  13. #13
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,935


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    Both of you are absolutely right, but I was referring to the Variables definition, that were always defined wrong. Now it got it right.

    I am convinced that it is getting better every time and maybe after a few months, it will correct those mistakes too.

    And in general does not define the various FSR's unless you strictly ask it to do so.

    Ioannis

  14. #14
    Join Date
    May 2013
    Location
    australia
    Posts
    2,451


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    I am convinced that it is getting better every time
    not at all , when you see fundamental errors like this

    Code:
        ' Read the high byte of the angle register
        I2CREAD AS5600_ADDR, AS5600_ANGLE_H, [angleH]
        PAUSE 10
    
    
        ' Read the low byte of the angle register
        I2CREAD AS5600_ADDR, AS5600_ANGLE_L, [angleL]
        PAUSE 10
    if the read angle moves by 1 bit count in-between the reads then the read position error could up to +- 45 degrees [256 counts]

    pbp i2c is glacially slow a 10ms pause in between reads wtf!, its just nonsense
    Warning I'm not a teacher

  15. #15
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,935


    Did you find this post helpful? Yes | No

    Default Re: I2C Communication between PIC18F4550 and AS5600

    Definetly will not take the job of software engineers in the near future. But it is getting better. And I suspect the free version is not that good for obvious reasons.

    But paid AI does incredible job in other fields, like voice, image and video processing, that by definition is more complex than a I2C transaction. That is why I believe it can do much better that this PAUSE 10...!!!

    Don't forget also that we are all beta testers and at the same time teachers of this beast...

    Ioannis

Similar Threads

  1. Replies: 18
    Last Post: - 4th July 2017, 14:26
  2. PIC18F I2C Communication with Arduino
    By picmilan in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 14th July 2016, 21:38
  3. Replies: 0
    Last Post: - 26th May 2012, 13:57
  4. EEprom and MCP9801 I2C communication problems
    By aajgss in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 3rd September 2011, 21:45
  5. Replies: 2
    Last Post: - 10th June 2005, 02:34

Members who have read this thread : 15

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