Rev Author Line No. Line
3489 kaklik 1 #!/usr/bin/python
3502 kakl 2 #
3 # Sample of measiring and correct frequency with ACOUNTER02A
3489 kaklik 4  
5 import time
6 import datetime
7 import sys
8 from pymlab import config
9  
10 #### Script Arguments ###############################################
11  
3502 kakl 12 if (len(sys.argv) > 3) or (len(sys.argv) < 2):
3489 kaklik 13 sys.stderr.write("Invalid number of arguments.\n")
3502 kakl 14 sys.stderr.write("Usage: %s PORT_ADDRESS [REQUIERED_MHz]\n" % (sys.argv[0], ))
3489 kaklik 15 sys.exit(1)
16  
17 port = eval(sys.argv[1])
18 #### Sensor Configuration ###########################################
19  
20 cfg = config.Config(
21 port = port,
22 bus = [
3498 kakl 23 { "name":"counter", "type":"acount02"},
24 { "name":"clkgen", "type":"clkgen01"},
3489 kaklik 25 ],
26 )
27 cfg.initialize()
28  
3492 kaklik 29 print "Frequency counter readout example \r\n"
3489 kaklik 30 fcount = cfg.get_device("counter")
3498 kakl 31 fgen = cfg.get_device("clkgen")
3489 kaklik 32 time.sleep(0.5)
3492 kaklik 33 frequency = fcount.get_freq()
3498 kakl 34 rfreq = fgen.get_rfreq()
35 hsdiv = fgen.get_hs_div()
36 n1 = fgen.get_n1_div()
37 fdco = 0
38 fxtal = 0
3502 kakl 39 regs = [0, 0, 0]
3489 kaklik 40  
3498 kakl 41  
3489 kaklik 42 #### Data Logging ###################################################
43  
44 try:
3502 kakl 45 with open("frequency.log", "a") as f:
3492 kaklik 46 while True:
47 now = datetime.datetime.now()
48 if (now.second == 15) or (now.second == 35) or (now.second == 55):
49 frequency = fcount.get_freq()
3502 kakl 50 if (len(sys.argv) == 3):
51 regs = fgen.set_freq(frequency/1e6, float(eval(sys.argv[2])))
3492 kaklik 52 now = datetime.datetime.now()
3489 kaklik 53  
3498 kakl 54 rfreq = fgen.get_rfreq()
55 hsdiv = fgen.get_hs_div()
56 n1 = fgen.get_n1_div()
3502 kakl 57 fdco = (frequency/1e6) * hsdiv * n1
3498 kakl 58 fxtal = fdco / rfreq
59  
60 sys.stdout.write("frequency: " + str(frequency) + " Hz Time: " + str(now.second))
61 sys.stdout.write(" RFREQ: " + str(rfreq) + " HSDIV: " + str(hsdiv) + " N1: " + str(n1))
3502 kakl 62 sys.stdout.write(" fdco: " + str(fdco) + " fxtal: " + str(fxtal) + "\r")
3492 kaklik 63 f.write("%d\t%s\t%.3f\n" % (time.time(), datetime.datetime.now().isoformat(), frequency))
3489 kaklik 64  
3492 kaklik 65 sys.stdout.flush()
3502 kakl 66 time.sleep(0.9)
3489 kaklik 67 except KeyboardInterrupt:
3492 kaklik 68 sys.stdout.write("\r\n")
3489 kaklik 69 sys.exit(0)
3492 kaklik 70 f.close()