Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:52 03 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : I2C Help

Author Message
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1097
Posted: 11:56pm 25 Jun 2020
Copy link to clipboard 
Print this post


HI

Just experimenting with talking between 2 uMites using I2C according to the instructions & codes in the 5.04 manual.

The two units do talk to each other but the Slave won't receive the full message from the Master.

I get the following messages:
 [9] I2C SLAVE READ 3, MSG(), RECVD
  ERROR: Cannot find RECVD


I take this to mean that the Slave is not receiving the instructions.
I did a fair bit of stuffing around but not getting anywhere.

I'm just trying to turn on a LED on PIN 16 on the Slave.

Any help would be appreciated.


SSETPIN 16, 8  'pin 16 & Digital Out
SPIN 16, 1
PAUSE 300
SPIN 16, 0

SUB SSETPIN 'pinnbr, cfg
 I2C OPEN 100, 1000,pu
 I2C WRITE &H26, 0, 3, 1, pinnbr, cfg
 IF MM.I2C THEN ERROR "Slave did not respond"
 I2C CLOSE
END SUB

' set the output of an I/O pin on the slave
SUB SPIN pinnbr, dat
 I2C OPEN 100, 1000,pu
 I2C WRITE &H26, 0, 3, 2, pinnbr, dat
 IF MM.I2C THEN ERROR "Slave did not respond"
 I2C CLOSE
END SUB

SLAVE

 OPTION AUTORUN ON
 DIM msg(2) ' array used to hold the message
 I2C SLAVE OPEN &H26, 0, 0, WriteD, ReadD ' slave's address is 26 (hex)
 DO ' the program loops forever
   WATCHDOG 1000 ' this will recover from errors
 LOOP
 
 SUB ReadD ' received a message
   print "Received Message"
   I2C SLAVE READ 3, msg(), recvd ' get the message into the array
   IF msg(0) = 1 THEN ' command = 1
     SETPIN msg(1), msg(2) ' configure the I/O pin
   ELSEIF msg(0) = 2 THEN ' command = 2
     PIN(msg(1)) = msg(2) ' set the I/O pin's output
   ELSE ' the command must be 3
     s$ = str$(pin(msg(1))) + Space$(12) ' get the input on the I/O pin
 END SUB ' return from the interrupt
 
 SUB WriteD ' request from the master
   I2C SLAVE WRITE 12, s$ ' send the last measurement
 END SUB ' return from the interrupt



BTW, just found out recently that you can paste more than one line at a time into MMChat using the Paste Icon.
Very handy more running multiple lines of straight code

Brian


ChopperP
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 12:18am 26 Jun 2020
Copy link to clipboard 
Print this post

recvd is a variable that is used to hold the number of bytes actually received.
Try declaring it as a local at the start of the SUB.

Jim
VK7JH
MMedit
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1097
Posted: 12:32am 26 Jun 2020
Copy link to clipboard 
Print this post

Thanks Jim.

Much better. LED flashes.
Had to be something simple

Brian
ChopperP
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1097
Posted: 12:32am 26 Jun 2020
Copy link to clipboard 
Print this post

double post.
Edited 2020-06-26 10:33 by Chopperp
ChopperP
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2442
Posted: 01:25am 26 Jun 2020
Copy link to clipboard 
Print this post

shouldn't RECVD be created automatically, unless OPTION EXPLICIT has been declared earlier on in the code?

this looks like a bug in MMbasic to me, but i could be wrong.


cheers,
rob   :-)
 
Chopperp

Guru

Joined: 03/01/2018
Location: Australia
Posts: 1097
Posted: 03:01am 26 Jun 2020
Copy link to clipboard 
Print this post

I wouldn't have thought that the variable needed declaring in this case.
I assumed that the demo programs were tried & tested.
Of course, I would have declared it in a real program & used OPTION EXPLICIT  

BTW I forgot to remove the comment in the following line before posting. Was playing.

SUB SSETPIN 'pinnbr, cfg

Brian
ChopperP
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025