If you need to monitor a number of analog inputs and transmit the ASCII data over a serial link, the following 18X code is unable to maintain a constant scan rate. As the loop time will be dependant on the analog input levels, due to higher ADC input voltages generating more ASCII characters per sample. When running the example code at 8MHz, reading three zero inputs results in a scan rate of 70Hz but this drops to only 40Hz when reading three full-scale voltage inputs.
setfreq m8
start:
readadc10 0,w0
readadc10 1,w1
readadc10 2,w2
sertxd (#w0,",",#w1,",",#w2,cr)
goto start
This potential problem can be overcome by triggering each scan from an external clock pulse, via a digital input. However, if you don’t want to add another chip (e.g. 555) I have developed an alternative three 'passive' component solution. A resistor is connected between a PICAXE output and one of its (Schmitt) inputs, with a capacitor between the input and ground. A diode is placed in parallel with the resistor with the anode towards the capacitor.
The following code is revised to discharge the capacitor (via the diode) at the start of each scan. The capacitor then starts to charge, through the resistor, whilst data is being transmitted. At the end of transmission the PICAXE monitors the input and triggers the next scan when the voltage across the capacitor exceeds the 'logic 1' threshold. As an example, a 100K resistor and 0.33uF capacitor will result in a scan rate of about 30Hz, which provide an adequate margin for the worst-case condition (e.g. all analog inputs at full scale).
setfreq m8
high 7
start:
pulsout 7, 200
readadc10 0,w0
readadc10 1,w1
readadc10 2,w2
sertxd (#w0,",",#w1,",",#w2,cr)
tick:
if pin6 = 0 then : goto tick : else : goto start: endif
Tests show that this approach offers timing stability that is at least as good as the 18X internal clock (using a polypropolene capacitor, thin film resistor and BAT 85 diode). To better this you would need to use a 28X1 and crystal, with each scan triggered from the internal timer.
setfreq m8
start:
readadc10 0,w0
readadc10 1,w1
readadc10 2,w2
sertxd (#w0,",",#w1,",",#w2,cr)
goto start
This potential problem can be overcome by triggering each scan from an external clock pulse, via a digital input. However, if you don’t want to add another chip (e.g. 555) I have developed an alternative three 'passive' component solution. A resistor is connected between a PICAXE output and one of its (Schmitt) inputs, with a capacitor between the input and ground. A diode is placed in parallel with the resistor with the anode towards the capacitor.
The following code is revised to discharge the capacitor (via the diode) at the start of each scan. The capacitor then starts to charge, through the resistor, whilst data is being transmitted. At the end of transmission the PICAXE monitors the input and triggers the next scan when the voltage across the capacitor exceeds the 'logic 1' threshold. As an example, a 100K resistor and 0.33uF capacitor will result in a scan rate of about 30Hz, which provide an adequate margin for the worst-case condition (e.g. all analog inputs at full scale).
setfreq m8
high 7
start:
pulsout 7, 200
readadc10 0,w0
readadc10 1,w1
readadc10 2,w2
sertxd (#w0,",",#w1,",",#w2,cr)
tick:
if pin6 = 0 then : goto tick : else : goto start: endif
Tests show that this approach offers timing stability that is at least as good as the 18X internal clock (using a polypropolene capacitor, thin film resistor and BAT 85 diode). To better this you would need to use a 28X1 and crystal, with each scan triggered from the internal timer.