This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Comparator B and Timer A0 interrupts msp430f5529

Other Parts Discussed in Thread: MSP430F5529, ENERGIA

Hi 

I am trying to use the comparator B and timer A0 interrupts on the msp430f5529 .The code is written in Energia and imported into CCS:

#include <msp430.h>
int count =0;
int x=5;
int time1=0;
int time2=0;
void setup()
{ volatile unsigned int i;
Serial.begin(9600);
WDTCTL = WDTPW + WDTHOLD;     // Stop WDT
  P1DIR |= BIT0;                // P1.0/LED output direction
 P4DIR |=BIT7;
// Setup ComparatorB                           
  CBCTL0 |= CBIPEN + CBIPSEL_0; // Enable V+, input channel CB0
  CBCTL1 |= CBPWRMD_0;          // ULTRA LOW power mode
  CBCTL2 |= CBRSEL;             // VREF is applied to -terminal 
  CBCTL2 |= CBRS_3|CBREFL_3;    // R-ladder off; bandgap ref voltage (1.2V)
                                // supplied ref amplifier to get Vcref=2.5V (CBREFL_3)            
  CBCTL3 = BIT0;               // Input Buffer Disable @P6.0/CB0

  __delay_cycles(75);           // delay for the reference to settle
  
  CBINT &= ~(CBIFG | CBIIFG);   // Clear any errant interrupts
  CBINT  |= CBIE;               // Enable CompB Interrupt on rising edge of CBIFG (CBIES=0)

  CBCTL1 |= CBON + CBIES;               // Turn On ComparatorB
  TA0CTL = TASSEL_1| MC_2 | TACLR |TAIE;//Enable Timer interrupt in Continuous mode
  enableWatchDog();
  __bis_SR_register(LPM3_bits|GIE); // Enter LPM3 with inetrrupts enabled

}

// Comp_B ISR - LED Toggle
#pragma vector=COMP_B_VECTOR
__interrupt void Comp_B_ISR (void)
{
  CBCTL1 ^= CBIES;              // Toggles interrupt edge
  CBINT &= ~(CBIFG | CBIIFG);              // Clear Interrupt flag
  P1OUT ^= 0x01;				// Toggle P1.0

 
}
//Timer A1 ISR
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR(void)
{

if( CBOUT ==1)
  { count =1;
	P4OUT ^=BIT7;
	delay(1000);
 

  }
else{P4OUT ^=BIT7;
delay(1000);
	count =0;}
}

void loop()
{enableWatchDog();
	Serial.println("HELLO");
Serial.println(count);
	sleepcount(x);
	

delay(1000);
	__bis_SR_register(GIE);

}
int sleepcount(int y)
{enableWatchDog();
	time1=millis();
	if (count == 1)
		{sleepSeconds(x);
		x=x+1;}

	else
		{delay(x*1000);
		x=x-1;
		if(x==0){x=10;}
		else{}}

	time2=millis();
	Serial.println(time2-time1);
}

I get the following output:

When voltage applied is less than or more 2.5 V  Green and Red led toggle alternatively.(GREEN ON RED OFF and vice versa) .

According to the code I am following once the processor enters the comparator interrupt for V>2.5 V it should toggle RED LED and stay in that state until V<2.5 V

The problem seems to be that the Comparator interrupt somehow resets all of its flags .

Also the Serial print statement gives value 0 for (time2-time1) and if I do not enable the Watch Dog before entering into LPM3 it does not even print "HELLO".

  • What does ‘enableWatchDog()’ do?
    The reason for a cleared ‘CBINT’ can be a WatchDog reboot and so new ‘setup’.
  • What is the purpose of the timer? And of the entire program, for that matter?

    At least one comment is wrong (CBPWRMD_0 is high speed, not low power).

    The comparator output is not debounced, so it will oscillate. (See section 31.2.5 of the User's Guide.)

    Why are there delay() calls in the interrupt handler? Won't the watchdog reset the CPU when the delay is too long?

  • I do agree putting the enableWatchDog() in loop() and sleepcount() was unnecessary. But the Serial.println statement stops working if I remove the enableWatchDog() before the __bis_SR_register(LPM3_bits|GIE); in the setup loop .What could be the reason for that?

    Actually the aim of my program is too vary the frequency at which the comparator checks for the input signal and compares it to reference voltage and hence I tried to put a timer inerrupt in addition to the comparator interrupt .Is there any other way to do so?
  • The WatchDog can have several functions, therefore it is not possible to answer your question not knowing what ‘enableWatchDog()’ does.
  • Clemens Ladisch said:
    What is the purpose of the timer? And of the entire program, for that matter?

    The aim of my program is to vary the frequency at which the comparator compares the input voltage to the reference voltage and hence I tried to put the timer module to use.Further if the voltage is below the reference voltage I wanted to increase the sleep time and if it is above then decrease the delay /sleep time.I am using this program as part of a low power application and hence the variable sleep according to input voltage value.

    Is there any other way to do this without trying to use a timer? 

    Clemens Ladisch said:
    At least one comment is wrong (CBPWRMD_0 is high speed, not low power).

    Sorry about the CBPWRMD_0 .Actually I was testing the effect of different power modes on the functioning of the comparator.

    Clemens Ladisch said:
    Why are there delay() calls in the interrupt handler? Won't the watchdog reset the CPU when the delay is too long?

    I thought maybe the green LED output would require time to stabilise so I introduced a delay. But probably that was unnecessary.

  • prerd said:
    But the Serial.println statement stops working if I remove the enableWatchDog() before the __bis_SR_register(LPM3_bits|GIE)

    Do you understand what LPM3 does? Did you examine which influence it could have on transmitting serial (UART) data?

  • Leo Bosch said:
    The WatchDog can have several functions, therefore it is not possible to answer your question not knowing what ‘enableWatchDog()’ does.

    Sorry, I am not  aware of the multiple functions of the WATCH DOG .But as far as I know enableWatchDog() and dsiableWatchDog() are inbuilt energia functions to enable or disable the WatchDog .I just used them without knowing their functions.

    I also removed the timer interrupt completely.The comparator works  fine without the timer.Is there another way to change the frequency of sampling of comparator ?

    Thanks

  • The comparator does not use a clock and does not have a frequency; it works continuously.

    The low-power mode only reduces its ability to react to faster changes (faster switching requires more power), and increases the propagation delay.

    If you want to disable the comparator for some time, you have to switch it off altogether (and then wait again 2 µs after re-enabling it).

  • I don’t know how Energia initialize the WatchDog, I would remove (comment out) all calls to this and move your line “WDTCTL = WDTPW + WDTHOLD;” immediately to the begin of ‘main’.

    In this case you don’t need the Comparator interrupt, only from the Timer which checks (polls) the Comparator output frequently.
    Create a modifiable timer -> Up-mode with CCR0 as time interval value.

**Attention** This is a public forum