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.

MSP430F2012 PWM with momentary switch and interrupt function

Other Parts Discussed in Thread: MSP430F2012

Hello all,

I am new to the msp430, and am struggling with a current program. I can output a 5ms x 10ms on P1.2 as desired, would like to use an interrupt function to output a specific  X number cycles (periods). Also I would like to use a momentary switch to start the software ( for hardware implemenation later on). Can you provide me examples or help me with the provided code.

#include <msp430f2012.h>

#include <msp430.h>


int main(void)               //PWM 5ms x 10ms (PWxP) for 1000 cycles
{

  WDTCTL = WDTPW + WDTHOLD;  //Stop WDT
  BCSCTL1 = CALBC1_1MHZ;     // Set range
  DCOCTL = CALDCO_1MHZ;      // SMCLK = DCO = 1MHz
  P1DIR |= 0x0C;             //Set P1.2 and P1.3 to output direction
  P1SEL |= 0x0C;             //Declares TA1 a
  TACTL= MC_1 + TASSEL_2;    //Tells CCR0 to count up with SMCLK
  TACCTL1=OUTMOD_7;          //Sets Set/Reset Mode with TACCR1
  TACCR0=10000-1;            //Set CCR0 to 100 Hz (10ms Period)
  TACCR1=5000-1;             //Sets 50% duty cycle

while(1)
{

}}

Thanks

  • You can set CCIE bit in TACCR0 or TACCR1 to get an interrupt on the falling or rising edge of each cycle.
    Inside the ISR you can count the numbe rof cycles and deactivate the output change by changing the OUTMOD setting to 0 (set th eOUT bit to th eproper value before).

    Note that for CCR0, the ISR will auto-clear the CCIE bit, but for CCR1, you need to manually clear it before exiting the ISR.

    For the activation by switch, you can program a port pin interrupt (with a debouncing algorithm!) that resets the counter and re-enables the OUTMODE to toggle the output.
    The debouncing can be handled by the same ISR that handles the timer cycle interrupt.

    Joshua Hatfield said:
      TACCR1=5000-1;             //Sets 50% duty cycle

    Actually, it's 5000, not 5000-1. The -1 on CCR0 is required because '0' is a step too (10000 in total), but for the PWM output by CCR1, you want an output change on 0 (CCR0+1 )and the next one on 0+5000.

**Attention** This is a public forum