How to write code using serin and serout commands?

redmons88

New Member
Hello,
I have connected 2 08m2 Picaxes via C.2 as an output pin and C.3 as an input pin and am trying to setup serial communication between Pic 1 and 2,
What i want to happen is for pic 1 to detect when an input pin is high (C.1 and C.3) and then send these values via the serout command to the second picaxe that can receive the values (using the serin command) and then make output pins C.1 or C.2 high/low depending on what input pin was high on the first picaxe (c.1 or c.3).
I have been trying to write the code but cannot figure out how to send the information that a certain pin is high on the first picaxe and then make an output on the second picaxe. What would an example of the 2 codes (using serin and serout) look like? the examples from the PICAXE manual are not very helpful.
Thanks
 

lbenson

Senior Member
If you're just talking about two pins, you can establish a code for the four possible conditions: "0"=all off, "1"=first on, "2"=second on, "3"=both on.

Alternatively, the easiest way is just to read the port and send the value which you read to the other 08M2 and let it decode it.
Code:
symbol reading=b4
symbol priorReading=b5

do
  reading = pinsC and %00001010 ' mask all but C.1 and C.3
  if priorReading <> reading then
    priorReading = reading
    serout {your pin and baudrate}(reading) 
  endif
loop
 

AllyCat

Senior Member
Hi,

The "problem" with simple serial communications is that the receiver needs to be "listening" (so cannot be doing anything else) when the transmitter sends the data (byte), and/or the transmitter must be repeatedly sending the data. One solution is to use an additional "handshake" or "wakeup" pin to synchronise the data transfers, but the additional pin may be unavailable and may be an unnecessary complication.

A better way to transmit a single data byte is to use HSERIN; then the receiver can go and "collect" the (most recent) data byte whenever convenient. But only pin C.1 can be used for HSERIN (actually it is possible to use C.5 on an 08M2, but that's not really for novices) and you must use HSERSETUP first.

The "easiest" way to transfer up to about 8 bits of data between two PICaxes may be to use PWMOUT and READADC10, between two pins coupled by a "low pass filter". The filter could be just a 10k resistor from the "sending" pin to the receiving pin, the latter having a capacitor to ground (perhaps 100 nF, or higher if the data is not changing quickly). Data could even be transferred in either direction by using two 10k resistors in a "T" network (two resistors in series form the horizontal bar and the capacitor is the vertical

Or a really "dirty trick" for just two bits is to set the output pin as either "Low", "High", "Input" (tristate) or "Weak Pullup" and write some cunning code to detect its state. ;)

Cheers, Alan.
 

redmons88

New Member
Thanks,
Just a quick question of the code.
I understand that that this line (reading = pinsC and %00001010) is only reading pins c.1 and c.3 but am unsure of how the binary part works to define those pins?

also what are these 2 lines doing? -
if priorReading <> reading then
priorReading = reading
 

lbenson

Senior Member
"reading = pinsC and %00001010" causes whatever logic levels ("1"s or "0"s) are present on the pins other than pinC.1 and pinC.3 to be "0"s (in the receiving variable, "reading"), and the logic levels of the bits representing pinsC.1 and pinC.3 to be whatever the pins read. For instance, on a bit level, "1 and 0" equals 0; "1 and 1" equals 1; and "0 and 1" equals 0. (You could look at the Wikipedia article, https://en.wikipedia.org/wiki/Boolean_algebra to see how the binary logic operators, "and", "or", "not" work on bitwise operations.)

When you look for a change in value, you must have retained the old value in order to make the comparison. "if priorReading <> reading then" asks the question, "is the old value (priorReading) equal to the present value (reading)?" If it is not, then you have detected a change, so you save the current reading as the prior one (prior, that is, to the next test), and do whatever you wish to do when you have detected a change.
 

AllyCat

Senior Member
Hi,

am unsure of how the binary part works to define those pins?
-----
also what are these 2 lines doing? -
In the binary notation (prefixed by a % symbol) the bits (or pin numbers) are listed ascending from right to left and starting from zero. That may seem rather illogical, but there are good mathematical reasons why it's done that way. ;) Thus the rightmost bit (0 or1) respresents c.0, the next left c.1, etc. through to c.7 immediately after the % symbol (for a full byte).
-----
The first line is checking to see if the value of "reading" has changed, and although it's convenient to put the second line there, it's not really necessary. The "important" line is the next (third) which SEROUTs some data only when the value changes. It's "convenient" to update the "priorReading" within the IF structure, but not essential. If "reading" hasn't changed, then "priorReading = reading" doesn't do anything useful (because it's the same value), but it does no harm either (except perhaps "wearing out" an EPROM location if it were a WRITE command).

Cheers, Alan.
 
Top