Adding hysteresis to an analog value

fernando_g

Senior Member
The snippet below actually consists of two routines. Each one with (slight) advantages or disadvantages to the other.

Use only one.

'
Code:
testing of two routines which provide hysteresis to a compared value.
'In this example, I'm adding +/- 11 counts to a setpoint value of 128
'Using 08M2 port C.1 as analog input, port C.0 as output
*******************************************************************
'first routine, more lines, less compiled bytes (24)
schmitt_trig1:
readadc C.1, b0
if pinC.0 = 1 then
	goto lohyst
endif
hihyst: 'C.0 =0
if b0 >139 then
	high C.0
endif
lohyst: 'C.1 =1
if b0 <117 then
	low C.0
endif
'second routine, more compact, more compiled bytes (26)
schmitt_trig2:
readadc C.1, b0
if b0 >139 and pinC.0 =0 then
	high C.0
elseif b0 <117 and pinC.0 =1 then
	low C.0
endif
 

hippy

Technical Support
Staff member
I think the C.0 output pin is being used as a physical flag for when the ADC has been low or high and that output pin is being read as an input to set the next ADC level at which a change will occur.

The +/-11 relates to the positions at which switching will occur, 128+11 = 139, 128-11 = 117. The algorithm seems to be -

If the ADC was low and ADC > 137 it is now high, if it was high and ADC < 117 it is now low. It seems to me this could be achieved without needing to know if the ADC was previously high or low ...

Code:
ReadAdc C.1, b0
Select Case b0
  Case > 139 : High C.0
  Case < 117 : Low  C.0
End Select
 

techElder

Well-known member
I'm assuming too much, probably, because the question in my mind was "Why use an output to indicate a flagged condition?"

The answer in my mind is, "The output is electronically changing a threshold value or divider ratio ... somewhere."

Only those in the know ... know. :rolleyes:
 

fernando_g

Senior Member
Hey Tex;
you are absolutely correct, I should have added more info in my message.
Essentially, I was attempting to duplicate the operation of a "classic" opamp schmitt trigger, whereas positive feedback from the output is added or substracted to the analog voltage being compared, as shown on the first image.

Therefore my poor analog brain, decided that a Picaxe S.T. emulation would have to have some output dependency.
Hippy has proved me wrong, with a very compact and elegant code...thanks Hippy, for your valuable insight!

If you want to have a good laugh, please check the second image...this is what I initially considered to perform the schmitt trigger function with a Picaxe.....go ahead, open it up and smile.:D
 

Attachments

Last edited:

techElder

Well-known member
No laughing from me! You should see some of the stuff I come up with!

Glad you got some help here. I always do.
 

AllyCat

Senior Member
Hi,

Or do it with "real" hardware: ;)

Most PICaxes (including the 14M2) have at least one on-chip comparator that can be accessed with a few POKESFRs. Complete with optional hysteresis (~50 mV in "normal" mode, 10mV in Low Power mode) which can be enhanced with the on-chip DAC (basically a 150k potentiometer that can be optionally connected to B.1, B.0 and Gnd pins). Configure the "wiper" to link to the comparator positive input to give an adjustable slicing level, and/or externally connect the "Reference" end (B.1) to the Comparator Output (B.2) for adjustable hysteresis.

Add R-C feedback to the negative input and you can make an oscillator. Currently (!), I'm using a humble 08M2 to implement a current-to-frequency ADC (particularly for photoelectric currents). Just 3 external components to achieve around 6 decades (20 bits) of dynamic range (e.g 1 Hz to 1 MHz). :rolleyes:

Much more is possible, but the serial output (e.g. for debugging) also coming out on B.0 can be rather challenging. :(

Cheers, Alan.
 

fernando_g

Senior Member
Hi,

Or do it with "real" hardware: ;)

Most PICaxes (including the 14M2) have at least one on-chip comparator that can be accessed with a few POKESFRs.

Cheers, Alan.
Are those documented, or do I have to pore over Microchip's datasheets?
To me, SFR Poking is like the Windows Registry: Eingang Verboten
 

AllyCat

Senior Member
Hi,

Well, I will concede that using the SFRs on a PICaxe can be quite "Hard Work" (poring through Microchip datasheets), but IMHO not comparable to editing the Windows Registry (where you might cause long-term "damage" to your system). Using a PICaxe DISCONNECT unwisely is much more likely to cause you grief than a few POKESFRs. ;)

The Comparator(s) and "Timer 1 Gate Control", etc. are not "officially" supported on M2s, but the commands are quite similar to those available on X2s, and hippy has been known to support and even recommend their use on occasions. :) The FVR and DAC (and the SR Latch, etc.) are supported on M2s, and sometimes it's apparent that the PE is doing little more than creating a POKESFR semi-automatically (but a direct POKESFR can produce a faster or more useful functionality).

Certainly it is necessary to get to grips with sections of the base PIC data sheet (which could be improved with a few more cross-referencing hot links) but IMHO it's well worthwhile for the comparator functions. The main things to be aware of are that X2s and M2s use different SFR maps; in the case of the M2s, the first 32 bytes of the first 8 SFR banks are mapped into a single byte. But attempting to access any of the first 12 bytes of each bank causes an instant reset, so put some identifyable "starting" code at the beginning of your program to catch any unexpected resets. Also, if accessing port pins, beware that some of the port.pin names are remapped between the PICaxe M2s and their base PIC. But the "base Port A" is identical between 08, 14 and 20 pin M2s, so the SFR version actually may be more "portable", if plugging those particular devices into the same socket.

Cheers, Alan.
 
Top