For a detailed explanation of this program, you can download the e-book by clicking on the picture above.
The original book contains 4 programs in total
Program 1
#include <msp430x41x.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog
FLL_CTL0 |= XCAP18PF; // Configure crystal load capacitance
P1DIR |= BIT3; // Set P1.3 as output pin
TACCTL0 |= CCIE; // Enable interrupt of compare/capture module 0
TACCR0 = 3277-1; //100ms is about 3277 ACLK cycles
TACTL = TASSEL_1 + MC_1; //TA is set to up-counting mode, clock = ACLK
_EINT();
LPM3;
}
#pragma vector=TIMERA0_VECTOR
__interrupt void TACCR0_ISR (void) // count to TACCR0 interrupt
{
P1OUT ^= BIT3; // P1.3 inverted
}
Copy code
Program 2
#include <msp430x41x.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog
FLL_CTL0 |= XCAP18PF; // Configure crystal load capacitance
P1DIR |= BIT3+BIT1+BIT4; // Set P1.3, P1.1, P1.4 as output pins
TACCR0 = 32768-1; // 1 second = 32768 ACLK cycles
TACCR1 = 6554-1; // 0.2 seconds = 6554 ACLK cycles
TACCR2 = 29938-1; // 0.7 seconds = 29938 ACLK cycles
TACTL = TASSEL_1 + MC_1; // TA is set to up-counting mode, clock = ACLK
TACTL |= TAIE; // Enable TA overflow interrupt
TACCTL1 |= CCIE; // Enable interrupt for compare/capture module 1
TACCTL2 |= CCIE; // Enable interrupt for compare/capture module 2 _EINT
();
while(1)
{
// CPU can perform other tasks
}
}
#pragma vector=TIMERA1_VECTOR
__interrupt void TA_ISR(void)
{
switch( TAIV )
{
case 2: P1OUT &=~( BIT3+BIT1+BIT4);
P1OUT |= BIT1; //Only the green light is on in the TACCR1~TACCR2 stage
break;
case 4: P1OUT &=~( BIT3+BIT1+BIT4);
P1OUT |= BIT4; //Only the blue light is on in the TACCR2~0 stage
break;
case 10: P1OUT &=~( BIT3+BIT1+BIT4);
P1OUT |= BIT3; //Only the red light is on in the 0~TACCR1 stage
break;
}
}
Copy code
Program 3
#include <msp430x41x.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog
FLL_CTL0 |= XCAP18PF; // Configure crystal load capacitor
P1DIR |= BIT3 + BIT4; // Set P1.3, P1.4 as output pins
TACTL |= MC_1 + TASSEL_1 + ID_0; // Set timer TA to incremental counting mode, ACLK
TACCR0=328-1; // Total PWM period = 328 ACLK periods, approximately equal to 100Hz
TACCR1=246; // TA1 duty cycle = 246/328= 75%
TACCR2=164; // TA2 duty cycle = 164/328= 50%
TACTL |= TAIE; // Enable TA overflow interrupt
TACCTL1 |= CCIE; // Enable interrupts for compare/capture module 1
TACCTL2 |= CCIE; // Enable interrupts for compare/capture module 2
_EINT();
while(1)
{
//... ... // The CPU can perform other tasks
}
}
#pragma vector=TIMERA1_VECTOR
__interrupt void TA_ISR(void)
{
switch( TAIV )
{
case 2: P1OUT &=~ BIT3; // Count to TACCR1, P1.3 set low
break;
case 4: P1OUT &=~ BIT4; // Count to TACCR2, P1.4 set low
break;
case 10: P1OUT |=( BIT3 + BIT4); // Count to TACCR0, set high
break;
}
}
Copy code
Program 4
#include <msp430x41x.h>
int Result;
int TA_OverflowCnt; //Variable for storing the number of TA overflows
unsigned long int ExeTime; //Variable for storing the execution time
/********************************************************************
Running time Function 1 to be tested: Calculate the sum of two floating-point numbers
************************************************************/
float FloatSum(float x ,float y) /*226 instruction cycles*/
{
return(x+y);
}
/********************************************************************
Running time Function 2 to be tested: Calculate the sum of two fixed-point numbers
************************************************************/
int IntSum(int x ,int y) /*17 instruction cycles*/
{
return(x+y);
}
/********************************************************************
Running time Function 3 to be tested: Software delay
****************************************************************/
void Delay() /*774030 instruction cycles*/
{ int i,j;
for(i=0;i<1234;i++)
{
for(j=0;j<123;j++);
}
}
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog
FLL_CTL0 |= XCAP18PF; // Configure crystal load capacitance
_EINT(); // General interrupt enable
while(1)
{
//-------------------------Measure the intrinsic delay of the timer-------------------------
TACTL = TASSEL_2 + MC_2 + TAIE + TACLR; // TA is cleared and starts timing
TACTL = TASSEL_2 + MC_0; // TA stops timing
ExeTime=TAR; // Read TA count value, which is the intrinsic delay time
_NOP(); // Set a breakpoint on this line to observe
//-----------------------Measure the execution time of the floating-point sum function----------------------
TACTL = TASSEL_2 + MC_2 + TAIE + TACLR; // TA is cleared and starts timing
Result=FloatSum(12345 ,12456); // Execute the function to be tested 1
TACTL = TASSEL_2 + MC_0; // TA stops timingExeTime
=TAR; // Read TA count value, which is the execution time_NOP
(); // Set a breakpoint on this line to observe
//----------------------Measure the execution time of the fixed-point sum function-------------------
TACTL = TASSEL_2 + MC_2 + TAIE + TACLR; // TA is cleared and starts timingResult
= IntSum(12345 ,12456); // Execute the function to be tested 2
TACTL = TASSEL_2 + MC_0; // TA stops timingExeTime
=TAR; // Read TA count value, which is the execution time_NOP
(); // Set a breakpoint on this line to observe
//----------------------Measure the execution time of the delay and function--------------------
TA_OverflowCnt=0; // The long execution time function TA will overflow, and a variable is used to count the number of overflowsTACTL
= TASSEL_2 + MC_2 + TAIE + TACLR; // TA is cleared and starts timingDelay
(); // Execute the function to be tested 3
TACTL = TASSEL_2 + MC_0; // TA stops timing
ExeTime=TA_OverflowCnt*65536+TAR; // Read TA count value and overflow times, calculate execution time
_NOP(); // Set a breakpoint on this line to observe
}
}
#pragma vector=TIMERA1_VECTOR
__interrupt void TA_ISR(void)
{
switch( TAIV )
{
case 2: break;
case 4: break;
case 10: TA_OverflowCnt++; // Each time TA overflows, the overflow count variable + 1
break;
}
}
Copy code
|