程序功能:
MSP430 IO上電為輸入 高阻
P1.0 as ACLK output P1.4 as SMCLK output
按下S2可控制綠色LED亮滅
P1.1 和 P1.5 用于輸出PWM波形
P1.2用于輸出脈寬變化的PWM(8種占空比)
fx.h文件
#ifndef FX_H_
#define FX_H_
#define EnableWDT WDTCTL = WDTPW + WDTHOLD
void InitClkModule(void);
void InitGPIO(void);
void InitTimerA(void);
void InitADC10(void);
void InitUSI(void);
void YrApplication(void);
void InitSys(void);
#endif
fx.c文件
#include <msp430g2231.h>
void InitClkModule(void)
{
if (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF)
{
while(1); // If calibration constants erased, trap CPU!!
}
// Configure Basic Clock
BCSCTL1 = CALBC1_1MHZ; // Set range vloclk is 12kHz
DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation DCOCLK = 1MHz
//Insert User Code Here
BCSCTL2 |= SELM_0 + DIVM_0 + DIVS_3; // MCLK = DCOCLK = 1MHz, SMCLK = DCOCLK/8
BCSCTL3 |= LFXT1S_2; // ACLK = VLOCLK 6pF
}
void InitGPIO(void)
{
P1DIR |= BIT0 + BIT1 + BIT2 + BIT4 + BIT5 + BIT6; // P1.0 P1.1 P1.2 P1.4 P1.5and P1.6 are configured as output pin
P1SEL |= BIT0 + BIT1 + BIT2 + BIT4 + BIT5; // P1.0 as ACLK output P1.1 and P1.5 as compare out0 out putP1.4 as SMCLK output
P1IES |= BIT3; // P1.3 Hi/lo edge
P1IFG &= ~BIT3; // P1.3 IFG cleared
P1IE |= BIT3; // P1.3 interrupt enabled
}
void InitTimerA(void)
{
//Insert User Code Here
TACTL = TASSEL_2 + MC_2 + TAIE + TACLR; // SMCLK, enable TimerA interrupt
//TACTL = TASSEL_2 + MC_1 + TAIE + TACLR; // SMCLK, enable TimerA interrupt
TACCTL1 = OUTMOD_7; //PWM: reset/set mode
TACCTL0 = OUTMOD_4;
CCR0 = 0x7fff;
CCR1 = 0;
}
void InitADC10(void)
{
//Insert User Code Here
}
void InitUSI(void)
{
//Insert User Code Here
}
void YrApplication(void)
{
}
void InitSys(void)
{
InitClkModule();
InitGPIO();
InitTimerA();
}
main.c文件
#include <msp430g2231.h>
#include "fx.h"
unsigned int i = 0;
unsigned char flag = 1;
void main(void)
{
EnableWDT; // stop the watchdog
InitSys();
_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/interrupt
while(1);
}
#pragma vector=PORT1_VECTOR
__interrupt void Port1_ISR()
{
//The code below is a reference handle process,the interrupt related with the port never used should be delete。
if((P1IFG&BIT0) == BIT0)
{
//Handle the Interrupt generated by P1IN.0
P1IFG &= ~BIT0; //Clear the IFG
//Insert User Code Here
}
else if((P1IFG&BIT1) ==BIT1)
{
//Handle the Interrupt generated by P1IN.1
P1IFG &= ~BIT1; //Clear the IFG
//Insert User Code Here
}
else if((P1IFG&BIT2) ==BIT2)
{
//Handle the Interrupt generated by P1IN.2
P1IFG &= ~BIT2; //Clear the IFG
//Insert User Code Here
}
else if((P1IFG&BIT3) ==BIT3)
{
//Handle the Interrupt generated by P1IN.3
P1IFG &= ~BIT3; //Clear the IFG
//Insert User Code Here
P1OUT ^= BIT6; // P1.0 = toggle
}
else if((P1IFG&BIT4) ==BIT4)
{
//Handle the Interrupt generated by P1IN.4
P1IFG &= ~BIT4; //Clear the IFG
//Insert User Code Here
}
else if((P1IFG&BIT5) ==BIT5)
{
//Handle the Interrupt generated by P1IN.5
P1IFG &= ~BIT5; //Clear the IFG
//Insert User Code Here
}
else if((P1IFG&BIT6) ==BIT6)
{
//Handle the Interrupt generated by P1IN.6
P1IFG &= ~BIT6; //Clear the IFG
//Insert User Code Here
}
else
{
//Handle the Interrupt generated by P1IN.7
P1IFG &= ~BIT7; //Clear the IFG
//Insert User Code Here
}
//LPM3_EXIT; //退出中斷后退出低功耗模式。若退出中斷后要保留低功耗模式,將本句屏蔽
}
#pragma vector=USI_VECTOR
__interrupt void USI_ISR()
{
//Insert User Code Here
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR()
{
//Insert User Code Here
}
#pragma vector=TIMERA1_VECTOR
__interrupt void TIMERA1_ISR() // the interrupt source is CC1~2
{
switch (TAIV)
{
case 2:
//capture/compare1 interrupt
//Insert User Code Here
break;
case 4:
//capture/compare2 interrupt MSP430G2231 donot have the compare2 module
//Insert User Code Here
break;
case 10:
//TAIFG Timer overflow interrupt
//Insert User Code Here
P1OUT ^= BIT6; // change the status of green led when TimerA overflow
TACTL &= ~BIT0;
if(flag)//i++;
i += 4095;
else //i--;
i -=4095;
if(i > 0x7fff) flag = 0;
if(i == 0) flag = 1;
CCR1 = i; //change the duty-cycle
break;
}
}
#pragma vector=TIMERA0_VECTOR
__interrupt void TIMERA0_ISR() // the interrupt source is CC0
{
//Insert User Code Here
}
|