#include "msp430g2553.h"
void main( void )
{
WDTCTL = WDTPW + WDTHOLD; //关看门狗
TACTL=TASSEL_1+TACLR+MC_1; //ACLK为时钟源,清TAR,增计数模式
TACCR0=512-1; //设定PWM周期
TACCTL1=OUTMOD_7; //CCR1输出为reset/set模式
TACCR1=55584; //CCR1的PWM占空比设定
TACCTL2=OUTMOD_7; //CCR2输出为reset/set模式
TACCR2=28; //CCR2的PWM占空比设定
P1DIR|=BIT0 + BIT6; //P1.2、P1.3输出,对应TA1,TA2
P1SEL|=BIT0 + BIT6; //TA1,TA2输出功能
while(1);
}
此帖出自
小平头技术问答
UP MODE
PWM控制小灯亮度
author:279827164
****************************/
#include "msp430g2553.h"
#define uint unsigned int
#define uchar unsigned char
volatile uchar flag,dir;
volatile uint t0,t1; //高低电平计数个数
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
t0 = 100;
TACTL |= TASSEL1 + MC_1; //UP to CCR0,SMCLK
CCTL0 |= CCIE; //比较中断使能
CCR0 = 100 ;
P1DIR |= BIT2 + BIT0;
__enable_interrupt();
for(;;)
{
if(flag == 2)
{
flag = 0;
}
}
}
#pragma vector = TIMER0_A0_VECTOR
__interrupt void up_to_CCR0(void)
{
P1OUT ^= BIT2;
if(flag == 0) //高电平
{
t1 = 1200 - t0; //总周期1200
CCR0 = t1;
}
else //低电平
{
CCR0 = t0;
if(t0 == 1100) //高电平计时个数最大值
{
dir = 1;
}
else if(t0 == 100) //最小值
{
dir = 0;
}
if(dir == 0)
{
t0++;
}
else
{
t0--;
}
}
flag ++;
}
换成TA1.用示波器测试的是对的
#include "msp430g2553.h"
void main( void )
{
WDTCTL = WDTPW + WDTHOLD; //关看门狗
TA1CTL=TASSEL_1+TACLR+MC_1; //ACLK为时钟源,清TAR,增计数模式
TA1CCR0=2000; //设定PWM周期
TA1CCTL1=OUTMOD_7; //CCR1输出为reset/set模式
TA1CCR1=1000; //CCR1的PWM占空比设定
TA1CCTL2=OUTMOD_7; //CCR2输出为reset/set模式
TA1CCR2=1500; //CCR2的PWM占空比设定
P2DIR|=BIT1 + BIT4; //P2.1、P2.4输出,对应TA1.1,TA1.2
P2SEL|=BIT1 + BIT4; //对应TA1.1,TA1.2
while(1);
}
一周热门 更多>