Comparator not working properly when external supply is on EasyPIC7

Discussion in 'Embedded Systems and Microcontrollers' started by roxi60, Jan 15, 2019.

  1. roxi60

    Thread Starter Member

    Sep 2, 2018
    44
    1
    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.
     
  2. ericgibbs

    Moderator

    Jan 29, 2010
    8,104
    1,642
    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
     
  3. dl324

    AAC Fanatic!

    Mar 30, 2015
    8,510
    2,047
    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.
     
  4. roxi60

    Thread Starter Member

    Sep 2, 2018
    44
    1
    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 (Microchip Assembler):
    1.  
    2. list P = 16F877A
    3. include P16F877A.inc
    4. __config _HS_OSC & _WDT_OFF & _LVP_OFF & _PWRTE_ON
    5.  
    6. ;* Define variable storage
    7.     CBLOCK 0x70
    8.     w_temp
    9.     status_temp
    10.     count, lc1, lc2
    11.     ENDC
    12.  
    13.     org    0x000        ; startup vector
    14.     ;nop            ; required for ICD operation
    15.     clrf    PCLATH          ; ensure page bits are cleared
    16.       goto    Initialize      ; go to beginning of program
    17.     ORG     0x004           ; interrupt vector location
    18. ; -----------------------------------------------------------------------
    19. ; Interrupt routine
    20. ; -----------------------------------------------------------------------    
    21. ;
    22.         movwf w_temp
    23.         swapf STATUS,W
    24.         bcf   STATUS,RP0
    25.     movwf status_temp
    26.  
    27. ;
    28.         btfsc INTCON, INTF
    29.         goto  Ext_int
    30.  
    31.  
    32. Ext_int            
    33.          banksel PORTB
    34.     bsf PORTB,5
    35.     banksel TRISB
    36.     bcf   INTCON, INTF
    37.         goto  EndInterrupt
    38.      
    39. EndInterrupt
    40.  
    41.         swapf status_temp,W
    42.         movwf STATUS
    43.         swapf w_temp,F
    44.         swapf w_temp,W    
    45.         retfie
    46. ;**********************************************************************
    47. ;* Initialize I/O ports and peripherals
    48. Initialize
    49.     clrf    PORTB
    50.     clrf    PORTA
    51.     banksel TRISA
    52. ; setup I/O
    53.     movlw    B'00111111'
    54.     movwf    TRISA
    55.     movlw    B'00000001'
    56.     movwf    TRISE
    57.     movlw    B'00000011'
    58.     movwf    TRISB
    59.     movlw    B'00000100'
    60.     movwf    CMCON
    61.     movlw    B'00000010'
    62.     movwf    ADCON1
    63.     banksel PORTB
    64.     movlw    B'10010001'
    65.     movwf    ADCON0
    66.     banksel TRISA
    67.     movlw    B'11000011'
    68.     movwf    OPTION_REG
    69.     bsf     INTCON,INTE
    70.     bsf    INTCON,GIE
    71.     bsf    INTCON,PEIE
    72. ; Main control loop
    73. main_loop
    74.     banksel TRISB
    75.         btfsc   CMCON,C1OUT
    76.         goto setCompOFF
    77.     banksel PORTB
    78.     movlw    B'10010001'
    79.     movwf    ADCON0
    80.     movlw   0x01
    81.     call    swait
    82.     bsf ADCON0,GO
    83. Wait
    84.     btfsc ADCON0,GO
    85.     goto Wait
    86.         goto main_loop
    87. setCompOFF
    88.         banksel PORTB
    89.     bcf     PORTB,5
    90.     movlw   0x01
    91.     call    swait
    92. check
    93.     banksel PORTB
    94.     btfss    PORTB,1
    95.     goto stop
    96. stop
    97.     bcf    PORTB,2
    98.     goto main_loop
    99. swait      movwf   lc2
    100. _sw2       movlw   0x20
    101.            movwf   lc1
    102. _sw3       nop
    103.            decfsz  lc1,f
    104.            goto    _sw3
    105.            decfsz  lc2,f
    106.            goto    _sw2
    107.            return
    108.     END                    
    109.  
    110.  
    Moderators note : you can use code=mpasm in the tags to highlight
     
    Last edited by a moderator: Jan 19, 2019
  5. dl324

    AAC Fanatic!

    Mar 30, 2015
    8,510
    2,047
    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?
     
  6. roxi60

    Thread Starter Member

    Sep 2, 2018
    44
    1
    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
     
  7. dl324

    AAC Fanatic!

    Mar 30, 2015
    8,510
    2,047
    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.
     
  8. JohnInTX

    Moderator

    Jun 26, 2012
    3,736
    1,938
    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.
     
    dl324 likes this.
  9. roxi60

    Thread Starter Member

    Sep 2, 2018
    44
    1
    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.
     
  10. jayanthd

    Active Member

    Jul 4, 2015
    863
    72
  11. roxi60

    Thread Starter Member

    Sep 2, 2018
    44
    1
    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.
     
  12. jayanthd

    Active Member

    Jul 4, 2015
    863
    72
    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: Jan 19, 2019
  13. roxi60

    Thread Starter Member

    Sep 2, 2018
    44
    1
    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.
     
  14. jayanthd

    Active Member

    Jul 4, 2015
    863
    72
    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: Jan 19, 2019
  15. roxi60

    Thread Starter Member

    Sep 2, 2018
    44
    1
    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 (Microchip Assembler):
    1.  
    2. list P = 16F877A
    3. include P16F877A.inc
    4. __config _HS_OSC & _WDT_OFF & _LVP_OFF & _PWRTE_ON
    5.  
    6. ;* Define variable storage
    7.     CBLOCK 0x70
    8.     w_temp
    9.     status_temp
    10.     count, lc1, lc2
    11.     ENDC
    12.  
    13.     org    0x000        ; startup vector
    14.     ;nop            ; required for ICD operation
    15.     clrf    PCLATH          ; ensure page bits are cleared
    16.       goto    Initialize      ; go to beginning of program
    17.     ORG     0x004           ; interrupt vector location
    18. ; -----------------------------------------------------------------------
    19. ; Interrupt routine
    20. ; -----------------------------------------------------------------------      
    21. ;
    22.         movwf w_temp
    23.         swapf STATUS,W
    24.         bcf   STATUS,RP0
    25.     movwf status_temp
    26.  
    27. ;
    28.         btfsc INTCON, INTF
    29.         goto  Ext_int
    30.    
    31.  
    32. Ext_int            
    33.          banksel PORTB
    34.     bsf PORTB,5
    35.     banksel TRISB
    36.     bcf   INTCON, INTF
    37.         goto  EndInterrupt
    38.      
    39. EndInterrupt
    40.  
    41.         swapf status_temp,W
    42.         movwf STATUS
    43.         swapf w_temp,F
    44.         swapf w_temp,W      
    45.         retfie  
    46. ;**********************************************************************
    47. ;* Initialize I/O ports and peripherals
    48. Initialize
    49.     clrf    PORTB
    50.     clrf    PORTA
    51.     banksel TRISA
    52. ; setup I/O
    53.     movlw    B'00111111'  
    54.     movwf    TRISA
    55.     movlw    B'00000001'  
    56.     movwf    TRISE
    57.     movlw    B'00000011'  
    58.     movwf    TRISB
    59.     movlw    B'00000100'  
    60.     movwf    CMCON
    61.     ;movlw    B'00000010'  
    62.     ;movwf    ADCON1
    63.     ;banksel PORTB
    64.     ;movlw    B'10010001'  
    65.     ;movwf    ADCON0
    66.     banksel TRISA
    67.     movlw    B'11000011'
    68.     movwf    OPTION_REG
    69.     bsf     INTCON,INTE
    70.     bsf    INTCON,GIE
    71.     bsf    INTCON,PEIE
    72. ; Main control loop
    73. main_loop
    74.     banksel TRISB
    75.         btfsc   CMCON,C1OUT
    76.         goto setCompOFF  
    77.     banksel PORTB
    78.     ;movlw    B'10010001'  
    79.     ;movwf    ADCON0
    80.     movlw   0x01
    81.     call    swait
    82.     ;bsf ADCON0,GO
    83. Wait
    84.     ;btfsc ADCON0,GO
    85.     ;goto Wait
    86.         goto main_loop
    87. setCompOFF
    88.         banksel PORTB
    89.     bcf     PORTB,5
    90.     movlw   0x01
    91.     call    swait
    92. check  
    93.     banksel PORTB
    94.     btfss    PORTB,1
    95.     goto stop
    96. stop
    97.     bcf    PORTB,5
    98.     goto main_loop
    99. swait      movwf   lc2
    100. _sw2       movlw   0x20
    101.            movwf   lc1
    102. _sw3       nop
    103.            decfsz  lc1,f
    104.            goto    _sw3
    105.            decfsz  lc2,f
    106.            goto    _sw2
    107.            return  
    108.     END  
    Moderators note : you can use code=mpasm in the tags to highlight
     
    Last edited by a moderator: Jan 19, 2019
  16. jayanthd

    Active Member

    Jul 4, 2015
    863
    72
    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.
     
    Last edited: Jan 19, 2019
  17. roxi60

    Thread Starter Member

    Sep 2, 2018
    44
    1
    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
     
  18. jayanthd

    Active Member

    Jul 4, 2015
    863
    72
    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.
     
  19. roxi60

    Thread Starter Member

    Sep 2, 2018
    44
    1
    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.
     
  20. roxi60

    Thread Starter Member

    Sep 2, 2018
    44
    1
    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.
     
Loading...