;SMLA AUTOTUNER 03 PROGRAM ;Created by Andrew Cornwall of Nova Scotia, Canada. ;The program is copyrighted, 2019, under the terms of the ;GNU General Public License version 3 as published by the ;Free Software Foundation. See the GNU General Public License ;for more details: https://www.gnu.org/licenses/ . ; ;This program is free software: you may use it, and under the ;terms of GNU 3 you may redistribute and/or modify it. ; ;This program is distributed in the hope that it will be useful, ;but WITHOUT ANY WARRANTY - without even the implied warranty of ;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ; ;PLATFORM: ;This program was written for the PICAXE 18M2+ microcontroller, using ;the PICAXE Editor 6. ; ;The program is for use with the small magnetic loop antenna autotuner ;circuit created by Andrew Cornwall. The circuit is documented ;at www.acornwall.ca . ; ;NOTES: ;The program assumes that a free-turning (360 degree) variable ;capacitor is attached to an automatic tuner stepper motor with 0.9 ;degree full steps. It is possible to modify the program for use with ;a 1.8 degree full step stepper motor. See comment lines about 106-112 ;below. ; ;The circuit has optional provision to use a sampling antenna relay to ;reduce the current going into the automatic tuner's receiver when a high ;wattage transmission occurs (i.e. not a low wattage tuning signal). ;Although the program provides for relay operation, it does not ;matter that a relay is omitted from the circuit. Without a relay there ;will not be the added protection from possible circuit overload, however, ;with ;transmition of up to 100 watts relay protection may not be needed. ; ; ; ************************ Program Starts Below ****************************** setfreq m32 ;This tells the 18M2+ to work at a 32 MHz microcontroller ;(internal) clock speed. This its maximum speed. I've run the program at ;slower rates, but why bother. The effect of 32 MHz is to speed up the ;pause statement by a factor of 8, e.g. pause 8000 is one second ;or 1000 ms. ;SET TERMINAL BAUD RATE TO 38,400. SERTXD ("SMLA Autotuner Program ver. 03 (Half and Sixteenth Steps)",CR,LF,"By Andrew Cornwall, VE1COR", CR,LF, "Copyright 2019, subject to CPLv3",CR,LF) ;VARIABLES - Variables starting with 'W' have 16 bits and can range in ;value from 0 to 65535, those starting with 'B' have 8 bits and range from ;0 to 255. All variables are integers. symbol AVG_SIGNAL=W0 ;AVG_SIGNAL is the measure of Sample Antenna radiated ;signal symbol DELTA=W1 symbol MAX_SIGNAL=W2 symbol REF_SIGNAL=W3 symbol SIGNAL=W4 symbol M=W5 symbol TEMP_SIGNAL=W6 symbol NUMBER_STEPS=W7 symbol MANUAL=B24 symbol SWEEP=B25 symbol J=B27 symbol N=B26 symbol STEP_COUNT=B20 ;PINS - Pin names, e.g. LED_RED, are assigned here for inputs or outputs. ;Most pins of the 18M2+ can be used as either an input or output. Whether ;a pin is an input or output is determined by how it used in the program. ;Pin references are labeled starting with'B' or 'C' in the 18M2+, ;and represent specidic pin numbers 1 to 18 according to the microcontroller's ;diagram. Pin designations were made mostly for the convenience of wiring. symbol LED_RED=B.0 ;RED LED too high tuning signal. Output HIGH or LOW symbol LED_YELLOW=B.1 ;YELLOW LED manual mode. Output HIGH or LOW symbol LED_BLUE=B.2 ;BLUE LED too low tuning signal. OUTPUT HIGH or LOW symbol BUZZER=B.3 ;Buzzer. Output HIGH or LOW symbol DIR=B.4 ;Determins turn direction of stepper motor. Output HIGH or LOW symbol STEPX=B.5 ;Move stepper moter one increment. Output PULSE symbol MS1=C.7 ;Determins stepper motor partial incremment. Output HIGH or LOW symbol MS2=C.6 ;Determins stepper motor partial incremment. Output HIGH or LOW symbol MS3=B.7 ;Determins stepper motor partial incremment. Output HIGH or LOW symbol SLEEPX=B.6 ;Turns stepper motor ON or OFF. Outut HIGH or LOW symbol ADC=C.1 ;Analogue voltage input from receiver. Input ~0 to ~5 SIGNAL symbol RELAY=C.0; Optional relay - disconnects sampling antenna from ;receiver when tuning is completed. Output HIGH or LOW ;Initial conditions LOW LED_RED ;Off LOW LED_BLUE ;Off LOW LED_YELLOW ;Off LOW BUZZER ;Off LOW STEPX ;Off HIGH MS1 ;HIGH MS1 and LOW MS2 sets the stepper motor to move at Half Step LOW MS2 LOW MS3 ;MS3 Remains LOW except for sixteenthe step of A4988 module driver LOW SLEEPX ;NO power to the stepper motor when LOW LOW DIR ;Direction at the Easy Driver needs to be something to start, ;which way does not matter LOW RELAY ;Antenna disconnected to start SWEEP=0 ;Sweep counts the number of active tuning tries later on in the ;program. MANUAL=0 ;MANUAL MODE=1 when autotuner is in manual mode NUMBER_STEPS=6500 ;6400 is a full turn for a 0.9 degree stepper motor turning ;at sixteenth step. The extra 100 steps provides overlap and may account for ;possible slipage in the stepper motor-variable capacitor linkage. ; ;For a 1.8 degree stepper motor remove (comment out with a ';') the statement ;on line 106 and remove the comment status of the command below: ;NUMBER_STEPS=3250 ; ;Below: pocessor self test and announce the start of low power tuning ;transmission unless manual mode is desired. for j=1 to 2 gosub BLINK_BLUE gosub BLINK_RED gosub BLINK_YELLOW gosub BLINK_BUZZER next j pause 16000 ;Wait 2 seconds for tuning signal to start MAX_SIGNAL=0 ;Initialize maximum value variable HIGH SLEEPX ;Turn on stepper motor power HIGH RELAY ;Connect sampling antenna by relay, if available, to the receiver For j=1 to 20 ;Move variable capacitor away from previous tuned location. pause 10 gosub MOVE next j SERTXD ("STARTING MAX. VALUE SWEEP",CR,LF) STEP_COUNT=7 ;Half Step to start sertxd(LF,CR,"Half Step engaged",CR,LF) ;Program section below finds maximum value of sample antenna voltage ;during one full turn of the variable capacitor. BEGIN_MAX_SIGNAL_SWEEP: for M=1 to NUMBER_STEPS gosub MOVE gosub READ_AVG_SIGNAL if AVG_SIGNAL>MAX_SIGNAL then MAX_SIGNAL=AVG_SIGNAL endif if AVG_SIGNAL>10 then sertxd ("TUNER POS: ",#M," SIGNAL: ",#AVG_SIGNAL, " HIGHEST YET: ",#MAX_SIGNAL,CR,LF) endif M=M+STEP_COUNT ; increment step count according size of step gosub STEP_CHOOSE next M ;Manual Mode triggered by near Zero MAX_SIGNAL. if MAX_SIGNAL<10 then LOW SLEEPX ;Turn off power to stepper motor. MANUAL=1 sertxd ("STARTING MANUAL MODE",CR,LF) HIGH LED_YELLOW ;YELLOW LED ON indicates autotuner is in Manual Mode MAX_SIGNAL=0 REPEAT: ;Manual Mode continually returns to this point gosub READ_AVG_SIGNAL if AVG_SIGNAL>MAX_SIGNAL then ;Record Maximum SIGNAL so far MAX_SIGNAL=AVG_SIGNAL endif sertxd ("MANUAL - SIGNAL: ",#AVG_SIGNAL," HIGHEST YET: ",#MAX_SIGNAL,CR,LF) pause 2500 ;slow manual readings to about 3 times per second goto REPEAT endif ;FROM HERE IS ACTIVE AUTOTUNE MODE SWEEP=0 ;Initialize active tuning sweep counter sertxd (CR,LF,"ACTIVE TUNING FOR APPROX. MAXIMUM SIGNAL ",#MAX_SIGNAL, CR,LF) DELTA=MAX_SIGNAL/20 ;DELTA is a 5% factor to reduce autotune target in ;succesive active tuning sweeps if necessary TRY_ANOTHER_SWEEP: ;This is the point where the program returns if ;another active tuning sweep is needed SWEEP=SWEEP+1 ;Keeps track of number of active tuning sweeps that is occuring. REF_SIGNAL=MAX_SIGNAL-DELTA ;Lower autotune target with each active sweep MAX_SIGNAL=REF_SIGNAL ; Lowers MAX_SIGNAL to prepare for next possible ;active sweep sertxd ("STARTING ACTIVE TUNING SWEEP: ",#SWEEP," - REF_SIGNAL: ",#REF_SIGNAL,CR,LF) ;Announce Active tuning is about to start for next tuning sweep - number ;sweep indicated by buzzer beeps and LED blinks for j=1 to SWEEP gosub BLINK_BUZZER gosub BLINK_RED gosub BLINK_BLUE next j HIGH MS1 ;HIGH MS1 and LOW MS2 sets the stepper motor to move at Half Step LOW MS2 STEP_COUNT=7 ;initially use Half Step sertxd(LF,CR,"Half Step engaged",CR,LF) SWEEP=SWEEP+1 ;Keeps track of number of active tuning sweeps that is occuring. ;Start of an active tuning sweep process for M=1 to NUMBER_STEPS gosub MOVE gosub READ_AVG_SIGNAL ;Send data to computer, if attached, only for radiated signal values greater ;than 10 if AVG_SIGNAL>10 then sertxd ("TUNER POS: ",#SWEEP," / ", #M," TUNED SIGNAL: ",#AVG_SIGNAL, CR,LF) endif ;Check to see if radiated signal is greater than or equal to the target, if ;YES go to final autotuning stage. If NOT continue the tuning sweep. if AVG_SIGNAL>=REF_SIGNAL then goto QUIT_TUNING endif M=M+STEP_COUNT ; increment step count accoring size of step gosub STEP_CHOOSE Next M ;If the target radiated signal was not encountered during the active tuning ;sweep, try again with a lower target for up to four tries. if M>=NUMBER_STEPS then if SWEEP<4 then goto TRY_ANOTHER_SWEEP endif sertxd ("* UNABLE TO TUNE",CR,LF) LOW SLEEPX ;Turn off power to stepper motor goto EXIT_TUNING endif ;Active tuning should be completed. The sequence below is: turn off ;power to stepper motor, perform a post tuning radiated signal diagnostic ;sound buzzer, and light status LEDs. QUIT_TUNING: ;Calibrate Option ;Use the statements below as a last resort for consistent best tuning. ;Uncomment statements as needed ; ;toggle DIR ;this statement makes the var. cap. turn backwards if desired ;gosub MOVE ; turns var. cap. a little. Add below more 'gosub MOVE' statements as needed ; ;End of Calibrate Option section pause 4000 For J=1 to 10 ;Final diagnostic to check if radiated signal is reasonably stable gosub READ_AVG_SIGNAL sertxd ("FINAL SIGNAL: ",#AVG_SIGNAL, CR,LF) pause 2000 Next J EXIT_TUNING: LOW RELAY ;turn off sampling antenna relay gosub BLINK_BUZZER sertxd ("TUNING FINISHED",CR,LF) if MAX_SIGNAL>875 then ; Blink red LED for possible receiver saturation HIGH LED_BLUE ;Blue LED stays ON gosub BLINK_BUZZER ;second buzzer sound to indicate possible problem sertxd ("* HIGH SIGNAL SATURATION MAY HAVE OCCURED",CR,LF) STAY_HERE_4: gosub BLINK_RED goto STAY_HERE_4 endif if MAX_SIGNAL<101 then ; Blink Blue LED for signal too low to tune HIGH LED_RED ;RED LED stays ON gosub BLINK_BUZZER ;second buzzer sound to indicate possible problem sertxd ("* SIGNAL TOO LOW TO TUNE",CR,LF) STAY_HERE_3: gosub BLINK_BLUE goto STAY_HERE_3 endif ;Signal poor tuning: more than 20% (1 dB) below MAX_SIGNAL DELTA=5*DELTA REF_SIGNAL=MAX_SIGNAL-DELTA If AVG_SIGNAL875 then; while tuning indicates possible saturation gosub BLINK_RED sertxd ("Probable Saturation Measurement of : ",#AVG_SIGNAL,cr,lf) endif return STEP_CHOOSE: ;Note Below: When counting steps per micro step mode: 7 is sixteenth, 1 is half. if AVG_SIGNAL<10 and STEP_COUNT<>7 then ;use Half Step STEP_COUNT=7 pause 100 HIGH MS1 LOW MS2 LOW MS3 pause 100 sertxd(LF,CR,"Half Step engaged",CR,LF) endif if AVG_SIGNAL>=10 and STEP_COUNT<>0 then ;use Sixteenth Step STEP_COUNT=0 pause 100 HIGH MS1 HIGH MS2 HIGH MS3 sertxd(LF,CR,"SIXTEENTH Step engaged",CR,LF) pause 100 endif return