2 Arduino I2C communication

Post and discuss new components that you have created.
max.tisc
Posts: 113
http://meble-kuchenne.info.pl
Joined: Thu Dec 10, 2020 5:40 pm
Been thanked: 10 times

Re: 2 Arduino I2C communication

Post by max.tisc »

yours seems like a very hard job, and I'm already lost, it seems strange to me that the I2C module was not designed for data exchange between 2 micros.
I thank you for your work

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: 2 Arduino I2C communication

Post by mnfisher »

Did you try the interrupt version? - I've had it running all day (I was out) - and it has not missed a beat. Also - being interrupt driven means that the slave doesn't have to busy wait on the i2c status.

The master controls the data transfer (send and receive!) - and inter-mcu communication is good!

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: 2 Arduino I2C communication

Post by mnfisher »

LabNation_Screenshot28.png
LabNation_Screenshot28.png (259.42 KiB) Viewed 6818 times

max.tisc
Posts: 113
Joined: Thu Dec 10, 2020 5:40 pm
Been thanked: 10 times

Re: 2 Arduino I2C communication

Post by max.tisc »

I'm sorry but as I told you I got lost, what should I upload? on i2c_rcv3.fcfx I only see the circular buffer
have you set the LED to see if the code works?
Thank you

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: 2 Arduino I2C communication

Post by mnfisher »

Okay - I made a slightly modified example

Here the 'master' program (mst.fcfx running on the Mega) transmits a data string to the 'slave' (slv.fcfx running on a Nano). The string - 'abcd\r\n' rotates on each send (next is 'bcda\r\n' then 'cdab\r\n' etc) to add bit of interest. The slave receives the string and outputs it to UART. (I include the NL and CR characters in the sent data :; )
The 'master' also requests a random amount of data (between 1 and 6 bytes) and the slave just sends data from the array reply (in this case just 1,2,3, ...)

I haven't added an output to UART on the master - but can watch the data on the scope.

It's a slightly contrived example - with the slave waiting on a '\0' in the data received to mark an end of string - I'm guessing in most cases you'd want to send a fixed size block of data. However - I wanted to demonstrate that the 'master' side controls the transaction - the number of bytes sent and requested'

Another option is to set a flag in Rcv_data for a end of data marker or number of bytes received etc - the loop in main just waits on the end marker (using the circular_buffer's wait for ...) = but it could be doing something else instead and do calculations / sleep etc until the 'master' requires it..

Do not be tempted to add too much to the rcv_data and send_data macros - these are acting as part of the interrupt handler and need to be kept short (and more importantly fast)
slv.fcfx
(17.09 KiB) Downloaded 369 times
mst.fcfx
(13.66 KiB) Downloaded 450 times
Martin

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: 2 Arduino I2C communication

Post by mnfisher »

LabNation_Screenshot30.png
LabNation_Screenshot30.png (258.17 KiB) Viewed 6789 times

Sending a string 'abcd\r\n\0' and requesting and receiving 5 bytes

max.tisc
Posts: 113
Joined: Thu Dec 10, 2020 5:40 pm
Been thanked: 10 times

Flowcode v9 Re: 2 Arduino I2C communication

Post by max.tisc »

I only partially tested the new codes because I'm out of the office and I don't have all the necessary equipment, when I get back I'll test more thoroughly, but for what I've tried it seems very good
thanks

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: 2 Arduino I2C communication

Post by mnfisher »

Let us know how it goes.

Do you have a specific user case in mind - transmit / receive strings, numbers / arrays of numbers etc?

Martin

max.tisc
Posts: 113
Joined: Thu Dec 10, 2020 5:40 pm
Been thanked: 10 times

Flowcode v9 Re: 2 Arduino I2C communication

Post by max.tisc »

Hi Martin, I was playing with the master software and I added the UART output but I don't read what I expected 1,2,3,4, I definitely did something wrong, but I wanted to ask you on the oscilloscope I see the transmission of writing A, B,C,D and then the reading request, the question is: is what I see on the screen just the request or is it also the slave's response? to answer your question the slave will be connected via SPI to a TOF sensor for reading the water flow (and this works) and collects the data, will carry out calculations to obtain the flow speed, and will send them 1 time per second per master who will take care of displaying them on a screen, then will have to control the 4..20ma, rs485, impulsive outputs, furthermore from the keyboard connected to the master the programming data will be sent to the slave which will send them to the sensor, at this point perhaps it was Is it better to do everything in SPI starting from the master that controls both the slave and the sensor?

thank you
Attachments
mst_mod.fcfx
(14.88 KiB) Downloaded 343 times
Immagine 2024-06-12 174855.png
Immagine 2024-06-12 174855.png (160.38 KiB) Viewed 6540 times

mnfisher
Valued Contributor
Posts: 1453
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 135 times
Been thanked: 707 times

Re: 2 Arduino I2C communication

Post by mnfisher »

What you see on screen are the slaves reply - the master just requests the bytes (Read request (bit 0 of address is set) - sending 'ACK' after each is received - until it sets NACK on the final byte)

The reason they aren't displayed (on UART) is because the slave is returning 1, 2, 3, 4 (NOT '1', '2', '3' and '4') - so you can either modify the 'reply' array in the slave code or add '0' to each value in the master to convert the values to ASCII. Alternatively use a loop and SendNumber().

In pseudocode:

Code: Select all

for .i = 0..3    (Loop count 4 using .i as counter)
  .reply[.i] = .reply[.i] + '0'   // Convert a digit to it's ASCII equivalent
end  for 
SendByteArray(.reply, 4)     // This should now output 1234 

// OR

for .i= 0..3   (Loop count 4)
  SendNumber(.reply[.i])
  if(.i < 3)  SendChar(',')
end for
// Which will output 1,2,3,4

// OR in the slave code
reply = {'1', '2', '3', '4' ....}    // (not {1,2,3,4... }
Note that the demo was returning a set 'reply' - I envisaged it returning (say) the value of a sensor reading?

Martin

Post Reply