Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Comparator tutorial

Status
Not open for further replies.

Rusttree

Member
Can someone recommend a good online comparator tutorial for my 16F88? I'm a little confused with all the proper registry settings in the data sheet.

Thanks,
Dan
 
Not many people have used the comparators and so I don't think there are any tutorials. I'm sure someone will now come along and prove me wrong. :rolleyes:

However, the section on the comparators is self explanatory and there is only 1 register involved - 2 if you include the Vref module. Which part is unclear?

Mike.
 
I spent a bit more time on the comparators and the A/D converters, so I think I have a better understanding of what's going on. I've written a piece of code that's entirely an exercise in principle and does nothing useful. However, it doesn't work. I'm hoping someone can point out why.

I have an internally referenced Vref set to 0.4V. I'm connecting an external voltage that ranges from 0 to 0.9V to pin RA0. I'm using Operating Mode '010', so this should send RA3 (C1OUT) high or low based on the input at RA0. Also, just as an exercise, I want RB6 to follow C1OUT. So I AND the CMCON registry with b'01000000' to determine if C1OUT (bit6) is high or low. Then I apply the result to PORTB. That should make RB6 go high. The whole thing loops infinitely so I can watch RA3 and RB6 go high and low as I vary RA0.

As you may have guessed, that didn't work. RA3 seems to be stuck at low and RB6 is stuck at high no matter what the input to RA0 is. Below is the code. Any help is greatly appreciated.

Thanks!
Dan

Code:
	list	p=16F88
	#include	<p16f88.inc>
	__CONFIG	_CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO
	ERRORLEVEL -302
;***************************************************************
;***************************************************************
	org	0x0000
	goto	INIT
	NOP
	NOP
	NOP
	NOP
	org	0x0005
;
INIT   BSF     STATUS,RP0      ;Bank1
       BSF     OSCCON,IRCF1    ;set oscilator
       BSF     OSCCON,IRCF2    ; to 4Mhz
       BSF     TRISA,0         ;RA0 input
       BCF     TRISA,3         ;RA3 output (C1OUT)
       CLRF    TRISB           ;All PortB output
       MOVLW   b'11100010'     
       MOVWF   CVRCON          ;Configure internal Vref
       MOVLW   b'00000010'
       MOVWF   CMCON           ;set internally ref'ed comparators
       BSF     PIE2,CMIE       ;interupt enable
       BCF     STATUS,RP0      ;Bank0
       BSF     INTCON,PEIE     ;interupt enable
       BSF     INTCON,GIE      ;interupt enable

LOOP2  NOP
LOOP1  BTFSS   PIR2,CMIF       ;Wait until compare interupt
       GOTO    LOOP1 
       MOVLW   b'01000000'     
       BSF     STATUS,RP0      ;Bank1
       ANDWF   CMCON,0
       BCF     STATUS,RP0      ;Bank0
       MOVWF   PORTB
       BCF     PIR2,CMIF       ;reset comparator interupt
       GOTO    LOOP2
 
Last edited:
Oh dear Rusttree,

What are you doing with enabling interrupt while there isn't the required interrupt handling routine at 0x0004?
 
Yeap, totally forgot interupt handling. I'm pretty sure I get out of this one free because this is my first assembly program.

Anyway, I fixed it up and now it works :) ..... mostly. RA3 still doesn't follow C1OUT, but RB6 does! From what I read, it seems like all I need to do is set RA3 as digital output on TRISA, but apparently I'm missing something else. Nonetheless, I have the basic comparator working. Here's my new code:

Code:
;***************************************************************
	list	p=16F88
	#include	<p16f88.inc>
	__CONFIG	_CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_ON & _PWRTE_ON & _WDT_OFF & _INTRC_IO
	ERRORLEVEL -302
;***************************************************************
         ORG     0x0000
         GOTO    INIT
         ORG     0x0004
         GOTO    INTERUPT
;
INIT     BSF     STATUS,RP0      ;Bank1
         BSF     OSCCON,IRCF1    ;set oscilator
         BSF     OSCCON,IRCF2    ; to 4Mhz
         BSF     TRISA,0         ;RA0 input
         BCF     TRISA,3         ;RA3 output (C1OUT)
         CLRF    TRISB           ;All PortB output
         MOVLW   b'11100010'     
         MOVWF   CVRCON          ;Configure internal Vref
         MOVLW   b'00000010'
         MOVWF   CMCON           ;Internally ref'ed comparators
         BSF     PIE2,CMIE       ;interupt enable
         BCF     STATUS,RP0      ;Bank0
         BSF     INTCON,PEIE     ;interupt enable
         BSF     INTCON,GIE      ;interupt enable

LOOP     NOP
         GOTO    LOOP

INTERUPT MOVLW   b'01000000'     
         BSF     STATUS,RP0      ;Bank1
         ANDWF   CMCON,0
         BCF     STATUS,RP0      ;Bank0
         MOVWF   PORTB
         BCF     PIR2,CMIF       ;reset comparator interupt
         RETFIE

END
 
gramo said:
just on the f88 comparators and the A/D converters, are they the same as the 16f877, ie, could i run a 877 program on a f88 (using a/d and comparitor settings) with the only loss of the additional ports?

The 88 has 1 less A/D channel and has a new register ANSEL. The pin allocation bits have moved from ADCON1 to ANSEL. So, the code would need (minimal) conversion.

Mike.
 
Your interrupt routine is really 'nasty' as well, it's altering registers without saving and restoring them - in this case perhaps it doesn't matter?, but it's a really bad example to start with - and if you then expand your program it won't work anymore!.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top