Yet another Serin issue [SOLVED]

Ghostbear

Member
Evening all...

I find myself having a bit of a head scratcher with Serout/Serin between a 14M2 and an 08M2

The initial idea involves an IR transmitter /receiver and I thoguht I was having problems with that.

So, I stripped everything back to what i sbasically a slightly modified version of the example code for the serout and serin command, with the Serout and Serin pins connected via a single wire.

And despite stripping it down, I'm still having problems.

Basically, it looks like the receiver code/chip is only picking up on every third byte. And everything is simplified all the way down.

I've had a look through the forum and not had much luck finding anything, so here we go:

14M2 code:

Code:
symbol LED = B.0
setint or %000100,%000100

for b0 = 0 to 63    ; start a loop
      write b0,b0        ; write value into b1
    sertxd("The value of b0 is ",#b0," b1 is ",#b1,13,10)
next b0            ; next loop

main:  

low LED
    pause 2000
    high LED
    pause 200

goto Main

interrupt:
setint off
if pinc.2 = 1 then
        ;high b.4
        ;pause 5
        ;low b.4
    for b0 = 0 to 63    ; start a loop
      read b0,b1        ; read value into b1
      serout B.4,N2400_4,(b1)    ; transmit value to serial LCD
    ;sertxd("The value in memory b0 ",#b0," b1 is ",#b1,13,10)
    next b0  
endif
setint or %000100,%000100
return
and the output to the terminal (included for debug)

Code:
The value of b0 is 0 b1 is 0
The value of b0 is 1 b1 is 0
The value of b0 is 2 b1 is 0
The value of b0 is 3 b1 is 0
The value of b0 is 4 b1 is 0
The value of b0 is 5 b1 is 0
The value of b0 is 6 b1 is 0
The value of b0 is 7 b1 is 0
The value of b0 is 8 b1 is 0
The value of b0 is 9 b1 is 0
The value of b0 is 10 b1 is 0
The value of b0 is 11 b1 is 0
The value of b0 is 12 b1 is 0
The value of b0 is 13 b1 is 0
The value of b0 is 14 b1 is 0
The value of b0 is 15 b1 is 0
The value of b0 is 16 b1 is 0
The value of b0 is 17 b1 is 0
The value of b0 is 18 b1 is 0
The value of b0 is 19 b1 is 0
The value of b0 is 20 b1 is 0
The value of b0 is 21 b1 is 0
The value of b0 is 22 b1 is 0
The value of b0 is 23 b1 is 0
The value of b0 is 24 b1 is 0
The value of b0 is 25 b1 is 0
The value of b0 is 26 b1 is 0
The value of b0 is 27 b1 is 0
The value of b0 is 28 b1 is 0
The value of b0 is 29 b1 is 0
The value of b0 is 30 b1 is 0
The value of b0 is 31 b1 is 0
The value of b0 is 32 b1 is 0
The value of b0 is 33 b1 is 0
The value of b0 is 34 b1 is 0
The value of b0 is 35 b1 is 0
The value of b0 is 36 b1 is 0
The value of b0 is 37 b1 is 0
The value of b0 is 38 b1 is 0
The value of b0 is 39 b1 is 0
The value of b0 is 40 b1 is 0
The value of b0 is 41 b1 is 0
The value of b0 is 42 b1 is 0
The value of b0 is 43 b1 is 0
The value of b0 is 44 b1 is 0
The value of b0 is 45 b1 is 0
The value of b0 is 46 b1 is 0
The value of b0 is 47 b1 is 0
The value of b0 is 48 b1 is 0
The value of b0 is 49 b1 is 0
The value of b0 is 50 b1 is 0
The value of b0 is 51 b1 is 0
The value of b0 is 52 b1 is 0
The value of b0 is 53 b1 is 0
The value of b0 is 54 b1 is 0
The value of b0 is 55 b1 is 0
The value of b0 is 56 b1 is 0
The value of b0 is 57 b1 is 0
The value of b0 is 58 b1 is 0
The value of b0 is 59 b1 is 0
The value of b0 is 60 b1 is 0
The value of b0 is 61 b1 is 0
The value of b0 is 62 b1 is 0
The value of b0 is 63 b1 is 0
And receiver chip code:
Code:
setint OR %00001000,%00001000 ; check for c.3 going high
symbol LED = C.0
;symbol LED = B.0
main:

low LED
    pause 2000
    high LED
    pause 200

goto Main

interrupt:
setint off
for b0 = 0 to 63    ; start a loop
          serin C.3,N2400,b1    ; receive serial value
          write b0,b1        ; write value into b0 location
next b0            ; next loop


for b0 = 0 to 63                ; start a loop
    read b0,b1
    sertxd("The value of b0 is ",#b0," b1 is ",#b1,13,10)
next b0  

pause 5
setint OR %00001000,%00001000 ; check for c.3 going high

return
And the terminal output for the receiver:

Code:
[00][00]The value of b0 is 0 b1 is 1
The value of b0 is 1 b1 is 3
The value of b0 is 2 b1 is 5
The value of b0 is 3 b1 is 7
The value of b0 is 4 b1 is 248
The value of b0 is 5 b1 is 10
The value of b0 is 6 b1 is 12
The value of b0 is 7 b1 is 248
The value of b0 is 8 b1 is 15
The value of b0 is 9 b1 is 252
The value of b0 is 10 b1 is 18
The value of b0 is 11 b1 is 252
The value of b0 is 12 b1 is 21
The value of b0 is 13 b1 is 252
The value of b0 is 14 b1 is 24
The value of b0 is 15 b1 is 252
The value of b0 is 16 b1 is 27
The value of b0 is 17 b1 is 252
The value of b0 is 18 b1 is 30
The value of b0 is 19 b1 is 252
The value of b0 is 20 b1 is 33
The value of b0 is 21 b1 is 254
The value of b0 is 22 b1 is 36
The value of b0 is 23 b1 is 254
The value of b0 is 24 b1 is 39
The value of b0 is 25 b1 is 249
The value of b0 is 26 b1 is 42
The value of b0 is 27 b1 is 254
The value of b0 is 28 b1 is 45
The value of b0 is 29 b1 is 254
The value of b0 is 30 b1 is 48
The value of b0 is 31 b1 is 254
The value of b0 is 32 b1 is 51
The value of b0 is 33 b1 is 254
The value of b0 is 34 b1 is 54
The value of b0 is 35 b1 is 254
The value of b0 is 36 b1 is 57
The value of b0 is 37 b1 is 254
The value of b0 is 38 b1 is 60
The value of b0 is 39 b1 is 254
The value of b0 is 40 b1 is 63
The value of b0 is 41 b1 is 0
The value of b0 is 42 b1 is 2
The value of b0 is 43 b1 is 4
The value of b0 is 44 b1 is 6
The value of b0 is 45 b1 is 8
The value of b0 is 46 b1 is 10
The value of b0 is 47 b1 is 248
The value of b0 is 48 b1 is 13
The value of b0 is 49 b1 is 248
The value of b0 is 50 b1 is 16
The value of b0 is 51 b1 is 252
The value of b0 is 52 b1 is 19
The value of b0 is 53 b1 is 252
The value of b0 is 54 b1 is 22
The value of b0 is 55 b1 is 252
The value of b0 is 56 b1 is 25
The value of b0 is 57 b1 is 252
The value of b0 is 58 b1 is 28
The value of b0 is 59 b1 is 252
The value of b0 is 60 b1 is 31
The value of b0 is 61 b1 is 254
The value of b0 is 62 b1 is 34
The value of b0 is 63 b1 is 254
[00][00][00][00][00][00][00]
Edit:
Found this this morning: https://picaxeforum.co.uk/threads/solving-serin-serout-problems-with-20m2-and-20x2.20342/ - it looks like it could possibly be a similar issue and that my search criteria were a litte too stringent so I'll give this a shot tonight and see how it goes.
 
Last edited:

hippy

Technical Support
Staff member
The main issue is that you are interrupting on the serial input signal going high, then doing a SERIN. This doesn't work because, by the time the interrupt happens, half or more of the data being sent has been lost. The SERIN will either receive corrupt data or later data.

The HIGH-PAUSE-LOW on B.4 in the transmitter code is what would be required to give a pulse to the receiver to wake it up to expect new data, so it's executing SERIN before data is sent, but that has been commented out.

Also your sending loop sends out data quicker than the receiver can keep up, which will also result in corrupt or lost data.

Start with some simpler code to get things working. For the transmitter -
Code:
Do
  SerOut B.4, N2400_4, ( b1 )
  b1 = b1 + 1
  Pause 1000
Loop
For the receiver -
Code:
Do
  SerIn C.3, N2400_4, b1
  SerTxd( #b1, " " )
Loop
That should show an incrementing data value being received.
 

westaust55

Moderator
Also suggest that you move the SETINT instruction from the second line to just before the “main:” label.

As it is now, you could get an interrupt before the data to be displayed has been set up so all the values at least for a first cycle would be zeros.


Note that when an interrupt occurs, interrupts are automatically turned off.
Therefore the SETINT OFF command immediately after the “interrupt:” label is not necessary (but will not cause a problem).
 

hippy

Technical Support
Staff member
One trick for interruptng a receiver with a pulse is to keep the serial out line low when idle, set it high to wake the receiver up, then transmit using a Txxxx baud rate, setting the line low at the end -
Code:
                .--------------------------------- Set output high
                |        .------------------------ Send with Txxxx
                |        |                    .--- Set output low
                |        |                    |
                 ________   _ _ _ _ _ _ _ _ __
Serial out : ___|        |_|_|_|_|_|_|_|_|_|  |___
                          S 0 1 2 3 4 5 6 7 P
That can help simplify the code. To transmit -
Code:
Low TX
Pause 1000
Do
  High TX
  Pause 10
  SerOut TX, T2400, ( b1 )
  Low TX
  b1 = b1 + 1
  Pause 1000
Loop
To receive -
Code:
Gosub Interrupt_Enable
Do
  ; Nothing
Loop

Interrupt:
  SerIn RX, T2400, b1
  SerTxd( #b1, " " )
Interrupt_Enable:
  SetInt ...
  Return
 

Ghostbear

Member
Hi folks,

Thanks for the help.

In the end, it only needed a tiny change - taken from Hippy's first post:
add a brief pause after the serout nd it works fine...
Code:
    for b0 = 0 to 63    ; start a loop
      read b0,b1        ; read value into b1
      serout B.4,N2400_4,(b1)    ; transmit value to serial LCD
          pause 5
    next b0
Code:
symbol LED = B.0
setint or %000100,%000100

for b0 = 0 to 63    ; start a loop
      ;serin 6,N2400,b1    ; receive serial value
      write b0,b0        ; write value into b1
    sertxd("The value of b0 is ",#b0," b1 is ",#b1,13,10)
next b0            ; next loop

main:    

low LED
    pause 2000
    high LED
    pause 200
;
goto Main

interrupt:
setint off
if pinc.2 = 1 then
    for b0 = 0 to 63    ; start a loop
      read b0,b1        ; read value into b1
      serout B.4,N2400_4,(b1)    ; transmit value to serial LCD
          pause 5
    next b0    
endif
setint or %000100,%000100
return
 
Top