PIC18F I2C Communication with Arduino


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    May 2016
    Posts
    33

    Default PIC18F I2C Communication with Arduino

    Hello all,

    I read all the I2C related posts but still can't fully understand how to send dada from PIC18F to an Arduino.

    I set the address of arduino to 0x26 and try to do following in PicBasic but I don't get any data:


    Code:
    TRISC.0 = 0
    TRISB = 000000
    
    LED VAR PORTC.0   ' Assign name "LED" to PORTC.0
    SCL VAR PORTB.3    ' Clock pin
    SDA VAR PORTB.1    ' Data pin
    
    ADDR VAR WORD
    
    ADDR = $26 ' I2C address
    
    mainloop:
       HIGH LED        ' Turn on LED connected to PORTB.0
       Pause 1000       ' Delay for 1 seconds
       
       I2CWrite SDA,SCL,ADDR,[0]
       
       LOW LED          ' Turn off LED connected to PORTB.0
       Pause 1000       ' Delay for 1 seconds
       Goto mainloop   ' Go back to loop and blink LED forever

    Any idea?

  2. #2
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    Im no expert, but don't you have to send data. All the examples I've seen, and use send and receive a variable after the address, eg to send the value of a word variable

    Code:
    I2CWRITE SDApin,SCLpin,i2cWriteAddress,i2cControl,[variable.lowbyte,variable.highbyte]
    I can't see that in your code

    Also I think you need to set one device as a slave and another as master, so maybe posting your arduino code could also help those more qualified to comment to asses whats going on

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    The data being sent is the value 0, that looks alright to me though I've literally never used I2CWrite.
    Check the pins you're using for multiplexed functions, specifically analog such as ADC and comparators and make sure the pins are set to digital.

    Then I'd check the pins with a scope or logic analyzer to verify if data is actually coming out or not - if you don't KNOW, for sure, that the Arduino code is working.

    /Henrik.

  4. #4
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    I've literally never used I2CWrite.
    Really Henrick? Wow, I use it all the time to write to serial eeprom's, a/d's and i/o expanders, etc. You should really design more hardware..
    Dave Purola,
    N8NTA
    EN82fn

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    Can you inform us which 18F pic you are using?

    Most of the 18F PICs (28 pin and 40 pin) have the hardware I2C pins on PORT C not PORTB. Whilst I'm sure PBP can define any pins, it might be "better" to use the actual pins associated with the hardware module on the PIC, eg

    Code:
    SCLpin             var PORTC.3          'clk
    SDApin             var PORTC.4          'data

  6. #6
    Join Date
    May 2016
    Posts
    33


    Did you find this post helpful? Yes | No

    Default Re: PIC18F I2C Communication with Arduino

    I am using PIC18F2450 which doesn't have hardware I2C pins.Isn't I2CWRITE just a software solution and not using actual hardware?

    here is arduino code:

    Code:
    #include <Wire.h>
    
    void setup() {
      Wire.begin(0x26);                // join i2c bus with address
      Wire.onReceive(receiveEvent); 
      Serial.begin(9600);          
      
    }
    
    void loop() {
     delay(1000);
    
    }
    
    
    void receiveEvent(int howMany) {
    
      while (1 < Wire.available()) { 
        char c = Wire.read(); 
        Serial.print(c);    
      }
      int x = Wire.read();   
      Serial.println(x);
    }

Similar Threads

  1. Interfacing with Arduino I2C LCD
    By norohs in forum Documentation
    Replies: 47
    Last Post: - 30th May 2017, 18:53
  2. I2C lcd ( arduino ) with PICBASIC, help
    By iw2fvo in forum mel PIC BASIC Pro
    Replies: 92
    Last Post: - 10th September 2014, 18:00
  3. EEprom and MCP9801 I2C communication problems
    By aajgss in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 3rd September 2011, 21:45
  4. How do I operate an Arduino I2C LCD
    By menta in forum General
    Replies: 8
    Last Post: - 13th July 2011, 02:28
  5. Replies: 2
    Last Post: - 10th June 2005, 02:34

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