Friday, August 14, 2015

Example Code for KS-24361 REF-0 Standalone Operation

In this post, I will provide some example AVR and Arduino code for running the Lucent KS-24361 REF-0 standalone.

An Arduino Uno, and a custom ATTINY841 breakout board with a TCXO.

A little bit about the code...

I posted previously about how to run your KS-24361 REF-0 standalone. In that post, I provided some modified message strings that could be sent to the REF-0 to make it run without the REF-1. It is necessary to program a microcontroller (or some other device with serial capability) to send the strings with the proper timing. Here, I share two example programs I wrote for doing that.

I originally prototyped the code on an Arduino Uno, then transferred it to Atmel Studio in proper C. The program I wrote in Atmel Studio is for an ATTINY841, which is one of my favorite AVR devices. Both programs are very similar as you will see. I tried to keep them simple and easy to read. There are a thousand ways to do anything in programming; this is just how I did it.

Before we dive in, let's take a look at some code. This is from the main loop in the AVR C version:

if (gotPPS == 1) 
  {
    PORTA |= (1 << PINA6);
    _delay_ms(75);
    advanceSecond();
    sendMessages();
    PORTA ^= (1 << PINA6);
    gotPPS = 0;
  }

This is a good starting template for what you need to do in any program to make the REF-0 run standalone. You need to: know when the PPS happens, increment the time, send the messages in the right order as scheduled, then wait for the next PPS.

The programs below are the absolute bare minimum to make this work. There is no checking of the fix status or the real message strings from the GPS. If may or may not be important to you, but by reading messages from your GPS, you can add some good features:
  • Instead of time beginning at 00:00:00 when the microcontroller fires up, you can set it to UTC. This will help with re-syncing to the REF-0 if the microcontroller loses power but the REF-0 doesn't.
  • Check the fix status and other things from the real GPS strings. Even though the REF-0 analyzes the stability of the PPS itself, you might want to force it into holdover immediately when the GPS signal quality degrades. Remember, it's running blind except for the PPS signal and what we tell it.

There are a couple of issues you might run into on other microcontrollers, or if you add features. First, the modified message strings take up a lot of space. You will quickly run out of RAM if you just store them as arrays, and you might want to consider using program memory. Second, watch the timing. If the program is doing nothing but waiting for the PPS interrupt, that's really easy. But if you start doing other things, you will need to be more careful with what happens when.

Finally, if you modify any message strings, don't forget to update the check character.

The Important Stuff

Ok, here's the code! I put these programs on Github as gists. Hopefully that works out well. Let me know in the comments below if you have any issues.

This first program is for an Arduino Uno. It will work on other Arduino models, but you may need to modify a couple of things. When you download the file, simply double click it after unzipping. The Arduino IDE will open and ask to put the file in a directory. Then you can check it out, compile and upload. The pinout is listed in the comments at the top of the program.


The second program was written in Atmel Studio 6 for an ATTINY841. You can easily modify it for other devices by changing the register setup and adapting to the pinout of your device. The pinout for the ATTINY841 is in the comments at the top.


Wrap Up

Good luck getting your REF-0 working! After you test out these example programs, have fun modifying them and adding features. You have complete control over the messages that get sent to the REF-0, so there is great potential for experimentation. If you need help with the hardware setup, be sure to check out the original post on REF-0 standalone operation.

I hope this example code is useful to you. If you have questions, comments, or concerns about the example programs above, please post in the comments below.

Thanks for reading!

- Dan W.

4 comments:

  1. Great work Dan... I look forward to implementing the Arduino version soon. SoftwareSerial has a parameter for inverting the serial IO. It's not well documented, but it would be 'SoftwareSerial myserial(10, 11, true)'. Is there a reason you shouldn't use this, and prefer a hardware inverter? Would be nice to avoid an extra chip if possible. Thanks!

    ReplyDelete
    Replies
    1. Hi, the software inversion works just fine. I didn't use this because I knew I'd want a hardware UART later, but that could definitely be handy for some people. I added a note in the sketch.

      Also, if you later decide to parse real GPS strings, I would suggest using AltSoftSerial at a high baud rate for talking to the GPS. The old SoftwareSerial is painfully slow, but doesn't require library downloads, so that's why I used it in the sketch.

      Thanks.

      Delete
  2. Great work Dan, thanks a bunch. Is that your own custom made break out board for the ATTiny or one that's available somewhere?
    Thanks,
    Joe

    ReplyDelete
    Replies
    1. Hi, it is a breakout that I made myself. I think there is an ATTiny841 breakout available on Tindie. But I wanted mine set up in a certain way, with a decent oscillator on it.

      Delete