#!/usr/bin/perl # Jesper's MiniDDS for Linux $serial="/dev/ttyS0"; # serial port minidds is connected to $crystal=14318180; # Crystal oscillator that's being used. Note this requires a new firmware in the AVR to change because it affects serial port rate. $resolution=$crystal/9/16777216; # Constant used often in program... $debug=0; # debug mode. $delay=0.01; $script=$0; $script=~s/^.*\///; use Getopt::Std; use Device::SerialPort; getopts("hf:t:l:wd"); if ($opt_h) { print "Usage: $script [-h] [-f frequency] [-t type] [-l serialline]\n"; exit 1; } if ($opt_d) { $debug=1; } if ($opt_l) { $serial = $opt_l; } $port = Device::SerialPort->new($serial) or die "Can't open serial port: $!\n"; $port->baudrate(9600) or die "cannot set baud rate on $serial"; $port->parity("none"); $port->databits(8); $port->stopbits(1); $port->handshake("none"); $port->user_msg(ON); $port->write_settings; $port->lookclear; print "writing spaces\n" if $debug; ($out)=$port->write(" "); print "sent $out chars\n" if $debug; print "reading a char\n" if $debug; sub getachar { my $gotchar=""; until ($gotchar ne "") { $gotchar=$port->lookfor; # poll for data die "no data received\n" unless (defined $gotchar); select(undef, undef, undef, $delay); } return $gotchar; } $saw=&getachar; $l=length $saw; print "saw '$saw' - $l characters ...\n" if $debug; $saw=~s/^F//; $acc=unpack "N", $saw; $freq=$acc*$resolution; print "Current Accumulator is $acc for $freq Hz\n"; if ($opt_t =~ m/^[0-7]$/) { print "changing waveform type\n"; ($out)=$port->write($opt_t); print "sent $out bytes\n" if $debug; select(undef, undef, undef, $delay); } else { print "invalid waveform type (0-7)\n" if $opt_t; } if ($opt_f) { die "Error: can't use 0 as frequency" if ($opt_f == 0); $acc=int ($opt_f/$resolution); $got=$acc*$resolution; $error=int ( 100000*(($opt_f-$got)/$opt_f))/1000; $saw=pack "N",$acc; print "sending new accumulator value '$saw' " if $debug; print "-> Setting want=$opt_f got=$got error=$error%\n"; ($out)=$port->write("s$saw"); print "sent $out bytes\n" if $debug; if ($opt_f > ($crystal/9/16)) { print "WARNING: frequency exceeds resolution, WAVEFORM WILL BE JITTERY\n" } elsif ($opt_f > ($crystal/9/64)) { print "warning: frequency exceeds resolution, waveform is very degraded\n" } elsif ($opt_f > ($crystal/9/256)) { print "warning: frequency exceeds resolution, waveform will have missing samples\n" } } if ($opt_w) { ### custom for my minidds print "writing into eeprom\n"; ($out)=$port->write("w"); }