PDA

View Full Version : PIC18F I2C Communication with Arduino



picmilan
- 7th July 2016, 05:16
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:



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?

Scampy
- 7th July 2016, 08:20
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



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

HenrikOlsson
- 7th July 2016, 10:30
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.

Dave
- 7th July 2016, 13:24
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..

Scampy
- 7th July 2016, 17:53
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



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

picmilan
- 7th July 2016, 19:46
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:


#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);
}

picmilan
- 7th July 2016, 22:36
I am using 2 2.2K pull up resistors on pins.

Correct arduino 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(Wire.available() > 0){
int x = Wire.read();
Serial.println(x);
}
}

HenrikOlsson
- 8th July 2016, 06:17
Dave,
I do design hardware from time to time but yeah, I'd love to do more. I tend to use SPI as I think it's both easier and faster and all the functions you mention are available in chips with SPI-interface as well as I2C so I've never actually had the need TO use it - that's it really.

Scampy,
I2C/Read/Write are not "hardware assisted" commands, they do not use the (M)SSP module in the PIC so any pins can be used.

picmilan,
I'll repeat what I said earlier: Check if the pins you're using has analog functions and make sure they're turned off.

/Henrik.

picmilan
- 8th July 2016, 08:02
picmilan,
I'll repeat what I said earlier: Check if the pins you're using has analog functions and make sure they're turned off.

/Henrik.

Yeah,You are right! Looking at datasheet those pins are analog by default..

Now Control Address and write address is what confuses me.Control Address is 0x26 as defined in arduino code,right? so what is write address?

Is SPI easier? I don't have many pins available but if I2C doesn't work I guess I don't have any other option.

Scampy
- 8th July 2016, 08:17
Henrik, I thought as much, which is why I added the comment about PBP using any pin. I was simply suggesting he tried other pins, and use those designated for such communications if the pic has them. Hopefully if the OP disables any analogue config his issue will be resolved

richard
- 8th July 2016, 08:23
try it like this
Arduino i2c address' are 7bit
pbp uses 8bit


ADDR = $26 ' 7bit Arduino I2C address
addr=addr<<1 ;shift to 8 bitformat



I2CWrite SDA,SCL,ADDR,[0]

picmilan
- 14th July 2016, 21:38
try it like this
Arduino i2c address' are 7bit
pbp uses 8bit


ADDR = $26 ' 7bit Arduino I2C address
addr=addr<<1 ;shift to 8 bitformat



I2CWrite SDA,SCL,ADDR,[0]

It worked! Thanks alot!