# 16F628 comparator problem !

Status
Not open for further replies.

#### aljamri

##### Member
Hi,
I've tried to test the PIC comparator facility but not succeeded . At the begiging I've used mikroC with PIC12F675 and failed. This made me to go back to the datasheet and tried this time 16F628 and copied exactly the given example in page 55 with only two changes. Firstly I could not make a 10µ Second delay, instead I've put a 'nop' . Secondly I used mode 4 ( Two independent Comparators ) instep of mode 3 (Two common Reference comparators ) and the code is as follows:

Code:
	LIST	p=16F628		;tell assembler what chip we are using
include "P16F628.inc"	            ;include the defaults for the chip
ERRORLEVEL	0,	-302	;suppress bank selection messages
__config 0x3D18			;sets the configuration settings (oscillator type etc.)

cblock 	0x20 			;start of general purpose registers
FLAG_REG
endc

CLRF 	FLAG_REG 	;Init flag register
clrf	FLAG_REG
CLRF 	PORTA 		;Init PORTA
IORWF 	FLAG_REG,F 	;Store bits in flag register
MOVLW  0x04 		;Init comparator mode
MOVWF  CMCON 	;CM<2:0> = 011
BSF 	STATUS,RP0 	;Select Bank1
MOVLW  0x07 		;Initialize data direction
MOVWF TRISA 		;Set RA<2:0> as inputs
;RA<4:3> as outputs
BCF 	STATUS,RP0 	;Select Bank 0
CALL 	DELAY10 	;10µs delay
MOVF 	CMCON,F 	;Read CMCON to end change condition
BCF 	PIR1,CMIF 	;Clear pending interrupts
BSF 	STATUS,RP0 	;Select Bank 1
BSF 	PIE1,CMIE 	;Enable comparator interrupts
BCF 	STATUS,RP0 	;Select Bank 0
BSF 	INTCON,PEIE     ;Enable peripheral interrupts
BSF 	INTCON,GIE 	;Global interrupt enable

DELAY10
nop
end

but regardless the change in Vin+, no any output is observed. is this has any thing to do with FUSE setting as I copied the first 7 lines from NG tutorials. Any problem ? Thanks

Last edited:

#### ericgibbs

##### Well-Known Member
hi aljamri,
This causes the problem to end when you call it..!
DELAY10
nop
end

#### aljamri

##### Member
You mean I need to return with "retlw 0x00"
Thanks Mr. Eric

#### aljamri

##### Member
Sorry, but still it is not working, I think it has some wrong connection, unfortunately, I'm not able to upload the circuit, thou it is very simple ,

*the ouputs RA3 & RA4 ((Pin 2 & 3) are led connected to ground through 470 Resistors,

* Vin- which the reference is 5V divided by 2 1k resistors and connected to RA0 and RA1 through 10 k resistor.

* Vin+ is a potentiometer connected between 5v and Gnd. The mid terminal connected to RA2 (Pin1)

Last edited:

#### ericgibbs

##### Well-Known Member
Sorry, but still it is not working, I think it has some wrong connection, unfortunately, I'm not able to upload the circuit, thou it is very simple ,

*the ouputs RA3 & RA4 ((Pin 2 & 3) are led connected to ground through 470 Resistors,

* Vin- which the reference is 5V divided by 2 1k resistors and connected to RA0 and RA1 through 10 k resistor.

* Vin+ is a potentiometer connected between 5v and Gnd. The mid terminal connected to RA2 (Pin1)

Ok,
I will try your program on Oshonsoft simulator, let you know.
Is it connected like this.?

#### Attachments

• AAesp04.gif
3.6 KB · Views: 229
Last edited:

#### ericgibbs

##### Well-Known Member
hi,
You have not corrected the program, it still runs into a STACK overflow.

BSF INTCON,PEIE ;Enable peripheral interrupts
BSF INTCON,GIE ;Global interrupt enable

What happens when the program gets to here.???

DELAY10
nop
return
end

#### aljamri

##### Member

* YES the drawing is correct except for the two 10k. I've connected one of them to one of the inputs.

The other point, regarding the delay, it was for 10 micro second as per the datasheet. I was not able to make such delay, so I've put "nop" for the smallest possible delay I know.

#### ericgibbs

##### Well-Known Member

* YES the drawing is correct except for the two 10k. I've connected one of them to one of the inputs.

The other point, regarding the delay, it was for 10 micro second as per the datasheet. I was not able to make such delay, so I've put "nop" for the smallest possible delay I know.

hi,
Code:
BSF     INTCON,GIE     ;Global interrupt enable ;
when it executes this line it drops into the DELAY10 , does a NOP and then
BUT the STACK pointer is not set,so the program fails with a STACK error message.

DELAY10
nop
return
end

Last edited:

#### aljamri

##### Member
Hi Mr. Eric,
As I told you, I found this code in the 16F628 datasheet, so I've just cpied it. Frankly speaking this is one of my first attemempts to uderstand the operation of PIC comparators and seems not easy.

Thanks

Last edited:
Status
Not open for further replies.

Replies
56
Views
5K
Replies
0
Views
4K
Replies
24
Views
5K
Replies
31
Views
4K
Replies
12
Views
6K