MSP430F149学习之路——PWM信号
代码一:
1 /******************************* 2 程序功能:ACLK=32768Hz 3 PWM波 T=512/32768 占空比75% 4 *********************************/ 5 6 #include <msp430x14x.h> 7 void main() 8 { 9 WDTCTL = WDTPW + WDTHOLD; 10 TACTL = TASSEL_1 + TACLR; 11 12 CCTL0 = 0X00; 13 CCTL1 = 0X00; 14 15 CCR0 = 512-1; //T=512/32768 16 CCTL1 = OUTMOD_7; 17 CCR1 = 384; //384/512=0.75 18 CCTL2 = OUTMOD_7; 19 // CCR2 = 124; 20 21 P1DIR |= BIT2;k 22 P1SEL |= BIT2; 23 P2DIR |= BIT0; 24 P2SEL |= BIT0; 25 26 TACTL |= MC_1; 27 }
代码二:
1 #include <msp430x14x.h> 2 void int_clk() 3 { 4 BCSCTL1 &= ~XT2OFF; 5 BCSCTL2 |= SELM1 + SELS; //MCLK-->1MHz SMCLK-->8MHz 6 do 7 { 8 IFG1 &= ~OFIFG; 9 for(int i=0;i<100;i++) 10 _NOP(); 11 }while((IFG1 & OFIFG)!=0); 12 IFG1 &= ~OFIFG; 13 } 14 void main() 15 { 16 WDTCTL = WDTPW + WDTHOLD; 17 int_clk(); 18 P1DIR |= BIT0; 19 P1SEL |= BIT0; 20 TACCR0 = 800; //PWM f=10kHz 21 TACCR1 = 400; //1:1 22 TACCTL1 |= OUTMOD_7; 23 TACTL = TASSEL_2 + MC_1; 24 }
代码三:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /****************************** 2 程序功能:在P1.2,P1.6,P2.3及P1.3,P1.7,P2.4同时产生PWM波 3 *********************************************************/ 4 #include <msp430x14x.h> 5 void int_clk() 6 { 7 BCSCTL1 &= ~XT2OFF; 8 BCSCTL2 |= SELM_2 + SELS; 9 do 10 { 11 IFG1 &= ~OFIFG; 12 for(int i=100;i>0;i--) 13 _NOP(); 14 }while((IFG1 & OFIFG)!=0); 15 IFG1 &= ~OFIFG; 16 } 17 void main() 18 { 19 WDTCTL = WDTPW + WDTHOLD; 20 int_clk(); 21 P2SEL |= BIT3; 22 P2DIR |= BIT3; 23 24 P1SEL |= BIT6; 25 P1DIR |= BIT6; 26 27 P1SEL |= BIT2; 28 P1DIR |= BIT2; 29 30 P1SEL |= BIT3; 31 P1DIR |= BIT3; 32 33 P1SEL |= BIT7; 34 P1DIR |= BIT7; 35 36 P2SEL |= BIT4; 37 P2DIR |= BIT4; 38 39 TACTL |= TASSEL_2 + MC_1 + TACLR; 40 CCR0 = 8000-1; 41 CCR1 = 4000; 42 CCTL1|=OUTMOD_7; 43 CCR2 = 4000; 44 CCTL2|=OUTMOD_7; 45 }