Jump to content
43oh

Comparator Use


Recommended Posts

Hey all, I am still new to MSP430 and Energia so please bear with me. We have been comparing the responses of comparators for use in projects that will incorporate the MSP430 and we wanted to test the built-in comparator for our MSP430G2553. Is there any way to access and use the comparator to compare two signals within Energia? Any help would be greatly appreciated!

Link to post
Share on other sites

Any thoughts on this? A little more clarification. For now, our goal is simply to use the comparator to compare an input signal to a voltage reference and then output the result so that we can analyze this output. My main issue is determining how to select comparator use within Energia.

Link to post
Share on other sites

Not sure if Energia has any libraries or functions for doing that, so the devs would have to chime in. You should be able to monkey around with enabling and configuring the comparator the old fashioned way, i.e. toggling the special-function-registers for it... the MSP430 x2xx User's Guide has all those details-- http://www.ti.com/lit/ug/slau144i/slau144i.pdf

 

Energia (and Arduino, the AVR-oriented IDE it's derived from) ideally abstracts away a lot of those details, but maybe that feature just isn't written yet.

Link to post
Share on other sites

This should help you to get going.

 

void setup() 
{
 pinMode(RED_LED, OUTPUT);
 /* Internal reference (comp-)0.25 * VCC and turn on */
 CACTL1 = CARSEL + CAREF0 + CAON;

 /* Select P1.3/CA3/S2 as the compare input (comp+).
  * This selection is not obvious from register bit definitions. 
  * The Comparator_A+ Block Diagram  in sectotion Comparator_A+ Introduction
  * is a better reference. */
 CACTL2 = P2CA2 | P2CA1;                           
}

void loop() 
{
 /* Pressing S2 will turn on the LED1 */
 if ((CACTL2 & CAOUT))
   digitalWrite(RED_LED, HIGH);
 else
   digitalWrite(RED_LED, LOW);
}

Link to post
Share on other sites
  • 1 month later...

While the example works, I had some problems with it: when you push the button it goes to GND, so the comparator should give a 0, but it actually gives an 1 with the button pressed. I thought maybe the reference is sent to V+ and the button to V-, so I studied the diagram to reverse the assignment and I don't understand why your example works: according to the block diagram, you are sending both the reference value and the input pin to V- while V+ is left floating.

In the schematic, with CARSEL=1 and CAEX=0 the reference is sent to V-, and P2CA1, P2CA2, P2CA3 also select the input to feed to V-.

In theory V+ is selected by P2CA0 and P2CA4, though there's no combination to select P1_3.

I tried leaving out CARSEL, so the reference is fed to V+ and the button to V- and it also works (and this time I understand why).

Link to post
Share on other sites

Good catch! Below id the fixed code. Please not that I enable the internal pull-up on P1.3 since most of the LP's don't have R34 not populated.

 

void setup() 
{
 pinMode(RED_LED, OUTPUT);
 /* Internal reference (comp-)0.25 * VCC and turn on */
 CACTL1 = CAREF0 + CAON;

 pinMode(PUSH2, INPUT_PULLUP);

 /* Select P1.3/CA3/S2 as the compare input (comp+).
  * This selection is not obvious from register bit definitions. 
  * The Comparator_A+ Block Diagram  in section Comparator_A+ Introduction
  * is a better reference. */
 CACTL2 = P2CA2 | P2CA1;                           
}

void loop() 
{
 /* Pressing S2 will turn on the LED1 */
 if ((CACTL2 & CAOUT))
   digitalWrite(RED_LED, HIGH);
 else
   digitalWrite(RED_LED, LOW);
}

 

Schematic:

 

   VCC (3.3v)
    |
    /                     |\
47k  \      +----          | \
    /      |  0.25*VCC    |  \
    |      |  .83v Comp + |   \
    |      |              |    \ _____ CAOUT
    |      |              |    /
    |______|  P1.3 Comp - |   /
    |      |              |  /
    |      +----          | /
  \                       |/
   \  
    |
    |
   VSS

Link to post
Share on other sites
  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...