0% found this document useful (0 votes)
140 views2 pages

Multi Freq PWM g2452 Example.c

This document describes a MSP430G2452 microcontroller program that generates three independent pulse-width modulated (PWM) frequencies on different output pins using Timer A units and interrupts. Timer A CCR0, CCR1, and CCR2 units are configured to produce approximately 5 kHz, 2.5 kHz, and 1 kHz toggling rates on pins P1.1, P1.2, and P1.4 respectively. An interrupt on the Timer A overflow is used to toggle P1.0 at approximately 8 Hz. The program configures the timer clock source, output pins, duty cycles, and enables interrupts to implement the independent PWM outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views2 pages

Multi Freq PWM g2452 Example.c

This document describes a MSP430G2452 microcontroller program that generates three independent pulse-width modulated (PWM) frequencies on different output pins using Timer A units and interrupts. Timer A CCR0, CCR1, and CCR2 units are configured to produce approximately 5 kHz, 2.5 kHz, and 1 kHz toggling rates on pins P1.1, P1.2, and P1.4 respectively. An interrupt on the Timer A overflow is used to toggle P1.0 at approximately 8 Hz. The program configures the timer clock source, output pins, duty cycles, and enables interrupts to implement the independent PWM outputs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//******************************************************************************

// MSP430G2452 Multiple freq. PWM Demo - Timer_A, Toggle P1.0-2, P1.4


//
// Description: Use Timer_A CCRx units and overflow to generate three
// independent PWM freqs and duty cycles. For demonstration, CCR0 and CCR1 outp
ut
// units are optionally selected with port pins P1.1, P1.2, and P1.4 in toggle
// mode. Interrupts are also enabled with all CCRx units,
// software loads offset to next interval only - as long as the interval offset
// is aded to CCRx, toggle rate is generated in hardware. Timer_A overflow ISR
// is used to toggle P1.0 with software.
//
// ACLK = n/a, MCLK = SMCLK = TACLK = default DCO ~1MHz
// As coded and assuming ~1MHz DCO, toggle rates are:
// P1.1 = CCR0 ~ 1MHz/(50 + 150) = ~5kHz @ 25% duty cycle
// P1.2 = CCR1 ~ 1MHz/(50 + 350) = ~2.5kHz @ 12.5% duty cycle
// P1.4 = CCR2 ~ 1MHz/(600 + 400) = ~1kHz @ 60% duty cycle
//
// P1.0 = overflow ~ 1MHz/(2*65536) = ~8Hz @ 50% duty cycle
//
//
MSP430G2452
//
----------------//
/|\|
XIN|//
| |
|
//
--|RST
XOUT|//
|
|
//
|
P1.1/TA0|--> TACCR0
//
|
P1.2/TA1|--> TACCR1
//
|
P1.4/TA2|--> TACCR2
//
|
P1.0|--> Overflow/software
// http://www.ti.com/mcu/docs/mcuprodtechdoc.tsp?sectionId=95&tabId=1202&family
Id=342&techDoc=1&docCategoryId=1&viewType=mostrecent
//******************************************************************************
#include <msp430g2452.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;

// Stop WDT

//Calibrate DCO for 1MHz operation


BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
P1SEL |= 0x16;
P1SEL2 |= 0x10;
P1DIR |= 0x17;
TACCTL0
TACCTL1
TACCTL2
TACTL =

= OUTMOD_4
= OUTMOD_4
= OUTMOD_4
TASSEL_2 +

// P1.1 - P1.2, P1.4 option select


// P1.4 option select
// P1.0 - P1.2, P1.4 outputs
+ CCIE;
+ CCIE;
+ CCIE;
MC_2 + TAIE;

_BIS_SR(LPM0_bits + GIE);
}
// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0 (void)
{

//
//
//
//

CCR0 toggle, interrupt enabled


CCR1 toggle, interrupt enabled
CCR2 toggle, interrupt enabled
SMCLK, Contmode, int enabled

// Enter LPM0 w/ interrupt

if(TACCTL0 & CCI)


{
TACCR0 += 50;
}
else
{
TACCR0 += 150;
}

// If output currently high


// 25% high

// 75% low

}
// Timer A1 Interrupt Vector (TA0IV) handler
#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer_A1(void)
{
switch( TA0IV )
{
case 2: if(TACCTL1 & CCI)
// If output currently high
{
TACCR1 += 50;
// 12.5% high
}
else
{
TACCR1 += 350;
// 87.5% low
}
break;
case 4: if(TACCTL2 & CCI)
// If output currently high
{
TACCR2 += 600;
// 60% high
}
else
{
TACCR2 += 400;
// 40% low
}
break;
case 10: P1OUT ^= 0x01;
// Timer overflow
break;
default: break;
}
}

You might also like