SoftwareWire.h library and port selection?

I see that everything in SoftwareWire.h is very close to Wire.h, but how do you choose the Port for SDA and SCL? I'm using an ArduinoMega and want to have two ports set up one using PG7 and PD2, and the other using PG1, and PG0.

#include <Arduino.h>

#include <SoftwareWire.h>

//Digital pin 38 SDA 1 pd7 TO
//Digital pin 39 SCL 1 pg2 ALE
//Digital pin 40 SDA 2 pg1 RD
//Digital pin 41 SCL 2 pg0 WR

SoftwareWire IR1 (PD7, PG2);
SoftwareWire IR2 (PG1, PG0);

I then went to just simply print out the values of PD7, etc and found it is just defines as a pin on the port and not port specific. I know I should have know that, but I didn't. So how do I define this to get it working correctly?

Thanks,

Marcello

The library uses the Arduino pin numbers.
They are in the library translated into a port and a bit for faster access, but that is just an implemenation, the library uses the pin numbers.

SoftwareWire IR1 (38, 39);
SoftwareWire IR2 (40, 41);

Have you been using AVR before and are now getting used to Arduino ? Arduino uses the pin numbers. Here are the pinmapping details for the Arduino Mega 2560:

The SoftwareWire has not been fully tested yet. Let me know if it works or if you encounter problems.

Koepel:
The library uses the Arduino pin numbers.
They are in the library translated into a port and a bit for faster access, but that is just an implemenation, the library uses the pin numbers.

SoftwareWire IR1 (38, 39);

SoftwareWire IR2 (40, 41);




Have you been using AVR before and are now getting used to Arduino ? Arduino uses the pin numbers. Here are the pinmapping details for the Arduino Mega 2560:
https://www.arduino.cc/en/Hacking/PinMapping2560

The SoftwareWire has not been fully tested yet. Let me know if it works or if you encounter problems.

Actually I was using PIC chips before this. I'm enjoying the sharing that goes on with the arduino community. I had to write just about everything myself with the PIC. I do miss writing directly in assembly though. I knew exactly what was happening. So that is where I'm coming from. I really wish the Arduino IDE would give a nice verbose output from the compiler. I miss seeing the details. It is so very easy to make mistakes of datatypes and such.

I was surprised when I found PD7 did not equate to 38 but 7 instead.

Is there a detailed explanation of all the abilities of the SoftwareWire.h library? Like I2c_repStart? I need to do a repeat start to read the data from my mlx90614 correctly. I see it in the header file.
I am going to try my best to get this working with your library. I figure once I get it working it will be invaluable in the future.

Thanks,

MAB

The AVR chips use the bit number and the port (you need both). There is no single number for a pin.
The code is open source, you can find everything online.
It is possible to let the compiler output assembly, but that would do you no good.

This is the SoftwareWire : GitHub - Testato/SoftwareWire: Creates a software I2C/TWI bus on every pins
It uses things like "digitalPinToPort()". Those are (internal) Arduino functions to translate a pin to a bit number and the port. They are different for every type of AVR chip.
But you should not look so deep.

If you want to take a look at the inside of Arduino code, open one of these files : https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr/cores/arduino
Those are the base of code that Arduino will generate.

Have you seen the Library Manager in the Arduino IDE ?
You will find code there for the MLX90614.

Don't try to find out how SoftwareWire works, because that is not documented.
After declaring the SoftwareWire object, it should be a drop-in replacement for the normal Wire library.
Therefor the documentation is the documentation of the normal Wire library.

You better make the MLX90614 work with the normal I2C bus and then change that into the SoftwareWire.

Repeated start is here : Wire - Arduino Reference
and here : Wire - Arduino Reference

The SoftwareWire is not fully tested yet. If you want for example multiple MLX90614 to work, use one of these : TCA9548A I2C Multiplexer : ID 2717 : $6.95 : Adafruit Industries, Unique & fun DIY electronics and kits

Alright, I was in the middle of setting up an arduino to use for changing addresses on mlx90614s before looking at your library. Then I can use one Software I2C to drive both sensors since they'll have different addresses. I'll do that for now since it is fast for the moment. Haven't seen any code to get the MLX90614 to work with the standard wire library. But I would much rather not change addresses, but just add an extra software i2c port. Once I get two devices up and running and my project up and running I'll come back to trying to get the MLX90614 working with your library.

Thanks ,

Mab

In the menu of the Arduino IDE is the Library Manager. Open the Library Manager and search for MLX90614. When you have installed a library, one or more examples have been added to the menu as well. Try such an example.

If you want other code, then there are many more Arduino libraries for the MLX90614 at Github.com.

Tutorial : Overview | Using Melexis MLX90614 Non-Contact Sensors | Adafruit Learning System

I think it would be more reliable if you just use the Wire library and change the address of one of the MLX90614. See this : Multiple Melexis MLX90614 on the same i2c, how to change addresses ?? - Sensors - Arduino Forum

I thought It'd be good place here to post this document. Even though better late than never. The document is better than I can do explaining the differences.. Then you can fix the software for all to use :slight_smile:

"I do miss writing directly in assembly though."

You are in a unique position to use your knowledge of assembly language to give yourself insights into the inner workings of the compilers... I too love to know the machine as only assembly programmers do. A deep understanding, of what's "under the hood" so to speak.

Take your talents and write some code (in C), generate the listing of the C and assembly output together. You might be surprised, like me, that it's more than good at it. The best part of your talents here is that you can do the stuff in a higher language, and if it doesn't appear to be working right, you can read the generated code from it. More often than not, you also learn from this. Many years ago I found a bug in a compiler that way...

I also had problems with the MLX device. Of course it's really not an i2c bus, but a SMBus, similar definitely not the same. I've appended an explanation of it that's interesting. One of the problems with these devices is that they have so MANY variations and variables in just the configuration, then add (like the p) 28 pins, kind of limiting considering what's inside. Limit of three timers, so who gets them?...

Take care :slight_smile:

smbus-sloa132.pdf (106 KB)