Shop OBEX P1 Docs P2 Docs Learn Events
Problem using comparitor on SX28 — Parallax Forums

Problem using comparitor on SX28

BanditBandit Posts: 12
edited 2007-12-08 06:05 in General Discussion
On an SX28 chip I set the RB.1 at 2.4 volts and RB.2 at 5 volts and expected to get a 5 volt output from RB.0.
The only output from RB.0 was a flickering 1 Volt.
Any idea what I am doing wrong?

http://forums.parallax.com/forums/emoticons/jumpin.gif
jumpin

[noparse][[/noparse]edit] I added a subject to your post... Bean


Post Edited By Moderator (Bean (Hitt Consulting)) : 12/4/2007 3:56:33 AM GMT

Comments

  • BeanBean Posts: 8,129
    edited 2007-12-04 03:53
    Have you enabled the comparitor ?
    Please post your code.

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com

    ·
  • BanditBandit Posts: 12
    edited 2007-12-04 13:43
    Here's the code; thanks for the reply!

    ' File Timer_RevC.SXB
    ' Purpose multi use timer
    ' November 25, 2007
    '*******************************************************************************************************************
    DEVICE SX28,OSCXT2, TURBO, STACKX, OPTIONX, BOR26 ' OSCHS3 HIGH SPEED OSCILLATOR BOR26 Brown out reset
    'IRC_CAL IRC_SLOW ' TURBO RUNS 4X FASTER THAN COMPATIBILITY MODE.
    FREQ 20_000_000 ' IRC_CAL calibrates the internal oscillator. SETS THE INTERNAL OSCILLATOR TO IT'S SLOWEST VALUE
    ID "TIMER" ' SETS THE STARTING POINT OF THE PROGRAM FOR THE ASSEMBLER
    ' 50 kHz
    '
    Pin assignments

    Go PIN RA.1
    CLEAR PIN RA.0
    AtFinish PIN RB.0 ' Comparator output bit
    DigCtrl PIN RB
    Segments PIN RC OUTPUT

    '
    ' CONSTANTS
    '
    Yes CON 1 ' for active-high inputs
    No CON 0

    M_STOP CON 2 ' clock stopped
    M_Run CON 1 ' clock running
    M_Clear CON 0 ' clock clear and stopped

    Blank CON %00000000 ' all segments off
    DPoint CON %10000000 ' DP after sec display
    DPDigit CON 3 ' DP after sec display

    NumDigits CON 5 ' five digit display

    '
    Variables
    ops VAR BYTE ' operational mode

    ms VAR Byte 'milliseconds digit
    hs VAR Byte 'hundredths digit
    ts VAR Byte 'tenths digit
    sec01 VAR Byte 'ones digit
    sec10 VAR Byte 'tens digit

    digPntr VAR Byte ' digital pointer
    display VAR Byte(NumDigits) ' cuttent display segments

    ' THE COMPARE STATEMENT SETS RB.1 AND RB.2 AS INPUTS AND SETS RB.0 AND _PARAM1 TO 0 IF RB.1 > RB.2.
    ' So if the opt isolater is blocked RB.1 > RB.2. The LED lights and the timer stops due to _PARAM1 going high and
    ' setting to G0 = M_Stop
    ' Start interrupt routine no code before this routine

    INTERRUPT 1000 'Rate 1000 ' CHECK ISR EVERY MILLISECOND

    GOTO INT_HANDLER

    '****************************************************************
    PROGRAM Start
    '****************************************************************
    '
    ' Subroutine Declarations
    '
    UPDATE_DISPLAY SUB 0 ' Convert digits to segments
    '
    ' Program Code
    '
    Start:
    TRIS_B = %00000111 'CATHODE PINS SET TO OUTPUT Register B; Rb.0 Rb.1 Rb.2 are inputs Rb.3 thru Rb.7 are outputs
    PLP_A = %00000011 ' pull up the unused pins on RA. Ra.0 & Ra.1 the weak pull up resistor is disable.

    COMPARE 1, __PARAM1 ' STARTS THE COMPARATOR IN MODE 1; MONITORS rb.1 AND RB.2 OUTPUTS TO RB.0 to see if the optp isolator is blocked
    ' adjust Potentiometer to fine tune comparison. Opto isolator is = 5 volts __Param1 = 0 "two underscores before Param1.
    MAIN:
    DO 'ops sets the three mode of operation 0) stopped and cleared 1)running
    ' 2) stopped; ops is set in the interrupt routine
    UPDATE_DISPLAY ' refresh segment array
    IF Go = Yes THEN ' go button pressed
    IF ops = M_CLEAR THEN ' were we cleared
    ops = M_RUN ' yep so we can run
    ENDIF
    ENDIF
    IF Clear = Yes THEN ' clear button pressed
    IF ops = M_STOP THEN
    PUT @ms, 0, 0, 0, 0, 0
    ops = M_Clear ' clear the clock
    ENDIF
    ENDIF
    LOOP
    '
    ' Subroutine Code
    '

    INT_HANDLER:
    IF ops <> M_RUN THEN GOTO Next_Digit 'skip update if stopped ---OPS HOLDS THE CURRENT THREE STATUS OF TIMER -
    ' STOPPED AND CLEAR, RUNNING AND STOPPED
    Update_Clock:
    INC ms ' increment millisecond display
    IF ms = 10 THEN ' CHECKS TO SEE IF WE NEED TO WRAP BACK TO ZERO
    ms = 0
    INC hs ' increment hundredth display
    IF hs = 10 THEN
    hs = 0
    INC ts ' increment tenths display
    IF ts = 10 THEN
    ts = 0
    INC sec01 ' increment seconds digit
    IF sec01 = 10 THEN
    sec01 = 0
    INC sec01 ' increment tens digit
    IF sec01 = 6 THEN 'maximum time
    ops = M_STOP ' stops timer
    ENDIF
    ENDIF
    ENDIF
    ENDIF
    ENDIF

    Next_Digit: ' Defines which LED is being cycled
    INC digPntr ' point to next digit
    IF digPntr = NumDigits Then ' check pointer --- number of LED (5), then resets DigPntr
    digPntr = 0 ' wrap if needed
    ENDIF

    Update_Seqs: ' one display updated at a time
    Segments = %00000000 ' clears the LED for next digit creates a crisper display.
    READ Dig_Map + digPntr, DigCtrl ' update digital control - DP_Map is a table with one value READ COMMAND gets data from table offsets it digPntr and puts it in DigCtrl
    Segments = display(digPntr) ' output new segments - Controls the cathode

    Check_Finish:
    IF AtFinish = No THEN ' CHECKS TO SEE IF OPTO-TRANSISTOR IS BLOCKED; NO = 1 At_Finish = _PARMA1 = RB.0
    ops = M_STOP ' MUST PUSH THE CLEAR BUTTON TO RESET: M_STOP = 2
    ENDIF

    RETURNINT ' reset the RTCC value and re-enables interrupts
    ' End interrupt routine start program code
    ' Clear and Start go directly to Ra.0 and Ra.1. Ra.1 going high starts the timer, but the timer must be in mode zero defined as RAO_CLEAR.
    ' The timer starts by updating ops to M-Run. The timer stops when the opto-transistor goes high causing the comparator to go high the timer
    ' will be stop by changing the ops mode to M_STOP

    UPDATE_DISPLAY: ' called only once Read (Table + location) not an addtion, Variable or word so display should be Rc
    '
    READ Seg_Map + ms, display(0) ' display (0) is the LED for milliseconds
    READ Seg_Map + hs, display(1) ' Transfers seg_Map data table into display array.
    READ Seg_Map + ts, display(2) '
    READ DP_Map + sec01, display(3)
    IF sec01 = 0 THEN
    display(4) = %00000000 ' Blanks ms display
    ELSE
    READ Seg_Map + sec01, display(4)
    ENDIF

    RETURN

    '====================================================================================
    ' Data tables
    '====================================================================================

    Seg_Map: ' segments maps without DP
    ' .gfedcba
    DATA %00111111 ' 0
    DATA %00000110 ' 1
    DATA %01011011 ' 2
    DATA %01001111 ' 3
    DATA %01100110 ' 4
    DATA %01101101 ' 5
    DATA %01111101 ' 6
    DATA %00000111 ' 7
    DATA %01111111 ' 8
    DATA %01100111 ' 9

    DATA %01110111 ' A
    DATA %01111100 ' B
    DATA %00111001 ' C
    DATA %01011110 ' D
    DATA %01111001 ' E
    DATA %01110001 ' F

    DP_Map: ' segments maps with DP
    sets the cathode
    ' .gfedcba
    DATA %10111111 '0.
    DATA %10000110 '1.
    DATA %11011011 '2.
    DATA %11001111 '3.
    DATA %11100110 '4.
    DATA %11101101 '5.
    DATA %11111101 '6.
    DATA %10000111 '7.
    DATA %11111111 '8.
    DATA %11100111 '9.

    Dig_Map: ' Digit select map
    DATA %11110000
    DATA %11101000
    DATA %11011000
    DATA %10111000
    DATA %01111000
  • BeanBean Posts: 8,129
    edited 2007-12-04 14:03
    You need to set RB.0 as an OUTPUT.

    · TRIS_B = %00000111

    Should be:
    · TRIS_B = %0000110

    Also what is the purpose of using __PARAM1 in the "COMPARE 1, __PARAM1" line ?

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com

    ·
  • BanditBandit Posts: 12
    edited 2007-12-05 05:21
    Thanks again - this is the second time you've helped me out.


    AtFinish = RB.0
    The AtFinish when set high will stop a timer; this is a Nuts and Volts program from Parallax for a pine derby timer.

    The high output will light an LED indicating the end of the race.
    RB.1 and RB.2 differential input is controlled via and IR emitter and detector.

    This is my first project so I am treking on unfamiliar ground.
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-12-05 15:22
    Hmmm..... I wrote that program and never had any problem with it. My understanding is that the comparator output bit is written directly to the RB.0 pin, regardless of the state of the TRIS register, and the the demo program for COMPARE (in the help file) seems to bear this out. When mode 1 is used (comparator output enabled) the LED lights, even though RB.0 was not specifically set into output mode.

    For example, this still causes the LED to light:

    INPUT RB.0
    DO
      COMPARE 1, result 
    LOOP
    


    result is a bit variable, and RB.1 and RB.2 are tied to a potentiometers.

    Post Edited (JonnyMac) : 12/5/2007 3:29:03 PM GMT
  • BeanBean Posts: 8,129
    edited 2007-12-05 15:56
    Hmmm, well then maybe that wasn't the problem.

    Jon,
    Do you know do RB.1 and RB.2 become inputs automatically too (even if you specify them as outputs) ?


    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com

    ·
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-12-05 17:12
    You know, the docs (original Ubicom and current Parallax) say to make RB.0 an output, and RB.1 and RB.2 inputs; somewhere, though, I read that the comparator output is connected directly to the RB.0 pin and doesn't rely on the TRIS register (I wish I could remember where I read that).

    For grins, I just did another test like this:

    Start:
      COMPARE 1, result
      INPUT RB.0
    
    Main:
      GOTO Main
    


    And the LED still lights! -- so that continues to suggest that the comparator output is connected directly to the port pin. My concern is what happens when RB.0 is set to output low and the comparator bit is high -- is there a conflict? I can't imagine the designers would allow this.
  • BeanBean Posts: 8,129
    edited 2007-12-07 16:09
    I just did an experiment.

    When you enable the comparitor RB.0 is always an output and RB.1 & RB.2 are alway inputs. Even if you try to change them AFTER the comparitor is enable they don't change.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.iElectronicDesigns.com

    ·
  • JonnyMacJonnyMac Posts: 9,216
    edited 2007-12-07 16:25
    That makes sense of what I'm seeing. Thanks, Terry.
  • James NewtonJames Newton Posts: 329
    edited 2007-12-08 00:45
    I've added that to:

    http://www.sxlist.com/techref/ubicom/analogcomparator.htm

    along with a lot of other findings from other peoples tests...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • Tracy AllenTracy Allen Posts: 6,666
    edited 2007-12-08 06:05
    Interesting. How about CMP_OE?...

    When the comparator is enabled (bit 7 of CMP_B=0), AND comparator output is enabled (bit 6 of CMP_B=0), then RB.0 is the comparator output and overrides TRIS and the RB.0 states. But when CMP_OE=1, then RB.0 and !RB.0 manage that pin.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
Sign In or Register to comment.