Comparator not working properly when external supply is on EasyPIC7

Thread Starter

roxi60

Joined Sep 2, 2018
73
Hello.
I' using PIC 16F877A on EasyPIC7.

My code tells the chip to switch on RB5 when the pulse start and to switch it off when voltage on RA3 is greater then voltage on RA0 (using comparator module)
When I supply the easyPIC board with usb (the same I use to program) everyting goes well. It works properly.

When I use an external power supply to the board, 12 V DC, RB5 does not switch off anymore when voltage on RA3 overcomes voltage on RA0.

The only thing I see is that with USB supply the voltage on the board , and on the chip, is 5,14 V and with external supply to the board, the voltage is 5,01 V.

Any suggestion to explain this strange (for me) behaviour will be appreciated.
Thanks a lot in advance.
 

ericgibbs

Joined Jan 29, 2010
18,879
hi roxi,
What is the voltage range on RA0 that you have observed and programmed to detect.?
I assume the 0V lines are connected to the two units.
E
 

dl324

Joined Mar 30, 2015
16,943
Post a schematic so we can see how things are powered and what you mean by RA3 overcomes RA0, etc. I assume these are I/O's.
 

Thread Starter

roxi60

Joined Sep 2, 2018
73
Thank you Dennis, Eric, for your kind attention.
Enclosed the circuit oscillo and code (sorry if it is not optimum but I modified a sample...)
The +5V to pot are from the board.
High power,clock source and board power are from same net,all ground are the same.
PIC is on the Easypic board.

Code:
list P = 16F877A
include P16F877A.inc
__config _HS_OSC & _WDT_OFF & _LVP_OFF & _PWRTE_ON

;* Define variable storage
    CBLOCK 0x70
    w_temp
    status_temp
    count, lc1, lc2
    ENDC

    org    0x000        ; startup vector
    ;nop            ; required for ICD operation
    clrf    PCLATH          ; ensure page bits are cleared
      goto    Initialize      ; go to beginning of program
    ORG     0x004           ; interrupt vector location
; -----------------------------------------------------------------------
; Interrupt routine
; -----------------------------------------------------------------------     
;
        movwf w_temp
        swapf STATUS,W
        bcf   STATUS,RP0
    movwf status_temp
 
;
        btfsc INTCON, INTF
        goto  Ext_int
  
 
Ext_int            
         banksel PORTB
    bsf PORTB,5
    banksel TRISB
    bcf   INTCON, INTF
        goto  EndInterrupt
     
EndInterrupt

        swapf status_temp,W
        movwf STATUS
        swapf w_temp,F
        swapf w_temp,W     
        retfie 
;**********************************************************************
;* Initialize I/O ports and peripherals
Initialize
    clrf    PORTB
    clrf    PORTA
    banksel TRISA
; setup I/O
    movlw    B'00111111' 
    movwf    TRISA
    movlw    B'00000001' 
    movwf    TRISE
    movlw    B'00000011' 
    movwf    TRISB
    movlw    B'00000100' 
    movwf    CMCON
    movlw    B'00000010' 
    movwf    ADCON1
    banksel PORTB
    movlw    B'10010001' 
    movwf    ADCON0
    banksel TRISA
    movlw    B'11000011'
    movwf    OPTION_REG
    bsf     INTCON,INTE
    bsf    INTCON,GIE
    bsf    INTCON,PEIE
; Main control loop
main_loop
    banksel TRISB
        btfsc   CMCON,C1OUT
        goto setCompOFF 
    banksel PORTB
    movlw    B'10010001' 
    movwf    ADCON0
    movlw   0x01
    call    swait
    bsf ADCON0,GO
Wait
    btfsc ADCON0,GO
    goto Wait
        goto main_loop
setCompOFF
        banksel PORTB
    bcf     PORTB,5
    movlw   0x01
    call    swait
check 
    banksel PORTB
    btfss    PORTB,1
    goto stop
stop
    bcf    PORTB,2
    goto main_loop
swait      movwf   lc2
_sw2       movlw   0x20
           movwf   lc1
_sw3       nop
           decfsz  lc1,f
           goto    _sw3
           decfsz  lc2,f
           goto    _sw2
           return 
    END
Moderators note : you can use code=mpasm in the tags to highlight
 

Attachments

Last edited by a moderator:

dl324

Joined Mar 30, 2015
16,943
Waveforms from the .doc file:
upload_2019-1-15_14-24-48.png

Schematic from the PDF (scope connections were clipped):
upload_2019-1-15_14-27-23.png

What do the power connections look like?
 

Thread Starter

roxi60

Joined Sep 2, 2018
73
For the scope :
Yellow is clock
Blu is voltage on shunt
red is RB5

Power connection are the usual for easyPIC board (i.e.usb connected to PC (that works), or, external, AC DC adaptor 12 V 500 mA)

For the power to load, the 80V from a transformer and bridge+capacitor

Thank you
 

dl324

Joined Mar 30, 2015
16,943
Power connection are the usual for easyPIC board (i.e.usb connected to PC (that works), or, external, AC DC adaptor 12 V 500 mA)
For those of us who know nothing about easyPIC. Does the USB connection power the PIC directly, or is it operating at a voltage lower than 5V? Does 12V input go through a regulator, or is the PIC able to operate from 5V or 12V directly?

Since you say it works with 5V and doesn't with 12V, the problem has to be related to the power source.
 

JohnInTX

Joined Jun 26, 2012
4,787
Do you have a common ground between the EasyPIC7 and the +60V supply? The USB supply through the PC will be referenced to Earth and likely the digital ground but the +12V supply is floating with respect to Earth and the PC / digital ground. Maybe you don't have a common reference. Try a ground wire from the PC to the GND on the EasyPIC7 when using the 12V supply.
 

Thread Starter

roxi60

Joined Sep 2, 2018
73
Thanks a lot Dennis, John!
As you said, the problem is on power and ground.
I connected the ground of the easy board to the ground of the clock source (and gnd of the power +60 V) and it works also with external power on the board.

But now, to have on the shunt (R22), and on peak of the oscilloscope curve, a voltage drop 0.55 V (means 5.5 A), I have to set the pot to about 1 V : that's strange to me.
That's happen using both USB power or external power to the board.
This mismatching seems related to my action to put grounds together, because, if they are not, when pot is 0.55V I have a voltage drop peak on shunt correct: 0.55V (good compared).

I do not know why.

However, I have to thank you very much for your kind help.
 

Thread Starter

roxi60

Joined Sep 2, 2018
73
Thank you jayanthd.

Enclosed an update sketch that shows power sources (all grounded together) and the strange behaviour on oscillo2:
as I told in my previous message, the rising of RA3 does not end at value I set on POT1, like it should be.
I found out that the reason of this is that the RA0 value (the reference for the comparator) drops down during rising of RA3. It seems that RA3 rising affects reference RA0 and so comparison is not good (or better comparison is good for the degradated RA0, but not for the POT setting)
I do not know way and how to solve.

Thank you for any suggestion/explanation.
 

Attachments

jayanthd

Joined Jul 4, 2015
945
With CMCON = 0x04, pins RA0, RA1, RA2 and RA3 will be used for Comparators. You have to move your ADC input pin to some other pin link AN4 and try.

Where is CVREF (Comparator Reference) provided ? Where is CVRCON register configuration ?

First of all why use Comparator to compare two voltages ? Is it a requirement ? Why can't you use 2x ADC channels to get the two voltages and compare them ?

For USB power are you supplying from PC (Computer) ? If yes, then your Computers SMPS might not be good. When my PC's USB port is connected to EasyPIC v7 I get 5.01V on EasyPIC v7.

Post your Proteus file. What does your circuit do ?
 
Last edited:

Thread Starter

roxi60

Joined Sep 2, 2018
73
Enclosed the Proteus file.
Proteus simulation runs good.
I think it is a problem of my hardware.
If I use USB power, I have 5,15 V, if I use External power I have 5,01V on board.
But in both cases, the problem arises when I connect the ground of the board to the ground of the 60V power.
If I use USB power and not this ground connection, everythings works as expected.
If I use External power with this ground connection, that is required here otherwise it does not work, I have the problem.

Thank you so much for your attention.
 

Attachments

jayanthd

Joined Jul 4, 2015
945
In your code ADCON0 and ADCON1 values are wrong.

ADFM (right justified) bit is not set.

What Fosc are you using ? 8MHz crystal on EasyPIC v7 ?

Why do you want to read ADC if you are going to compare two voltages input to comparator. You can just check the C1OUT_bit and decide the state of RB5. Why are you reading RA0 using ADC ?

If you are reading ADC then usual way is to get right justified result but in your code ADFM bit is 0 and so ADC result is left justified.
 
Last edited:

Thread Starter

roxi60

Joined Sep 2, 2018
73
Ok, you're right, I removed all unnecessary ADC (I modified a previous program, that's why I had those residuals).

But the problem is still the same. If I connect the GND of the board with the GND of the +60V power, on the pot I set 0.8V but RB5 stops at about 0.6V due to the drop of RA0.
Quarz is 20 MHz, as you can see in the simulation properties of the PIC.

I give it up, I will try other solutions.

Thank you very much, anyway.


Code:
list P = 16F877A
include P16F877A.inc
__config _HS_OSC & _WDT_OFF & _LVP_OFF & _PWRTE_ON

;* Define variable storage
    CBLOCK 0x70
    w_temp
    status_temp
    count, lc1, lc2
    ENDC

    org    0x000        ; startup vector
    ;nop            ; required for ICD operation
    clrf    PCLATH          ; ensure page bits are cleared
      goto    Initialize      ; go to beginning of program
    ORG     0x004           ; interrupt vector location
; -----------------------------------------------------------------------
; Interrupt routine
; -----------------------------------------------------------------------      
;
        movwf w_temp
        swapf STATUS,W
        bcf   STATUS,RP0
    movwf status_temp
  
;
        btfsc INTCON, INTF
        goto  Ext_int
   
  
Ext_int             
         banksel PORTB
    bsf PORTB,5
    banksel TRISB
    bcf   INTCON, INTF
        goto  EndInterrupt
      
EndInterrupt

        swapf status_temp,W
        movwf STATUS
        swapf w_temp,F
        swapf w_temp,W      
        retfie  
;**********************************************************************
;* Initialize I/O ports and peripherals
Initialize
    clrf    PORTB
    clrf    PORTA
    banksel TRISA
; setup I/O
    movlw    B'00111111'  
    movwf    TRISA
    movlw    B'00000001'  
    movwf    TRISE
    movlw    B'00000011'  
    movwf    TRISB
    movlw    B'00000100'  
    movwf    CMCON
    ;movlw    B'00000010'  
    ;movwf    ADCON1
    ;banksel PORTB
    ;movlw    B'10010001'  
    ;movwf    ADCON0
    banksel TRISA
    movlw    B'11000011'
    movwf    OPTION_REG
    bsf     INTCON,INTE
    bsf    INTCON,GIE
    bsf    INTCON,PEIE
; Main control loop
main_loop
    banksel TRISB
        btfsc   CMCON,C1OUT
        goto setCompOFF  
    banksel PORTB
    ;movlw    B'10010001'  
    ;movwf    ADCON0
    movlw   0x01
    call    swait
    ;bsf ADCON0,GO
Wait
    ;btfsc ADCON0,GO
    ;goto Wait
        goto main_loop
setCompOFF
        banksel PORTB
    bcf     PORTB,5 
    movlw   0x01
    call    swait
check  
    banksel PORTB
    btfss    PORTB,1
    goto stop
stop
    bcf    PORTB,5
    goto main_loop
swait      movwf   lc2
_sw2       movlw   0x20
           movwf   lc1
_sw3       nop
           decfsz  lc1,f
           goto    _sw3
           decfsz  lc2,f
           goto    _sw2
           return  
    END
Moderators note : you can use code=mpasm in the tags to highlight
 
Last edited by a moderator:

jayanthd

Joined Jul 4, 2015
945
Add a 5.1V 500mW zener circuit across VDD and VSS pins of PIC and let me know what happens.

What is the range of voltage applied to RA0 and RA3 pins ? If the voltages doesn't exceed 4.7V then use 4.7V zener and try.

Can you test this .hex file on hardware ? It is written using mikroC PRO PIC.

There should be a common ground between your 60V power supply and EasyPIC v7 ground. This is a requirement.

Show circuit of your 60V power supply.
 

Attachments

Last edited:

Thread Starter

roxi60

Joined Sep 2, 2018
73
Range on RA0 and RA3 is lower than 1.5 V.
I tried with 4.7 and 5.1 zener with no results.
I tried your code but there's no signal on RB5 at pulse begin and so no voltage at all on shunt.
The circuit of 60 V is what I put in my message #11: simple transformer followed by bridge and electrolitic 1000 uF 63V.

Thank you
 

jayanthd

Joined Jul 4, 2015
945
Then you have to use 2x 30V zener in series with current limiting resistor to get 60V exact for your circuit. The voltage input to RA3 depends upon this 60V. If mains voltage fluctuates then 60V maybe 60.5 or 62V. You need to regulate it to 60V fixed using zeners. Try this and reply.
 

Thread Starter

roxi60

Joined Sep 2, 2018
73
I do not have such a zener.
But the problem is not RA3 but RA0: when RA3 goes up, RA0 goes down, and it should not.
I found that the drop in RA0 is related to drop in VDD (+5V), .
I tried with capacitors to keep VDD constant but no success.
So I will work with the GND board not wire connected to the +60V GND.
When the grounds are connected only via USB and PC, the system works well, no drop on VDD and no drop on RA0.
And I will not work with external voltage for the moment, because in that case I need the wire connection, since I do not have the PC connection to board.

Thank you.
 

Thread Starter

roxi60

Joined Sep 2, 2018
73
Hello Jayanthd.
I found out that during the rising of current (and voltage on shunt) there's a drop in gnd value.
So, if you take into account this (see enclosure) the voltage on RA3 drops really when it is equal to value set on pot (RA0).

Remains to me to understand why I have this drop in gnd, if it is due to inductor, to mosfet, or what else cause.
For the moment I do not know.
 

Attachments

Top