writing and reading a binary word on some pins

If doesn't fit into any other category ....
BeanieBots
Posts: 325
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 173 times
Been thanked: 104 times

Re: writing and reading a binary word on some pins

Post by BeanieBots »

I2C was actually specifically designed for communication between (on board) peripheral chips and would be the perfect choice.
However, serial (aka RS232) is much easier to do hardware wise and talk you through on a forum.
Everything is easy when you know how. The fact that you are asking about this makes me try and push you towards serial.
For serial, it is just connect Tx (transmit) to Rx (receive) and then send the data.
It needs to be configured initially. SERIAL.MODE baudrate [, bits, parity, stop]
I would suggest a fairly low baud rate and 'standard' for the rest. SERIAL.MODE 9600 , 8, 0, 1
Parity and stop are set as default if not defined, so you could simply use SERIAL.MODE 9600
To send, use the print command. PRINT expression.
Expression can be text (enclosed in quotes) or a variable.
You can either use ONSERIAL "label" to capture incomming data or poll the buffer to see if anything has arrived. (I suggest ONSERIAL)
Have a look in the help and come back if you have any questions.
User avatar
cicciocb
Site Admin
Posts: 1899
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1269 times
Contact:

Re: writing and reading a binary word on some pins

Post by cicciocb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Sun Aug 07, 2022 10:49 pm hello to all
I search in the help but could not find the way to do this.
I would like to be able to encode and decode 8 infos from an annex esp to another one, using only 3 wires max.

i could use an I2C module but I don't thing it will work fine after a 2m cables.

my idea is to use 3 ouputs as a binary word and decode this binary word at the other side.
I don't find a function directly implemented in Annex to declare something like :
"set pin 5,6,7 as binary binout"
then say "write value 0 to binout" and automatically the outputs goes "0 0 0"
or "write value 3 to binout" and automatically the outputs goes "0 1 1"

same as input.

is there a function directly implemented to do this? if not I will write all the code to do it.

thank you for your help.
Hi Its1000,
going back at your initial request, there is no specific function to write several pins at the same time.
However this can be easily done as below:

Code: [Local Link Removed for Guests]

'example of writing / reading a word
code_out = 5  ' number from 0 to 7
pin1 = 15
pin2 = 32
pin4 = 33
pin.mode pin1, output
pin.mode pin2, output
pin.mode pin4, output

write_word code_out 'write the code into the pins

code_in = 0
read_word code_in 'read the code from the pins
wlog code_in
end

sub write_word(w)
pin(pin4) = w and 4
pin(pin2) = w and 2
pin(pin1) = w and 1
end sub

sub read_word(w)
w = pin(pin4)*4 + pin(pin2) * 2 + pin(pin1)
end sub
If the requirement is to switch "simultaneously" ( i.e. without any intermediate state ), this cannot be done as there will always be a little delay (few microseconds) between the change of state of each pin. In this case the best is to use the serial port or I2C as already suggested.
its1000
Posts: 76
Joined: Thu May 20, 2021 6:57 pm
Has thanked: 3 times
Been thanked: 1 time

Re: writing and reading a binary word on some pins

Post by its1000 »

thank you very much Ciccio it's clear. I don't need to write simultaenously. because the word will be written some seconds before other esp "read" i
so your system would work perfect.

I will dig this option, and the one with serial or i2C.
if I use serial i will try to send more informations if i can. like a variable, and a value.
the best would be to have the option from the slave esp to read variable from the master esp when it needs.

example :
in master ESP I have 2 variable with value between 0 and 100
var1 = 80
var2 = 40

the slave esp need to get those variable.
with wifi activated it would be very easy to get it, but I need to get it by cable.
User avatar
Electroguard
Posts: 836
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 268 times
Been thanked: 317 times

Re: writing and reading a binary word on some pins

Post by Electroguard »

No problem - you can send serial text which includes whatever variable names and values you wish.
Eg the master could "PRINT VAR2=60" or "PRINT VAR1=80" and the slave would just branchOnSerial to the assigned subroutine which would send the complete name=value pair to be evaluated immediately using COMMAND stringpair$ without even needing to decipher it first.
If the slave used a variable called "Heater" you could send it "Heater=80" and evaluate that immediately without parsing or comparison.
(it would be the same as typing in variableName=variableValue (eg: VAR2=60) into the Editors immediate window, it would be evaluated immediately)
BeanieBots
Posts: 325
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 173 times
Been thanked: 104 times

Re: writing and reading a binary word on some pins

Post by BeanieBots »

Oh, thanks Electroguard. Can't believe I never knew there was COMMAND command :oops:
its1000
Posts: 76
Joined: Thu May 20, 2021 6:57 pm
Has thanked: 3 times
Been thanked: 1 time

Re: writing and reading a binary word on some pins

Post by its1000 »

thank you Electroguard.
is there a possibility for the slave to read the value from the master? I will have tx and rx on both sides.
BeanieBots
Posts: 325
Joined: Tue Jun 21, 2022 2:17 pm
Location: South coast UK
Has thanked: 173 times
Been thanked: 104 times

Re: writing and reading a binary word on some pins

Post by BeanieBots »

Serial itself can't actually "read" as such but you could have the slave send a request to the master for it to send the data to the slave.
its1000
Posts: 76
Joined: Thu May 20, 2021 6:57 pm
Has thanked: 3 times
Been thanked: 1 time

Re: writing and reading a binary word on some pins

Post by its1000 »

so to take an example with the master and one slave on serial 1 :

I will connect ESP-master TX to ESP-slave RX
and ESP-master RX to ESP-slave TX
i will declare the serial on both sides.

I have the variable VAR1 being stored in ESP-master with a value 0 to 255 for example.
I need to send a command from ESP-Slave to master asking it to execute a routine
then master execute the routine to send the var value?
then slave receive it and store the value in it's own variable?
I am right?

if it's not too much abuse i would really appreciate if you can help me with the commands I need to use i am a little bit lost.

thank you very much
bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 44 times
Been thanked: 50 times

Re: writing and reading a binary word on some pins

Post by bugs »

I did a simpler hookup with just the master TX connected to slave RX.
The master sends the value of var1 when it feels like it and the slave receives it.
The master code:-

Code: [Local Link Removed for Guests]


CODE: master.bas
--------------------------------------------------------------------------------

'Master

var1=0
serial.mode 9600
timer0 15000,sendvar1
wait

sendvar1:
var1 = var1 + 0.25
print "Var1=" & str$(var1)
return

And the slave receiver - with room for extra variables by using the case statement - but just a crude pause to allow for all characters to arrive:-

Code: [Local Link Removed for Guests]


CODE: slave.bas

--------------------------------------------------------------------------------

' Slave

true=1
false=0
serial.mode 9600
onserial getinput
wait

getinput:
  pause 100    'wait for all chars
  a$ = serial.input$  
  b$ = left$(a$,5)
  select case b$
    case "Var1="
      x=val(mid$(a$,6) ) 
      rxok=true     
    case else
      rxok=false
  end select
  if rxok then
     wlog "Received "; b$; str$(x)
  else
      wlog "Bad input: "; a$
  endif
return

bugs
Posts: 142
Joined: Mon Feb 08, 2021 10:10 pm
Location: Scotland
Has thanked: 44 times
Been thanked: 50 times

Re: writing and reading a binary word on some pins

Post by bugs »

I forgot to add a warning.
If your cable includes low level audio signals in the same bunch of wires the capacitive coupling of any digital pulses could inject audible interference.
Also - when using serial, other boot and error data is transmitted so you have to ignore it in the receiver - or use serial2 instead.
Post Reply