You are on page 1of 3

The Comparator_A+

Introduction: The MSP430x2xx Family comes with a built in comparator called the Comparator_A+. The comparator compares the analog voltages at the + and - input terminals. If the + terminal is more positive than the - terminal, the comparator output CAOUT is high. The output CAOUT can then be read in software, used to trigger a comparator output, and used to trigger a timer output. The comparator can be switched on or off using the control bit CAON. The comparator should be switched off when not in use to reduce current consumption. When the comparator is switched off, the CAOUT is always low. Features: Inverting and non-inverting terminal input multiplexer Software selectable RC-filter for the comparator output Output provided to Timer_A capture input Software control of the port input buffer Interrupt capability Selectable reference voltage generator Comparator and reference generator can be powered down Input Multiplexer Using the Comparator_A+: The MSP430 has three control registers used to configure the Comparator_A+: CACTL1, CACTL2, and CAPD. Here is the register for CACTL1:

CAEX is the Comparator_A+ Exchange bit and determines the direction of the inverter. When enabled, the comparator exchanges the inputs and inverts the comparator output. This bit does not usually need to be enabled. The CAEX and CAREFx bits control the reference voltage. Depending on whether CAEX is equal to 1 or 0, the CARSEL will assign Vref to one of the inputs. In the case of CAEX = 0: when CARSEL is enabled, Vref will be assigned to the -terminal. And if CARSEL is not enabled, Vref will be assigned to the + terminal. CAREFx is two bits and allows you to set your Vref to an internal or external reference voltage. Leave CAREFx at 00 to use an external reference. Change CAREFx to 01 to correspond to a reference voltage of 0.25*Vcc and to 10 to correspond to a reference voltage of 0.50*Vcc. When CAREFx equals 11, the diode reference will be selected. Enable CAON to turn on the Comparator_A+.

The CAIFG, CAIE, and CAIES bits control the interrupt for the comparator. CAIFG is the interrupt flag, which clears automatically when the interrupt is serviced. CAIE turns on the interrupt when enabled, and CAIES selects whether an interrupt is triggered on a rising edge, disabled, or a falling edge, enabled. Example CACTL1 configuration: CACTL1 = CARSEL + CAIE; // Enable an external Vref on the - pin and // turns on the interrupt. // Turn on Comparator

CACTL1 |= CAON;

You can turn on the comparator after configuring the rest of the comparator. The |= allows you to turn on the comparator without changing the previous assigned values of CACTL1. Here is the register for CACTL1:

The CASHORT bit shorts the terminal - and terminal +. The CAF bit puts the output through an on-chip RC filter to smooth out any rapid oscillations. The CAOUT bit is the output of the comparator, which is read only. For the default configuration of CAEX = 0, the P2CA4 and P2CA0 bits together selects pin on the MSP430 to set as input for terminal +. For the same CAEX configuration, the P2CA3, P2CA2, and P2CA1 bits together selects the pin on the MSP430 to set as the input for terminal -. For CAEX = 1, the P2CA4 and P2CA0 bits sets the input for terminal -, and the P2CA3, P2CA2, and P2CA1 bits sets the input pin for terminal +. [P2CA3 P2CA2 P2CA1] & corresponding pin [P2CA4 P2CA0] & corresponding pin

Example CACTL2 configuration: CACTL2 = P2CA4 + P2CA3 + CAF; // Sets input CA1 as + terminal & input // CA4 as - terminal and filters output.

The 3rd register, CAPD, is responsible for disabling the I/O functions of the pins. Each bit corresponds to an input pin CA0-CA7 and can be individually enabled and disabled. This step is kind of redundant since by configuring a pin to the comparator, that pins buffer is automatically disabled, disabling the I/O function of that pin. Example of using the comparator Interrupt Service Routine: #pragma vector = COMPARATORA_VECTOR __interrupt void COMPA_ISR(void) { if ((CACTL2 & CAOUT)==0x01) { CACTL1 |= CAIES; // value high, so watch for falling edge {Insert code here} // implement some code } else { CACTL1 &= ~CAIES; // value low, so watch for rising edge {Insert code here} // implement some code } } It is useful to use the comparator interrupt with the timer interrupt. Reference: MSP430x2xx Family Guide, Section 21: http://www.ti.com/lit/ug/slau144i/slau144i.pdf Scientific Instruments Using the TI MSP430, online blog: http://mspsci.blogspot.com/2010/12/tutorial-12-making-comparisons.html

You might also like