Code tested and working fine. Header and Pin definitions change.
#Picaxe 08M2
#No_Data
#Terminal OFF
; .----------------------------.
; | User Defined Configuration |
; `----------------------------'
Symbol TRIGGER
= pinC.3
Symbol OUT = C.1 ; Top right
Symbol POT_ADC =
B.4
Symbol FREQ_ADJUST_US = 30000 ; Add 30ms, 30000 us, below 2Hz
Symbol FREQ_PERIOD_US = 500000 ; Frequency period to add adjustment, 2Hz = 500 ms, 500000 us
; .----------------------------.
; | Code Defined from here on |
; `----------------------------'
Symbol reserveW0 = w0 ; b1:b0
Symbol potValue = w1 ; b3:b2 - Raw pot value
Symbol potTime = w2 ; b5:b4 - Time to add for the pot
Symbol highCount = w3 ; b7:b6 - Count of how long the high period was
Symbol highTime = w4 ; b9:b8 - Time of the high period
Symbol period = w5 ; b11:b10 - The time from start of pulse to start of next
Symbol adjustTime = w6 ; b13:b12 - Time to extend poulse depnding on frequency
; Make user defined constants code usable
Symbol FREQ_ADJUST_10US = FREQ_ADJUST_US / 10
Symbol FREQ_PERIOD_100US = FREQ_PERIOD_US / 100
; Other constants for tweaking timing
Symbol PULSE_SUBTRACT_10US = 111 ; Adjust to make pulse accurate
Symbol OVERHEAD_ADD_100US = 15 ; Adjust period for code overhead
; Run at 32MHz for maximum resolution
SetFreq M32
; Time for frequency adjustment - None until we calculate it
adjustTime = 0
Do
; Read analog input for pot
ReadAdc10 POT_ADC, potValue
; Calculate the time for pot, 30ms to 100ms, 3000 to 10000 in 10us units
; 0 to 100% = 0 to 1023
; * 7 = 0 to 7161
; + 3000 = 3000 to 10161
potTime = potValue * 7 + 3000
; Adjust so output pulse timing is spot-on
; We temporarily adjust the potTime because we know it will always be a lot
; higher than the adjustment. We add it back after the pulse so potTime is
; an actual representation of what was required, not what was used to make
; the pulse,
potTime = potTime - PULSE_SUBTRACT_10US
; Ensure the input signal is high
Do : Loop Until TRIGGER = 1
; Wait for falling edge, counting time
highCount = 0
Do
highCount = highCount + 1
Loop Until TRIGGER = 0
; Generate the pulse
; 1 x for 4MHz
; 2 x for 8MHz
; 4 x for 16MHz
; 8 x for 32MHz
High OUT
PauseUs potTime : PauseUs potTime : PauseUs potTime : PauseUs potTime
PauseUs potTime : PauseUs potTime : PauseUs potTime : PauseUs potTime
PauseUs adjustTime : PauseUs adjustTime : PauseUs adjustTime : PauseUs adjustTime
PauseUs adjustTime : PauseUs adjustTime : PauseUs adjustTime : PauseUs adjustTime
Low OUT
; Adjust the potTime back to what its actual value should be. Undo the
; adjustment made earlier
potTime = potTime + PULSE_SUBTRACT_10US
; Determine how long the high period was
; At 1 Hz with 10 ms pulse count is 4597 time wanted is 9900 in 100 us units
; 2 Hz 2276 4900
; 3 Hz 1501 3230
; 4 Hz 1116 2400
; 5 Hz 884 1900
; 6 Hz 726 1550
; The ratio of time to count is ..
; 1 Hz : 9900 / 4597 = 2.15357842071
; 2 Hz : 4900 / 2276 = 2.15289982425
; 3 Hz : 3230 / 1501 = 2.15189873418
; 4 Hz : 2400 / 1116 = 2.15053763441
; 5 Hz : 1900 / 884 = 2.14932126697
; 6 Hz : 1560 / 726 = 2.14876033058
; So time is approx count x 2.152
highTime = highCount / 500 ; count * 0.002 +
highTime = highCount / 20 + highTime ; count * 0.050 +
highTime = highCount / 10 + highTime ; count * 0.100 +
highTime = highCount * 2 + highTime ; count * 2.000
; Values when testing - Using 2.152 multiplier
; 1 Hz : 9889 = 9900 - 11 1100 us error 0.11%
; 2 Hz : 4896 = 4900 - 4 400 us 0.08%
; 3 Hz : 3228 = 3230 - 2 200 us 0.06%
; 4 Hz : 2398 = 2400 - 2 200 us 0.08%
; 5 Hz : 1899 = 1900 - 1 100 us 0.05%
; 6 Hz : 1561 = 1560 + 1 100 us 0.06%
; So pretty good
; Determine what the input freq period was
; This is the high time ...
; plus the pot time
; plus the adjust time
; plus our code overhead time
; Note high time is in 100 us units, others in 10 us units
; We want period in 100 us units
period = OVERHEAD_ADD_100US ; adjustment for code overhead
period = highTime + period
period = potTime / 10 + period
period = adjustTime / 10 + period
; Calculate what the frequency-based adjustment time should be
; For example ...
; This hould be 0 when above 2 Hz and 30 ms when below
; Alternatively 0 when period below 500 ms, 30 ms when above
If period < FREQ_PERIOD_100US Then
adjustTime = 0
Else
adjustTime = FREQ_ADJUST_10US
End If
Loop