Interrupting the PICAXE SERIN Command

Introduction

The Picaxe SETINT command checks an input pin for a certain condition between code execution. When condition is true, code will gosub to an interrupt routine, then return to main code. However, the SERIN (serial port input) command is not interruptible - once executed, it waits until data arrives. This means the Picaxe cannot handle multiple tasks once a SERIN is in the code. Obviously not an ideal situation.

But there is a good idea from Chris Leigh which requires an extra Picaxe. Why not use a slave Picaxe to receive data, set a pin to high before sending byte to master Picaxe? The Master can be doing some work while waiting for input from slave. On interrupt, it will gosub and execute the SERIN command.

The Picaxe circuit and programs for testing this idea are shown below:

Sender Program on Slave PICAXE

;Read a byte from PC kbd via a serial port.
;Raise line to high briefly before sending byte in TTL
;level to master PICAXE.

MainLoop:
  SERIN 2,N2400,b0      ;recv 1 byte from kbd
  HIGH 4                ;set line high
  PAUSE 20              ;allow master to respond to hi
  SEROUT 4,T2400,(b0)   ;transmit char (T=True Output)
  LOW 4                 ;set line low
  GOTO MainLoop

Receiver Program on Master PICAXE

;Continuously send a loop count to PC and flash an LED. When a byte is
;sent from slave PICAXE, an interrupt is generated. The byte is
;read and then sent to PC before master resumes its main loop.

  SETINT %00000100,%00000100    ;generate interrupt on pin 2 high
  b1=0                          ;init counter

MainLoop:
  SEROUT 4,N2400,("Loop ",#b1,13,10)   ;continuously output loop count
  b1=b1+1                       ;inc counter
  high 1                        ;turn LED on
  pause 100                     ;sleep
  low 1                         ;turn LED off
  pause 100                     ;sleep
  GOTO MainLoop

Interrupt:                      ;high detected on pin 2
  SERIN 2,T2400,b0              ;receive byte (T=True input)
  SEROUT 4,N2400,("Received ",b0,13,10)	   ;output byte
  SETINT %00000100,%00000100    ;setup interrupt again
  RETURN