profmason.com

December 27, 2008

PICAxe Serial communications

Filed under: Daily — profmason @ 11:11 pm

I started out revisiting the picaxe wireless problem and got tangled up in some of the limitations of the picaxe serial design.  The picaxe chips only support a blocking serial input (Meaning all program execution including timers STOPS until the serial input is received.  This has stymied me in the past in attempts to build picaxe based serial servo controllers etc.  I won’t admit to the problem that had me going for most of the evening.

However, using interrupts you can somewhat mitigate the problem. If you set an interrupt on the serial line and then toggle the line prior to the transmission, this means you can do something else instead of waiting for serial input all the time.

Here is code of a simple RX/TX between two picaxes.  Important things to remember:

  • tie the hardware RX line (pin 2) to ground.
  • tie the grounds of the two picaxes together!

Here is the code for the Receiver section:

'Serial Receive code using an interrupt.  The code expects that the transmitter will pull the line high briefly prior to the transmission.

sertxd ("Start",13,10)
main:
    setint %00000100,%00000100  'Set an Interrupt on pin 2 to look for the pin to go high
    high 1  'Do something while waiting for the interrupt.  In this case, flash an LED
    pause b0
    low 1
    pause b0
goto main

interrupt:  'If the interrupt goes high this means that there is serial data waiting
    serin 2,N2400,b0  'Get the byte of serial data
    sertxd (#b0,13,10) 'Transmit it to the terminal
return

Here is the Code for the Transmitter Section

'Serial Transmit Code
sertxd ("Start",13,10)
main:
    for b0 = 1 to 250 'Cycle through numbers
        high 1 'toggle the line to trigger the interrupt
        pause 2
        low 1
        pause 2
        serout 1,N2400, (b0) 'Output the data
        pause 2
        sertxd (b0) 'Mirror to terminal (debugging)
        pause 500  'Wait to send the next data byte
     next b0
goto main

The code transmits from pin 1 of one picaxe to pin2 of another picaxe.  It flashes an LED on the RX picaxe with a period eqaul to the transmitted value.

Next comes replacing the wire with a wireless RX/TX pair.  I will have to revisit how I handle the interrupts also.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress