救命啊!!!msp5529 pwm函数 无法输出pwm波形!请各位大侠帮我看看啊。。。
#include "msp430f5529.h"
#define TA01_SET P1SEL |= BIT2; P1DIR |= BIT2 //P1.2
#define TA02_SET P1SEL |= BIT3; P1DIR |= BIT3 //P1.3
#define TA03_SET P1SEL |= BIT4; P1DIR |= BIT4 //P1.4
#define TA04_SET P1SEL |= BIT5; P1DIR |= BIT5 //P1.5
#define TA01_OFF P1SEL&= ~BIT2 //关P1.2
#define TA02_OFF P1SEL &= ~BIT3 //P1.3
#define TA03_OFF P1SEL &= ~BIT4 //P1.4
#define TA04_OFF P1SEL &= ~BIT5 //P1.5
char TA0_PWM_Init(char Clk,char Div,char Mode1,char Mode2,char Mode3,char Mode4)
{
TA0CTL =TACLR;
TA0CTL |=MC_1;
switch(Clk) //时钟选择 //为定时器TA选择时钟源
{
case 'A': case 'a': TA0CTL|=TASSEL_1; break; //ACLK
case 'S': case 's': TA0CTL|=TASSEL_2; break; //SMCLK
case 'E': TA0CTL|=TASSEL_0; break; //外部输入(TACLK)
case 'e': TA0CTL|=TASSEL_3; break; //外部输入(TACLK取反)
default : return(0); //设置参数有误,返回0
}
switch(Div) //时钟分频 //为定时器TA选择分频系数
{
case 1: TA0CTL|=ID_0; break; //1
case 2: TA0CTL|=ID_1; break; //2
case 4: TA0CTL|=ID_2; break; //4
case 8: TA0CTL|=ID_3; break; //8
default : return(0); //设置参数有误,返回0
}
switch(Mode1) //设置PWM通道1的输出模式。
{
case 'F': case 'f':
TA0CCTL1 = OUTMOD_7;
TA01_SET;
break;
case 'B': case 'b':
TA0CCTL1 = OUTMOD_3;
TA01_SET;
break;
case 'D': case'd':
TA0CCTL1 = OUTMOD_6;
TA01_SET;
break;
case '0':case 0: //如果设置为禁用
TA01_OFF; //TA0.1恢复为普通IO口
break;
default : return(0); //设置参数有误,返回0
}
switch(Mode2) //设置PWM通道2的输出模式。
{
case 'F': case 'f':
TA0CCTL2 = OUTMOD_7;
TA02_SET; break;
case 'B': case 'b':
TA0CCTL2 = OUTMOD_3;
TA02_SET;
break;
case 'D': case 'd':
TA0CCTL2 = OUTMOD_2;
TA02_SET;
break;
case '0':case 0: //如果设置为禁用
TA02_OFF; //TA0.1恢复为普通IO口
break;
default : return(0); //设置参数有误,返回0
}
switch(Mode3) //设置PWM通道2的输出模式。
{
case 'F': case 'f':
TA0CCTL3 = OUTMOD_7;
TA03_SET; break;
case 'B': case 'b':
TA0CCTL3 = OUTMOD_3;
TA03_SET;
break;
case 'D': case 'd':
TA0CCTL3 = OUTMOD_2;
TA03_SET;
break;
case '0':case 0: //如果设置为禁用
TA03_OFF; //TA0.1恢复为普通IO口
break;
default : return(0); //设置参数有误,返回0
}switch(Mode4) //设置PWM通道2的输出模式。
{
case 'F': case 'f':
TA0CCTL4 = OUTMOD_7;
TA04_SET;
P4OUT ^=BIT7;break;
case 'B': case 'b':
TA0CCTL4 = OUTMOD_3;
TA04_SET;
break;
case 'D': case 'd':
TA0CCTL4 = OUTMOD_2;
TA04_SET;
break;
case '0':case 0: //如果设置为禁用
TA04_OFF; //TA0.1恢复为普通IO口
break;
default : return(0); //设置参数有误,返回0
}
return(1);
}
char TA0_PWM_SetPeriod(unsigned int Period)
{
if (Period>65535) return(0);
TA0CCR0 = Period;
return(1);
}
char TA0_PWM_SetPermill(char Channel,unsigned int Percent)
{
switch (Channel)
{
case 1 :
{
if(Percent>1000) Percent=1000;
if(Channel==1)
TA0CCR1=Percent* TA0CCR0/1000;
break;
}
case 2 :
{
if(Percent>1000) Percent=1000;
if(Channel==2)
TA0CCR2=Percent* TA0CCR0/1000;
break;
}
case 3 :
{
if(Percent>1000) Percent=1000;
if(Channel==3)
TA0CCR3=Percent* TA0CCR0/1000;
break;
}
case 4 :
{
if(Percent>1000) Percent=1000;
if(Channel==4)
TA0CCR4=Percent* TA0CCR0/1000;
break;
}
default:
return (0);
}
return (1);
}
|