完整例程说明:P2.1经TA1产生一个周期为400Hz、占空比为25%的方波,并将P2.1 ----连接到--- P1.2作为TA0的捕获输入,
P1.0接LED灯,在P1.2测得输入的方波是25%占空比的方波时,LED将会点亮否则是灭的!!!!
- #include <msp430.h>
-
- unsigned char Count, First_Time;
- unsigned int REdge1, REdge2, FEdge;
-
- int main(void)
- {
- unsigned int Period, ON_Period;
- unsigned char DutyCycle;
-
- WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
-
- // P1SEL |= BIT0;
- P1DIR |= BIT0; // P1.0/LED Output
- P1OUT &= ~BIT0; // LED off
- if (CALBC1_8MHZ==0xFF) // If calibration constant erased
- {
- while(1); // do not load, trap CPU!!
- }
- DCOCTL = 0; // Select lowest DCOx and MODx settings
- BCSCTL1 = CALBC1_8MHZ; // Set DCO to 8MHz
- DCOCTL = CALDCO_8MHZ;
-
- // Configure Port Pins
- P2DIR |= BIT1; // P2.1/TA1.1 Output
- P2SEL |= BIT1; // TA1.1 Option select
- P1DIR &= ~BIT2; // P1.1/TA0.1 Input Capture
- P1SEL |= BIT2; // TA0.1 option select
-
- // Configure TA1.1 to output PWM signal
- // Period = 82/32khz = 2.5ms ~ 400Hz Freq
- TA1CCR0 = 82-1; // Period Register
- TA1CCR1 = 21; // TA1.1 25% dutycycle
- TA1CCTL1 |= OUTMOD_7; // TA1CCR1, Reset/Set
- TA1CTL = TASSEL_1 + MC_1 + TACLR; // ACLK, upmode, clear TAR
-
- // Configure the TA0CCR1 to do input capture
- TA0CCTL1 = CAP + CM_3 + CCIE + SCS + CCIS_0;
- // TA0CCR1 Capture mode; CCI1A; Both
- // Rising and Falling Edge; interrupt enable
- TA0CTL |= TASSEL_2 + MC_2 + TACLR; // SMCLK, Cont Mode; start timer
-
- // Variable Initialization
- Count = 0x0;
- First_Time = 0x01;
-
- while(1)
- {
- __bis_SR_register(LPM0_bits + GIE); // Enter LPM0
- __no_operation(); // For debugger
- // On exiting LPM0
- if (TA0CCTL1 & COV) // Check for Capture Overflow
- while(1); // Loop Forever
-
- Period = REdge2 - REdge1; // Calculate Period
- ON_Period = FEdge-REdge1; // On period
- DutyCycle = ((unsigned long)ON_Period*100/Period);
- if(DutyCycle!= 25)
- {
- P1OUT |= BIT0;
- }
- else
- {
- P1OUT &= ~BIT0;
- }
- }
- }
-
|