Page 3 of 5
Re: 2 Arduino I2C communication
Posted: Sun Jun 02, 2024 8:58 pm
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
Re: 2 Arduino I2C communication
Posted: Sun Jun 02, 2024 9:06 pm
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!
Re: 2 Arduino I2C communication
Posted: Sun Jun 02, 2024 9:08 pm
by mnfisher

- LabNation_Screenshot28.png (259.42 KiB) Viewed 7642 times
Re: 2 Arduino I2C communication
Posted: Mon Jun 03, 2024 5:38 pm
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
Re: 2 Arduino I2C communication
Posted: Mon Jun 03, 2024 8:50 pm
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 386 times
- mst.fcfx
- (13.66 KiB) Downloaded 472 times
Martin
Re: 2 Arduino I2C communication
Posted: Mon Jun 03, 2024 8:58 pm
by mnfisher

- LabNation_Screenshot30.png (258.17 KiB) Viewed 7613 times
Sending a string 'abcd\r\n\0' and requesting and receiving 5 bytes
Re: 2 Arduino I2C communication
Posted: Tue Jun 04, 2024 9:46 pm
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
Re: 2 Arduino I2C communication
Posted: Tue Jun 04, 2024 10:43 pm
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
Re: 2 Arduino I2C communication
Posted: Wed Jun 12, 2024 4:57 pm
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
Re: 2 Arduino I2C communication
Posted: Wed Jun 12, 2024 5:59 pm
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