X893

USB dongle with TR24A radio module

This USB adapter can communicate with other TR24A in char-to-char or packet mode.
Add second TR24 to full-duplex communication (with a little changes).
Example. Eagle schematic and layout

#include <TR24.h>

// Arduino 0015
// Teensyduino 0.4
// TR24 library 0.1

// Build on AT90USB162 with TR24A radio module

int TR24_CHANNEL = 6;   // Default TR24 channel
int LED = 4;            // PD4 - LED connected to digital pin 4

char TR24Buffer[64];
char USBBuffer[64];

void setup()
{
    Serial.begin(19200);
    pinMode(LED, OUTPUT);
    LEDOFF();
    TR24.begin(TR24_CHANNEL);
}

void loop()
{
    char* ptr;
    byte error;

    // Blink 0.2 seconds interval while USB serial not connected
    while(!(Serial.get_control() & USB_SERIAL_DTR))
    {
        LEDTOGGLE();
        delay(200);
        return;
    }

    LEDOFF();
    Serial.flush();

    error = TR24.on();
    if(error > 0)
    {
        Serial.println("ERROR: Initialization error");
        BlinkLED(error);
        delay(1000);
        return;
    }

    Serial.println("OK");
    TR24.println("OK");
    TR24.mode_recieve();

    ptr = USBBuffer;
    error = 0;
    while (Serial.configured() && (Serial.get_control() & USB_SERIAL_DTR))
    {
        int c = Serial.read();
        if(c != -1)
        {
//    Unbuffered send
            TR24.print((char)c);
/*    Buffered send
            // Check for buffer overflow
            if((ptr - USBBuffer) >= sizeof(USBBuffer))
                error = 1;
            else
                *ptr++ = (char)c;

            if( c == '\n')
            {
                if(error)
                    Serial.println("ERROR: Data too long");
                else
                {
                    *ptr = 0;
                    LEDON();
                    TR24.send(USBBuffer, (byte)(ptr - USBBuffer));
                    LEDOFF();
                    TR24.mode_recieve();
                }
                ptr = USBBuffer;
                error = 0;
            }
*/
        }
        if(TR24.available())
        {
            TR24.read(TR24Buffer, sizeof(TR24Buffer));
            TR24.mode_recieve();
            Serial.print(TR24Buffer);
        }
    }
    TR24.mode_idle();
}
void LEDOFF()    { digitalWrite(LED, HIGH); }
void LEDON()     { digitalWrite(LED, LOW); }
void LEDTOGGLE() { digitalWrite(LED, !digitalRead(LED)); }
void BlinkLED(byte blinks)
{
    while(blinks-- > 0)
    {
        LEDON();
        delay(500);
        LEDOFF();
        delay(500);
    }
    delay(1000);
}

0 comment(s) so far

Post your comment

Thanks for your comments

  • Comment

Github
Bitbucket
SF.net

Skype
Telegram

Subscribe to x893 blog Subscribe