Skip to content

Instantly share code, notes, and snippets.

@edy555
Last active October 12, 2015 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edy555/c71d0256e6aaebee49e6 to your computer and use it in GitHub Desktop.
Save edy555/c71d0256e6aaebee49e6 to your computer and use it in GitHub Desktop.
GPSのPPS信号でOCXOの周波数調整をDACで制御するための適応フィードバックの実装抜粋。see http://ttrftech.tumblr.com/post/131004700971/
short phase_err;
short phase_err_int;
short duration_count;
short duration_exp;
#define REFERENCE 10000000L
static void
dac_control_init()
{
phase_err = 0;
phase_err_int = 0;
duration_count = 0;
duration_exp = 0;
}
static void
control_dac()
{
long d = counter_delta - REFERENCE;
if (d > 10 || d < -10) {
phase_err = 0;
phase_err_int = 0;
duration_exp = 0;
duration_count = 0;
return;
}
phase_err += d;
phase_err_int += phase_err;
if (duration_count == 0) {
short error = phase_err;
short emod;
if (duration_exp > 0 && duration_exp < 8) {
if (phase_err_int < 0)
error -= (-phase_err_int) >> (duration_exp-1);
else
error += phase_err_int >> (duration_exp-1);
}
if (duration_exp < 3)
emod = error * 16;
else if (duration_exp < 5)
emod = error * 2;
else if (duration_exp < 6)
emod = error;
else
emod = error > 0 ? 1 : -1;
if (error == 0) {
if (duration_exp <= 12)
duration_exp++;
} else if (error > 1) {
set_dac_value(dac_value - emod);
if (error >= 4 && duration_exp > 0)
duration_exp--;
phase_err = 0;
phase_err_int = 0;
} else if (error < -1) {
set_dac_value(dac_value - emod);
if (error <= -4 && duration_exp > 0)
duration_exp--;
phase_err = 0;
phase_err_int = 0;
}
duration_count = 1L << duration_exp;
}
duration_count--;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment