//******************************************************************************
// MSP430G2xx3 Demo - Timer_A, PWM TA1, Up/Down Mode, 32kHz ACLK
//
// Description: This program generates one PWM output on P1.2 using
// Timer_A configured for up/down mode. The value in CCR0, 128, defines the
// PWM period/2 and the value in CCR1 the PWM duty cycle. Using
// 32kHz ACLK as TACLK, the timer period is 7.8ms with a 75% duty cycle on
// P1.2. Normal operating mode is LPM3.
// ACLK = TACLK = LFXT1 = 32768Hz, MCLK = default DCO
// //* External watch crystal installed on XIN XOUT is required for ACLK *//
//
// MSP430G2xx3
// -----------------
// /|\| XIN|-
// | | | 32kHz
// --|RST XOUT|-
// | |
// | P1.2/TA1|--> CCR1 - 75% PWM
//
// D. Dang
// Texas Instruments, Inc
// December 2010
// Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10
//******************************************************************************
#include
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P1DIR |= 0x0C; // P1.2 and P1.3 output
P1SEL |= 0x0C; // P1.2 and P1.3 TA1/2 otions
CCR0 = 128; // PWM Period/2
CCTL1 = OUTMOD_6; // CCR1 toggle/set
CCR1 = 32; // CCR1 PWM duty cycle
TACTL = TASSEL_1 + MC_3; // ACLK, up-down mode
_BIS_SR(LPM3_bits); // Enter LPM3
}
这是TI的官方PWM代码,但看规格书P1.0好象不能使用PWM。
下面这个是以前一个供你参考,对错不知道 。
#include "MSP430G2202.h"
#define PWM_P 1000
#define PWM_S 400
#define PWM_L 750
#define PWM_T 10
#define KEY1 BIT1
#define LED1 BIT2 //P1.2|TA0.1 P2.1=TA1.1
#define KEY2 BIT3
#define LED2 BIT4 //P1.4|TA0.2 P2.4=TA1.2
void POWER_ON(unsigned char);
void POWER_OFF(unsigned char);
void LED_DIM(unsigned char);
void DIM_UP(unsigned char);
void DIM_DOWN(unsigned char);
void Delay(int);
unsigned char POWER_STU1=0;
//unsigned char POWER_STU2=0;
unsigned char COUNT_UP=0;
/////////////////////////
//void POWER_ON(void);
//void POWER_OFF(void);
//void LED_DIM(void);
//void Delay(int);
//void DIM_UP(void);
//void DIM_DOWN(void);
//unsigned char POWER_STU=0;
//unsigned char COUNT_UP=1;
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
DCOCTL|= MOD0+DCO0;//+DCO1;//+DCO2 // Set DCO Frequence select
BCSCTL1|=RSEL2;//+RSEL0;// RSEL3++RSEL1; // Set DCO Resistor
// P1SEL |= LED1+LED2; // P1.6 TA1/2 options
//P1SEL2 |=LED2;
P1OUT &= ~(LED1+LED2);
P1DIR |= (LED1+LED2);// // P1.0 and P1.6 output
CCR0 = PWM_P; // PWM Period
CCTL1 = OUTMOD_7; // CCR1 reset/set
CCTL2 = OUTMOD_7; // CCR2 reset/set
//TACTL = TASSEL_2 + MC_1; // SMCLK, up mode
P1IE |= BIT3; // P1.3 interrupt enabled
P1IES|= BIT3; // P1.3 H/L Hiedge
P1IFG &= ~(BIT3); // P1.3 IFG cleared
_EINT();
_BIS_SR(LPM0_bits); // Enter LPM0
}
/*********** P1 port 中断程序 ***************/
#pragma vector=PORT1_VECTOR
__interrupt void Port_11(void)
{ switch(P1IFG&(KEY1+KEY2))
{
case KEY1:
Delay(20);//延迟,防误判
if((P1IN&KEY1)==0) //再次确认键已经按下;
{ Delay(500);//延时0.5S
if((P1IN & KEY1)!=0)
{
if ((POWER_STU1&LED1)==0)
{
POWER_ON(LED1);
POWER_STU1|=LED1;
}
else
{
POWER_OFF(LED1);
POWER_STU1&=~LED1;
}
}
Delay(1000);
LED_DIM(LED1);
}
P1IFG &=~(KEY1);
break;
case KEY2:
Delay(20);//延迟,防误判
if((P1IN&KEY2)==0) //再次确认键已经按下;
{ Delay(500);//延时0.5S
if((P1IN & KEY2)!=0)
{
if ((POWER_STU1&LED2)==0)
{
POWER_ON(LED2);
POWER_STU1|=LED2;
}
else
{
POWER_OFF(LED2);
POWER_STU1&=~LED2;
}
}
Delay(1000);
LED_DIM(LED2);
}
P1IFG &=~(KEY2);
break;
}
}
//LED渐变调光
void LED_DIM(unsigned char LEDx)
{ unsigned char KEYx;
if (LEDx==LED1)
{KEYx=KEY1;}
else
{
if(LEDx==LED2)
{
KEYx=KEY2;
}
else
{
KEYx=(KEY1+KEY2);
}
}
while(POWER_STU1&&((P1IN&KEYx)==0)) //P1.3为LOW,开始自动调光
{ Delay(10);
if(COUNT_UP)
{
DIM_UP(LEDx);
}
else
{
DIM_DOWN(LEDx);
}
}
P1IFG &=~(KEYx);
}
//POWER ON OFF
void POWER_ON(unsigned char LED_x)
{
POWER_STU1|=LED_x;
P1DIR|=LED_x;
P1SEL|=LED_x;
P2SEL|=(LED_x&LED2);
CCR0 = PWM_P; // PWM Period
CCTL2=CCTL1 = OUTMOD_7;
switch(LED_x)
{
case LED1+LED2:
CCR2=CCR1=5; // CCR1 reset/set
for (; CCR1<700; CCR1+=10)
{
CCR2=CCR1;
Delay(10);
TACTL = TASSEL_2 + MC_1;
}
break;
case LED1:
CCR1=5; // CCR1 reset/set
for (; CCR1<700; CCR1+=10)
{
Delay(10);
TACTL = TASSEL_2 + MC_1;
}
break;
case LED2:
CCR1=5; // CCR1 reset/set
for (; CCR2<700; CCR2+=10)
{
Delay(10);
TACTL = TASSEL_2 + MC_1;
}
break;
}
}
void POWER_OFF(unsigned char LED_y)
{ int i;
if(CCR1>CCR2)
{i=CCR1;}
else{i=CCR2;}
switch(LED_y)
{
case LED1+LED2:
while(i>15)
{
Delay(10);
CCR1-=10;
CCR2-=10;
TACTL = TASSEL_2 + MC_1;
}
break;
//////
case LED1:
while(CCR1>15)
{
Delay(10);
CCR1-=10;
TACTL = TASSEL_2 + MC_1;
}
break;
///////
case LED2:
while(CCR2>15)
{
Delay(10);
CCR2-=10;
TACTL = TASSEL_2 + MC_1;
}
break;
P1OUT&=~(LED_y);
P1DIR&=~(LED_y);
POWER_STU1&=~(LED_y);
}
//
void DIM_UP(unsigned char LED_n)
{
int i;
if(CCR1>CCR2)
{i=CCR1;}
else
{i=CCR2;}
switch (LED_n)
{
case LED1+LED2:
if(i<1000)
{ Delay(10);
CCR1+=10;
CCR2+=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=0;}
break;
///
case LED1:
if(CCR1<1000)
{ Delay(10);
CCR1+=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=0;}
break;
////
case LED2:
if(CCR2<1000)
{ Delay(10);
CCR2+=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=0;}
break;
}
}
//
void DIM_DOWN(unsigned char LED_n)
{
int i;
if(CCR1
{i=CCR1;}
else{i=CCR2;}
switch (LED_n)
{
case LED1+LED2:
if(i>15)
{ Delay(10);
CCR1-=10;
CCR2-=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=1;}
break;
///
case LED1:
if(CCR1>15)
{ Delay(10);
CCR1-=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=1;}
break;
////
case LED2:
if(CCR2>15)
{ Delay(10);
CCR2-=10;
TACTL = TASSEL_2 + MC_1;
}
else{COUNT_UP=1;}
break;
}
//
//Delay function
void Delay(int i){} |