readadc10 incorrect function of output pins

scorpy

New Member
i am using the readadc10 feature to read a voltage from 0v to 2.5v
At 0.375V say adc10 77 i require the pic to turn on o/p 0 and stay on unless the level falls below 0.3v adc10 61 the output should flash, also at 2.5v adc10 512 i require output 1 high., at the low end it all works fine but once i get over one volt pin 0 goes out or starts to flash, because of this i cannot get output 1 to work correctly, however if i change back to below 1 volt for all the commands it works fine...any tips?


 
 

BeanieBots

Moderator
Not sure what your question is.
If you want to perform different functions based on a voltage, all you need is a set of If/Then's to goto a part of code depending on the voltage. The slightly trickier part is to keep testing so that you can go back once the voltage changes again.

I THINK this will do what you want.

main:
readadc10 1,w0
if w0 <= then Function1
if w0 > 61 and w0 <= 77 then Function2
if w0 > 511 then Function3
pause 500
goto main

Function1: '0 and 1 off at low volts
low 0
low 1
goto main

Function2: '0 flashing and 1 off
toggle 0
low 1
goto main

Function3: '0 off and 1 on at high voltage
low 0
high 1
goto main
 

scorpy

New Member
Thanks for the help beanie, everything is working fine, pin o wouldnt toggle but that was just for checking purposes , in reality it would just be on or off, so i have changed it accordingly.
I have added a pwmout signal, which runs all the time until the upper limit is reached and pin 1 goes high and the pwmout is switched off, this is now all owrking fine apart from there is a delay on the pwmout going off, this i need to be done in 60us. This is probably due to the fact i do not have a pull down resistor on the pwmout pin, or is it not possible for the pwmout 2,0,0 command to swich the pin off in this time scale?
is there any chance to chat to you privately beanie about this application

 
 

BeanieBots

Moderator
A PICAXE running with a standard clock will take about 250uS per instruction, so doing something within 60uS is asking a lot (but not impossible).
As for private discussions, it would be better to post questions here as you will get a better response and more than just my opinion. Besides, I get so many obscure e-mails it could take a while before I get to it.
 

scorpy

New Member
I am running a 4mhz crystal for the moment, what do you suggest to get this signal to flip in 60 us? use a 16mhz one?
To be honest probably 120us would be ok, i would have to work out the limiting effect on current rise from the inductor as it would be the only thing that would limit the current flow in a short circuit situation
 

andrewpro

New Member
according to my measurements, and those of others, 4mhz is ~250us, 8Mhz is ~125us, and 16Mhz is ~62.5us. This varies form command to command but is generally a good guideline.

As for making thigns go faster...I HAVE run a picaxe at 20Mhz. But, you wont be able to program the picaxe, you wont be able to use serout, serin, IR anyhting, and numerous other commands that require specific timing. Calculating specific PWM requenices and/or duty cycles, for instance, will be well off the mark.

--Andy P
 

scorpy

New Member
Thank you for all the responses, here is the program modified so far, this seems to work in the manner i require.
main:
pause 1000
pwmout 2,49,12
run:
readadc10 0,w0
if w0<= 60 then function1
if w0 >= 80 and w0 < 514 then function2
if w0 >=60 and w0 >=80 and w0 < 514 then function2
if w0 > 514 then function3
pause 500
goto run

function1: '0 and 1 off at low volts
low 0
low 1
goto run

function2: '0 on and 1 off
high 0
low 1
goto run

function3: '0 off and 1 on at high voltage
pwmout 2,0,0
low 0
high 1
stop
Note i am still using a 4mhz crystal at the moment

This does turn off the pwmout signal if not a tad slow. Can anyone suggest a way to use an interupt so when adc10 goes above 514 it will immediatly go to function 3 no matter what it is doing.
I will post the fuller program as it is completed if anyone is interested
 

hippy

Technical Support
Staff member
Taking out that 'PAUSE 500' will improve response times. There's no way to use interrupts with an analogue input, but overall responsiveness could be improved by using a Finite State Machine ...<code><pre><font size=2 face='Courier'>Main:
PAUSE 1000
PWMOUT 2,49,12
Run:
READADC10 0,w0
IF w0 = 514 THEN Run
IF w0 &gt; 514 THEN Function3
IF w0 &gt; 60 THEN Function2 </font></pre></code> <code><pre><font size=2 face='Courier'>Function1: '0 and 1 off at LOW volts
LOW 0
LOW 1
Loop1:
READADC10 0,w0
IF w0 = 514 THEN Loop1
IF w0 &gt; 514 THEN Function3
IF w0 &gt; 60 THEN Function2
GOTO Loop1 </font></pre></code> <code><pre><font size=2 face='Courier'>Function2: '0 on and 1 off
HIGH 0
LOW 1
Loop2:
READADC10 0,w0
IF w0 &gt; 514 THEN Function3
IF w0 &lt;= 60 THEN Function1
GOTO Loop2 </font></pre></code> <code><pre><font size=2 face='Courier'>Function3: '0 off and 1 on at HIGH voltage
PWMOUT 2,0,0
LOW 0
HIGH 1
STOP </font></pre></code> Not sure why an ADC value of 514 is important or a special case, but this is a conversion of your above code.
 

BeanieBots

Moderator
Loose the pause 500 for a start!
It adds a totally pointless delay of 500mS to your loop response.
You will then have 6 command lines each taking about 250uS giving about 1.5mS between tests.
The PICAXE cannot interrupt on a voltage level. To do that, you will need to use a comparitor set to trip at the desired voltage and feed its output into a digital input configured for interrupt.
Interrupts on the PICAXE are polled and thus they are only serviced at the completion of each command. That is why the best you can ever achieve is about 250uS for response plus 250uS to act at standard clock rate.

You could achieve similar timing within your code by inserting the lines &quot;readadc10 0,w0&quot; &amp; &quot;if w0 &gt; 514 then function3&quot; between each of the other tests. That way you only wait one command line before each crucial test.

Edited by - beaniebots on 09/03/2006 19:04:10
 

scorpy

New Member
the special purpose of adc 514 is it is the value of the current limit, if this signal is reached it mean i have an over current, and usually means a short circuit or the power supply has gone out of control, the value relates to 25 amps, from a lem terminated into a 100R giving 0.1v/a therefore 2.5v to the pic which equates to 514, considering i have a rise of 10a/60us in a short circuit situation, so it is imperative i switch off the pwmout as soon as possible as this is part of my drive to control an igbt, It would seem the best option is to use a comparator to pull the pwmout sig to ov, so cutting off the pwmout to a 555 i am using via an op amp to control the igbt. However it is still useful to keep the program as it is as i am using the signal to turn off eventually multiple output operating relays
 

hippy

Technical Support
Staff member
That makes sense. My concern was because the value 514 is handled somewhat differently to other values in your code. That is, if you get a reading of 514 in your 'main:' loop, it stays in the main loop rather than jumping to Function2 or Function3.

I suspect that's not what you intended, and your conditionals ( IF..THEN ) aren't correct.

If you are dealing with a power supply that has &quot;gone out of control&quot; ( mission critical if not safety critical, which it may be ) then you really do have to take exceptional care that your program does as it is expected to.
 

scorpy

New Member
I have come to the conclusion it would be simpler to drop the adc 514 command all together and just use the comparator via a diode to pull down the pwmout signal to 0v and also to use this signal to input 0, the diode is there to block the +15v the comparator would be at in normal operation mode. i would then just to figure out how to use an interupt on this pin.
I will give you a run down of the correct function that i am trying to achieve.
1/ 500ms delay to allow initial power up of all low voltage circuits
2/ pwmout enabled
3/ Check for external safety interlock low ok
4/ Check on/off is off (high) if low check auto function is low..if so soft start
5/ if on off low and auto high wait until on off high then goes back to low then start.
5/ Soft Start:- o/p0 high wait 1 second if i/p3 high then start if not o/p0 low back to 1/
6/ start:- o/p 1 high, wait until i/p 4 high Max 50m/s then o/p 2 and 3 high.
7/ ignition:- loop for 7 seconds checking on/off, i/lock and adc for correct signal, if arrives goto run if not low o/p 0,1,2,3,
8/run:- low o/p 2,3leave o/p4 high for 10ms (note high from the start) then low, checking on/off, i/lock and adc then continues in this state unless on/off, i/lock or adc changes state then depending which one unit shuts down in the appropriate manner.
i wish to thank you for all your help so far
 

Dippy

Moderator
Might be an idea to post your circuit as I'm not sure everyone (e.g. me) understands it. Things like 'safety interlock' - whats that? It sounds grand. etc. etc.
And then the bit that confused me is that you mention soft start and output relays (as in clicky-clicky relays?).

I could never understand why you didn't have the IF w0&gt;514... immediately after the ReadADC as that is the 'priority speed' command isn't it? And it is the highest value. But still, as you say, nowhere near as fast as a comparator doing the power control.
(Was the comparator idea to provide a logic input to PICAXE so you could use interrrupts? Or is it to control the power output (relays)?)

A drawing (even if a bit blocky) would be handy for the lads I think.

Could you not also have a suitably designed pot/div going into an input pin as well as analogue ADC? You could could take adantage of a Schmitt input to use SETINT then. And even then beware the limitations of polled/software interrupts.
 
Top