#include <msp430.h>
#define uchar unsigned char
#define CPU_F ((double)1035000)
#define delay_s(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
int a=0;
int b=0;
int c=0;
int d=0;
void Timer_A0_3_init() //TA0.3输出PWM P1.4输入
{
TA0CTL |= TASSEL_1+MC_1; //ACLK,增计数
TA0CCTL3 = OUTMOD_7; //输出模式7
TA0CCR0 = 512-1; //时钟频率为32768HZ,1KHZ
TA0CCR1 = a; //占空比CCR1/CCR0=1/4 左
}
void Timer_A0_4_init() //TA0.4输出PWM 1.5输入
{
TA0CTL |= TASSEL_1+MC_1; //ACLK,增计数
TA0CCTL4 = OUTMOD_7; //输出模式7
TA0CCR0 = 512-1; //时钟频率为32768HZ,波形32768/CCR0=1KHZ
TA0CCR1 =b; //占空比 右
}
void Timer_A1_1_init() //TA0.3输出PWM P1.4输入
{
TA1CTL |= TASSEL_1+MC_1; //ACLK,增计数
TA1CCTL1 = OUTMOD_7; //输出模式7
TA1CCR0 = 512-1; //时钟频率为32768HZ,1KHZ
TA1CCR1 = c; //占空比CCR1/CCR0=1/4 左
}
void Timer_A2_1_init() //TA2.1输出PWM 2.4输入
{
TA2CTL |= TASSEL_1+MC_1; //ACLK,增计数
TA2CCTL1 = OUTMOD_7; //输出模式7
TA2CCR0 = 512-1; //时钟频率为32768HZ,波形32768/CCR0=1KHZ
TA2CCR1 =d; //占空比 右
}
void IO_init()
{
P1SEL|=BIT4;
P1DIR|=BIT4;//P1.4输出 TA0.4
P1SEL|=BIT5;
P1DIR|=BIT5; // P1.5输出 TA0.3
P2SEL|=BIT0;
P2DIR|=BIT0;//P2.0输出 TA1.1
P2SEL|=BIT4;
P2DIR|=BIT4; // P2.4输出 TA2.1
TA0CCTL0 = CCIE; //设置捕获/比较控制寄存器中CCIE位为1,CCR0捕获/比较功能中断为允许。
TA0CCR0 = 512-1; //约1ms.实现X*(1/32768)=Y秒的定时,捕获/比较控制寄存器CCR0初值为32768。
TA0CTL = TASSEL_1+ID_0 + MC_1; //时钟源选择为ACLK(32768HZ)、1分频、增计数模式(0-CCR0溢出),32768/1=32768 HZ.
// _BIS_SR(LPM0_bits + GIE); //进入低功耗模式LPM0和开中断
__enable_interrupt(); //开总中断
}
void kongsu()
{
a=250;
b=250;
c=0;
b=0;
delay_s(3);
a=0;
b=0;
c=250;
b=250;
delay_s(3);
if(a>500)a=500;
if(a<0)a=0;
if(b>500)b=500;
if(b<0)b=0;
if(c>500)c=500;
if(c<0)c=0;
if(d>500)d=500;
if(d<0)d=0;
}
main(void)
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
_EINT(); //开总中断
IO_init();
while(1)
{
Timer_A0_3_init();
Timer_A0_4_init();
Timer_A1_1_init();
Timer_A2_1_init();
kongsu();
}
}
|