X2 onboard level converter!?

lanternfish

Senior Member
A recent edition of Silicon Chip contains a PIC controlled GPS Car Trip Meter project (sorry, I can't remeber the PIC id).

What is interesting about this project is that the GPS module is 3V, the PIC 5V (why didn't they just use a 3V PIC?) and to interface the serial from the GPS to the PIC they have used a neat trick utilising an onboard (PIC) comparator and onboard (PIC) voltage reference.

Essentially the serial from the GPS is applied to the non-inverting input of the comparator and the reference voltage is applied to the inverting input of the comparator. The output of the comparator is connected to the hserin of the PIC.

The internal Vref is programmed internally. (In this project it is set to 1.35V).

So, having had an admittedly brief look at the datasheets for the PIC18F2420(28X2) I am wondering how the Comparator Vref output pin can be accessed on the 28X2.

In regards to the 20X2, the Vref for the Comparator is also the Vref for the ADC's so this may cause problems there if both are required. Mind you, a little clever programming will get around that.

Will post a circuit diagram and more info later.
 

hippy

Ex-Staff (retired)
Neat trick - At the back of my mind I expected the comparators could be used for something clever but hadn't imagined what, and I expect level converter would have been well down my list of thoughts.

Take a look at the COMPSETUP command in the PICAXE manual. This does much of what you want to configure the comparators but doesn't route the outputs to physical output pins AFAIR. That may be a simple matter of twiddling some SFR - I'm sure I tested that on a PICAXE so I'll have a look to see what I can find.

If this can work then that means there may be one or two 'inverting logic gates' available which could be put to some use in some applications, either for input or output. Certainly could be useful where one wants a complementary output and in this level shifting case.
 

hippy

Ex-Staff (retired)
Look what I found ( either a late or very early Xmas Prezzie ). 28X2 and 40X2 should be similar ...

Code:
#Picaxe 20X2
#No_Data
#No_Table
#Terminal 9600

' Comp 1 = ADC6. leg 14, B.4
' Comp 2 = ADC5, leg 15, B.3

' Out  1 = leg 17, B.1
' Out  2 = leg 6,  C.4

Symbol config = w2
Symbol ivr    = w3

bit9 = 0 ' 0 = Comp 2 Vref is ladder  1 = Comp 2 Vref is 1.024V
bit8 = 0 ' 0 = Comp 1 Vref is ladder  1 = Comp 1 Vref is 1.024V
bit7 = 1 ' 0 = Comp 2 V+ is ADC2      1 = Comp 2 V+ is Vref
bit6 = 1 '                            1 = Comp 1 V+ is Vref
bit5 = 0 ' 0 = Comp 2 no comflag      2 = Comp 1 sets compflag
bit4 = 0 ' 0 = Comp 1 no comflag      1 = Comp 1 sets compflag
bit3 = 0 ' 0 = Comp 2 non-invert      1 = Comp 2 inverted
bit2 = 0 ' 0 = Comp 1 non-invert      1 = Comp 1 inverted
bit1 = 1 ' 0 = Comp 2 disabled        1 = Comp 2 enabled
bit0 = 1 ' 0 = Comp 1 disabled        1 = Comp 1 enabled

config = w0

bit7 = 1 ' 0 = Ladder disabled        1 = Ladder enabled
bit6 = 0
bit5 = 0
bit4 = 1 ' Ladder select Vref x 0.5
bit3 = 0 ' Ladder select Vref x 0.25
bit2 = 0 ' Ladder select Vref x 0.125
bit1 = 0 ' Ladder select Vref x 0.0625
bit0 = 0 ' Ladder select Vref x 0.03125

ivr = w0

CompSetup config, ivr

' Route physical comparator output Comp 1 to B.1 (leg17)

Output B.1
PeekSfr $6D, b0
bit5 = 1
PokeSfr $6D, b0

' Route physical comparator output Comp 2 to C.4 (leg6)

Output C.4
PeekSfr $6B, b0
bit5 = 1
PokeSfr $6B, b0

' That's all folks - everything is now happening in hardware

Do : Loop
 
Last edited:

hippy

Ex-Staff (retired)
This looks quite useful.

The comparator inputs can come from one of four sources and it should possible to use a digital output pin and also feed that to the comparator input entirely internally so that gives an automatic complementary output ( or straight-through, depending on the invert setting ) so you've got an opposite polarity signal or an easily switchable one.

Using a comparator with a + and - feed it should technically be possible to create AND or OR gates for modulating carriers without an external chip; useful for inverting or modulating signals to drive high power IR LED's via a FET for IROUT.

Correspondingly it should be possible to read the comparator output pin as an input internally and thus apply inversion to the input to the comparator.

Using the 'comparator flag setting' option, it should be possible to create additional edge detectors and edge triggered interrupts.

Note an 'adcsetup=' should have been included in the code.
 

boriz

Senior Member
Could the comparator(s) be made to self-oscillate? Would the frequency be proportional to Vcc? Would this mean battery levels could easily be accurately metered using COUNT without any external components? Could it also be used to generate a sort of random seed?
 

hippy

Ex-Staff (retired)
Could the comparator(s) be made to self-oscillate?

Probably; the simple way may be to try it.

There are various SFR which can have bits poked which could affect how it actually operates, the "fast ( higher power ) / slow ( lower power )" option in particular would likely affect 'resonant frequency'. A standard RC oscillator would probably work, so 'LDR/NTC to frequency' generation may be possible. Varying the VREF of an RC may create a variable mark-space ratio or change frequency.

COUNT, PULSIN or SETTIMER COUNT could be used to measure frequency or periods if not oscillating too fast otherwise it's grab an oscilloscope - watch out for 'aliasing' where a high frequency may be appear to be a lower frequency.
 

hippy

Ex-Staff (retired)
Self-oscialltion seems to be doing something but have to jump through some hoops to get there, for 20X2 usinge arlier code ...

CompSetup config,ivr

peeksfr $6d,b0
bit5 = 1
bit3 = 0
pokesfr $6d,b0

peeksfr $6b,b0
bit5 = 1
bit3 = 0
pokesfr $6b,b0

peeksfr $6c,b0
bit2 = 1
bit3 = 1
pokesfr $6c,b0

pokesfr $A0,0

Output b.1
Output c.4

do
count c.0, 2000, w0
sertxd( #w0, " ")
loop

Unfortunately it doesn't seem you can read the output pins internally but can wire-link that to another input pin ( to C.0 for COUNT ). Works, but no guarantees of under all circumstances.

Frequency varies with VCC but doesn't seem that stable. +/-50% in the test I used but that was thrown together.
 
Last edited:

LarryGrad

Member
3v leveling using comparators worked fine

The comparator code worked great! I have a 3v GPS and a 3v Polar Heart Rate module. The comparators took care of the leveling for me. I was able to eliminate some components.



; Setup pins in/out
Input C.7 ; This is CadenceIN
Input C.6 ; This is HRIN
Input C.5 ; This is WheelIN

;SETUP THE COMPARATORS
;Comp 1 In is ADC6 leg 14, B.4, this is HR input
;Comp 2 In is ADC5 leg 15, B.3, this is GPS input

' Comp 1 Out is leg 17, B.1 goes back into leg 4, C.6
' Comp 2 Out is leg 6, C.4 goes to serin on FPU


CompSetup %1111001111, 0 ; Set ref voltage to 1.024 internal, invert output

' Route physical comparator output Comp 1 to B.1 (leg17)

Output B.1
PeekSfr $6D, b0
bit5 = 1
PokeSfr $6D, b0

' Route physical comparator output Comp 2 to C.4 (leg6)

Output C.4
PeekSfr $6B, b0
bit5 = 1
PokeSfr $6B, b0
 

hippy

Ex-Staff (retired)
Thanks for the display, and glad you got a good result.

Real thanks should go to Silicon Chip and the original project's author, plus lanternfish for bringing it to our attention. I believe the originating author was Geoff Graham ...

http://www.siliconchip.com.au/cms/A_111803/printArticle.html

In some code it may be better to route the comparator outputs ( the PeekSfr then PokeSfr ) first then set the output pins ( Output B.1, C.4 ). Otherwise the pins may go from 'floating, to zero, to high', rather than 'floating to zero' or 'floating to high'. In many cases this brief zero-glitch may not matter.

For the $6D and $6B registers, bit 3 is a comparator speed/power select, and for $6C, bits 2 and 3 set hysteresis. These aren't fully explained in the 18F14K22 (20X2) datasheet but may be worth investigating.
 

MPep

Senior Member
@Hippy,

Correct that the author is Geoff Graham.

@LF,
I saw your post, then a few days later managed to purchase the magazine.
It is so obvious when it is explained well in the magazine, isn't it.

In a previous design I used a BJT to do level conversion (along with inversion) but changing T to N (or was it the other way around), all worked well.
But when space is at a premium, and you don't mind using a few more pins, then this method is certainly the way to go.

Good of you to point it out to the Forum et al.:D

MPep.
 
Top