goolloo 发表于 2012-7-7 23:00:14

MSP430-LAUNCHPAD 6个官方教学例程+GUI

本帖最后由 goolloo 于 2012-7-7 23:04 编辑

LaunchPad_TemperSensor

./******************************************************************************
*                  MSP-EXP430G2-LaunchPad User Experience Application
*
* 1. Device starts up in LPM3 + blinking LED to indicate device is alive   
*    + Upon first button press, device transitions to application mode
* 2. Application Mode
*    + Continuously sample ADC Temp Sensor channel, compare result against
*      initial value      
*    + Set PWM based on measured ADC offset: Red LED for positive offset, Green
*      LED for negative offset
*    + Transmit temperature value via TimerA UART to PC
*    + Button Press --> Calibrate using current temperature
*                     Send character '� via UART, notifying PC
******************************************************************************/

#include"msp430g2553.h"

#define   LED0                  BIT0
#define   LED1                  BIT6
#define   LED_DIR               P1DIR
#define   LED_OUT               P1OUT



#define   BUTTON                BIT3
#define   BUTTON_OUT            P1OUT
#define   BUTTON_DIR            P1DIR
#define   BUTTON_IN             P1IN
#define   BUTTON_IE             P1IE
#define   BUTTON_IES            P1IES
#define   BUTTON_IFG            P1IFG
#define   BUTTON_REN            P1REN

#define   TXD                   BIT1                      // TXD on P1.1
#define   RXD                   BIT2                      // RXD on P1.2

#define   APP_STANDBY_MODE      0
#define   APP_APPLICATION_MODE1

#define   TIMER_PWM_MODE      0   
#define   TIMER_UART_MODE       1
#define   TIMER_PWM_PERIOD      2000
#define   TIMER_PWM_OFFSET      20

#define   TEMP_SAME             0
#define   TEMP_HOT            1
#define   TEMP_COLD             2

#define   TEMP_THRESHOLD      5

//   Conditions for 9600/4=2400 Baud SW UART, SMCLK = 1MHz
#define   Bitime_5            0x05*4                      // ~ 0.5 bit length + small adjustment
#define   Bitime                13*4//0x0D   

#define   UART_UPDATE_INTERVAL1000


unsigned char BitCnt;


unsigned char applicationMode = APP_STANDBY_MODE;
unsigned char timerMode = TIMER_PWM_MODE;

unsigned char tempMode;
unsigned char calibrateUpdate = 0;
unsigned char tempPolarity = TEMP_SAME;
unsigned int TXByte;
                              
/* Using an 8-value moving average filter on sampled ADC values */
long tempMeasured;
unsigned char tempMeasuredPosition=0;
long tempAverage;

long tempCalibrated, tempDifference;



void InitializeLeds(void);
void InitializeButton(void);
void PreApplicationMode(void);                     // Blinks LED, waits for button press
void ConfigureAdcTempSensor(void);
void ConfigureTimerPwm(void);
void ConfigureTimerUart(void);
void Transmit(void);
void InitializeClocks(void);

void main(void)
{
unsigned int uartUpdateTimer = UART_UPDATE_INTERVAL;
unsigned char i;
WDTCTL = WDTPW + WDTHOLD;               // Stop WDT

InitializeClocks();
InitializeButton();
InitializeLeds();
PreApplicationMode();                     // Blinks LEDs, waits for button press

/* Application Mode begins */
applicationMode = APP_APPLICATION_MODE;
ConfigureAdcTempSensor();
ConfigureTimerPwm();
   
__enable_interrupt();                     // Enable interrupts.


/* Main Application Loop */
while(1)
{   
    ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
    __bis_SR_register(CPUOFF + GIE);      // LPM0 with interrupts enabled
   
   
    /* Moving average filter out of 8 values to somewhat stabilize sampled ADC */
    tempMeasured = ADC10MEM;
    if (tempMeasuredPosition == 8)
      tempMeasuredPosition = 0;
    tempAverage = 0;
    for (i = 0; i < 8; i++)
      tempAverage += tempMeasured;
    tempAverage >>= 3;                      // Divide by 8 to get average
   
    if ((--uartUpdateTimer == 0) || calibrateUpdate )
    {
      ConfigureTimerUart();
      if (calibrateUpdate)
      {
      TXByte = 248;                     // A character with high value, outside of temp range
      Transmit();
      calibrateUpdate = 0;
      }   
      TXByte = (unsigned char)( ((tempAverage - 630) * 761) / 1024 );      
      Transmit();
      uartUpdateTimer = UART_UPDATE_INTERVAL;
      ConfigureTimerPwm();
    }
   
   
    tempDifference = tempAverage - tempCalibrated;
    if (tempDifference < -TEMP_THRESHOLD)
    {
      tempDifference = -tempDifference;
      tempPolarity = TEMP_COLD;
      LED_OUT &= ~ LED1;
    }
    else
    if (tempDifference > TEMP_THRESHOLD)
    {
      tempPolarity = TEMP_HOT;
      LED_OUT &= ~ LED0;
    }
    else
    {
      tempPolarity = TEMP_SAME;
      TACCTL0 &= ~CCIE;
      TACCTL1 &= ~CCIE;
      LED_OUT &= ~(LED0 + LED1);      
    }
   
    if (tempPolarity != TEMP_SAME)   
    {      
      tempDifference <<= 3;
      tempDifference += TIMER_PWM_OFFSET;      
      TACCR1 = ( (tempDifference) < (TIMER_PWM_PERIOD-1) ? (tempDifference) : (TIMER_PWM_PERIOD-1) );
      TACCTL0 |= CCIE;
      TACCTL1 |= CCIE;      
    }   
}
}

void PreApplicationMode(void)
{   
LED_DIR |= LED0 + LED1;
LED_OUT |= LED0;                        // To enable the LED toggling effect
LED_OUT &= ~LED1;
   
BCSCTL1 |= DIVA_1;                        // ACLK/2
BCSCTL3 |= LFXT1S_2;                      // ACLK = VLO

TACCR0 = 1200;                           //   
TACTL = TASSEL_1 | MC_1;                  // TACLK = SMCLK, Up mode.
TACCTL1 = CCIE + OUTMOD_3;                // TACCTL1 Capture Compare
TACCR1 = 600;
__bis_SR_register(LPM3_bits + GIE);          // LPM0 with interrupts enabled
}

void ConfigureAdcTempSensor(void)
{
unsigned char i;
/* Configure ADC Temp Sensor Channel */
ADC10CTL1 = INCH_10 + ADC10DIV_3;         // Temp Sensor ADC10CLK/4
ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE;
__delay_cycles(1000);                     // Wait for ADC Ref to settle
ADC10CTL0 |= ENC + ADC10SC;               // Sampling and conversion start
__bis_SR_register(CPUOFF + GIE);          // LPM0 with interrupts enabled
tempCalibrated = ADC10MEM;
for (i=0; i < 8; i++)
    tempMeasured = tempCalibrated;
tempAverage = tempCalibrated;
}


void ConfigureTimerPwm(void)
{
timerMode = TIMER_PWM_MODE;

TACCR0 = TIMER_PWM_PERIOD;                              //   
TACTL = TASSEL_2 | MC_1;                  // TACLK = SMCLK, Up mode.
TACCTL0 = CCIE;
TACCTL1 = CCIE + OUTMOD_3;                // TACCTL1 Capture Compare
TACCR1 = 1;
}

void ConfigureTimerUart(void)
{
timerMode = TIMER_UART_MODE;               // Configure TimerA0 UART TX
                           
CCTL0 = OUT;                               // TXD Idle as Mark
TACTL = TASSEL_2 + MC_2 + ID_3;            // SMCLK/8, continuous mode
P1SEL |= TXD + RXD;                        //
P1DIR |= TXD;                              //
}

// Function Transmits Character from TXByte
void Transmit()
{
BitCnt = 0xA;                           // Load Bit counter, 8data + ST/SP
while (CCR0 != TAR)                     // Prevent async capture
    CCR0 = TAR;                           // Current state of TA counter
CCR0 += Bitime;                     // Some time till first bit
TXByte |= 0x100;                        // Add mark stop bit to TXByte
TXByte = TXByte << 1;               // Add space start bit
CCTL0 =CCIS0 + OUTMOD0 + CCIE;          // TXD = mark = idle
while ( CCTL0 & CCIE );                   // Wait for TX completion
}



// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{
if (timerMode == TIMER_UART_MODE)
{
    CCR0 += Bitime;                           // Add Offset to CCR0
    if (CCTL0 & CCIS0)                        // TX on CCI0B?
    {
      if ( BitCnt == 0)
      CCTL0 &= ~ CCIE;                        // All bits TXed, disable interrupt
      else
      {
      CCTL0 |=OUTMOD2;                  // TX Space
      if (TXByte & 0x01)
      CCTL0 &= ~ OUTMOD2;                   // TX Mark
      TXByte = TXByte >> 1;
      BitCnt --;
      }
    }
}
else
{
    if (tempPolarity == TEMP_HOT)
      LED_OUT |= LED1;   
    if (tempPolarity == TEMP_COLD)      
      LED_OUT |= LED0;
    TACCTL0 &= ~CCIFG;            
}
}

#pragma vector=TIMER0_A1_VECTOR
__interrupt void ta1_isr(void)
{
TACCTL1 &= ~CCIFG;
if (applicationMode == APP_APPLICATION_MODE)
    LED_OUT &= ~(LED0 + LED1);
else
    LED_OUT ^= (LED0 + LED1);
   
}

void InitializeClocks(void)
{

BCSCTL1 = CALBC1_1MHZ;                  // Set range
DCOCTL = CALDCO_1MHZ;
BCSCTL2 &= ~(DIVS_3);                         // SMCLK = DCO / 8 = 1MHz
}

void InitializeButton(void)               // Configure Push Button
{
BUTTON_DIR &= ~BUTTON;
BUTTON_OUT |= BUTTON;
BUTTON_REN |= BUTTON;
BUTTON_IES |= BUTTON;
BUTTON_IFG &= ~BUTTON;
BUTTON_IE |= BUTTON;
}


void InitializeLeds(void)
{
LED_DIR |= LED0 + LED1;                        
LED_OUT &= ~(LED0 + LED1);
}

/* *************************************************************
* Port Interrupt for Button Press
* 1. During standby mode: to exit and enter application mode
* 2. During application mode: to recalibrate temp sensor
* *********************************************************** */
#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{   
BUTTON_IFG = 0;
BUTTON_IE &= ~BUTTON;            /* Debounce */
WDTCTL = WDT_ADLY_250;
IFG1 &= ~WDTIFG;               /* clear interrupt flag */
IE1 |= WDTIE;
   
if (applicationMode == APP_APPLICATION_MODE)
{
    tempCalibrated = tempAverage;
    calibrateUpdate= 1;
}
else
{
    applicationMode = APP_APPLICATION_MODE; // Switch from STANDBY to APPLICATION MODE
    __bic_SR_register_on_exit(LPM3_bits);      
}   
}

#pragma vector=WDT_VECTOR
__interrupt void WDT_ISR(void)
{
    IE1 &= ~WDTIE;                   /* disable interrupt */
    IFG1 &= ~WDTIFG;               /* clear interrupt flag */
    WDTCTL = WDTPW + WDTHOLD;      /* put WDT back in hold state */
    BUTTON_IE |= BUTTON;             /* Debouncing complete */
}

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
__bic_SR_register_on_exit(CPUOFF);      // Return to active mode
}
.
.
.
.
.
.
LaunchPad_Lab2

.//******************************************************************************
//LaunchPad Lab2 - Software Toggle P1.0,
//
//                MSP430G2xx2
//             -----------------
//         /|\|            XIN|-
//          | |               |
//          --|RST          XOUT|-
//            |               |
//            |             P1.0|-->LED
//
//******************************************************************************

#include<msp430g2553.h>

void main(void)
{
        WDTCTL = WDTPW + WDTHOLD;               // Stop watchdog timer

        if (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF)
        {
                while(1);                                                // If calibration constants erased, trap CPU!!
        }

        // Configure Basic Clock
        BCSCTL1 = CALBC1_1MHZ;                                 // Set range
        DCOCTL = CALDCO_1MHZ;                                // Set DCO step + modulation
        BCSCTL3 |= LFXT1S_2;                                // Set LFXT1

        P1DIR = BIT6;                                                 // P1.6 output (green LED)
        P1OUT = 0;                                                    // LED off

        IFG1 &= ~OFIFG;                                      // Clear OSCFault flag
        BCSCTL2 |=SELM_1 + DIVM_0;                 // Set MCLK

        for(;;)
        {
                P1OUT = BIT6;                                   // P1.6 on (green LED)
                _delay_cycles(100);
                P1OUT = 0;                                        // green LED off
                _delay_cycles(5000);
        }
}
.
.
.
.
.
.
LaunchPad_Lab3
.//******************************************************************************
//LaunchPad Lab3 - Software Port Interrupt Service
//
//               MSP430G2xx2
//            -----------------
//      /|\|            XIN|-
//         | |               |
//         --|RST          XOUT|-
//   /|\   |               |
//      --o--|P1.3         P1.0|-->LED
//   \|/
//

//******************************************************************************

#include<msp430g2553.h>

void main(void)
{
        WDTCTL = WDTPW + WDTHOLD;               // Stop watchdog timer
        P1DIR |= BIT0;                            // Set P1.0 to output direction
        P1IES |= BIT3;                            // P1.3 Hi/lo edge
        P1IFG &= ~BIT3;                           // P1.3 IFG cleared
        P1IE |= BIT3;                           // P1.3 interrupt enabled

        _BIS_SR(LPM4_bits + GIE);               // Enter LPM4 w/interrupt
}

// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
        if (P1IFG & BIT3)
        {
                P1OUT ^= BIT0;                         // P1.0 = toggle
                P1IFG &= ~BIT3;                        // P1.3 IFG cleared
        }
}
.
.
.
.
.
.
LaunchPad_Lab4
.//******************************************************************************
//LaunchPad Lab4 - Timer Toggle P1.6,
//
//                MSP430G2452
//             -----------------
//         /|\|            XIN|-
//          | |               |
//          --|RST          XOUT|-
//            |               |
//            |             P1.6|-->LED
//
//******************************************************************************

#include<msp430g2553.h>

void main(void)
{
        WDTCTL = WDTPW + WDTHOLD;               // Stop watchdog timer

        if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)
        {
                while(1);                    // If calibration constants erased, trap CPU!!
        }

        BCSCTL1 = CALBC1_1MHZ;                   // Set range
        DCOCTL = CALDCO_1MHZ;                  // Set DCO step + modulation
        BCSCTL3 |= LFXT1S_2;                      // LFXT1 = VLO

        P1DIR = 0x40;                           // P1.6 output (green LED)
        P1OUT = 0;                              // LED off

        IFG1 &= ~OFIFG;                           // Clear OSCFault flag
        BCSCTL1 |= DIVA_3;                                                  // ACLK = VLO/8
        BCSCTL2 |= SELM_3 + DIVM_3 + DIVS_3;      // MCLK = DCO/8, SMCLK = DCO/8

        // Configure TimerA
        TACTL = TASSEL_1 + MC_1 + TAIE;          // Source: ACLK, UP mode
        CCR0 = 5100;                                        //Timer count 5100
        CCR1 = 2000;                                                //Timer count 100
        CCTL0 = CCIE;                          //CCR0 interrupt enabled
        CCTL1 = CCIE;                          //CCR1 interrupt enabled

        _BIS_SR(GIE);

        for(;;);
}

// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0 (void)
{
        P1OUT |= BIT6;                // P1.6 output High
}

// Timer A1 Interrupt Vector (TA0IV) handler
#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer_A1(void)
{
        switch( TA0IV )
        {
        case2: P1OUT &= ~BIT6;                  // P1.6 output Low
         break;
        case 10:
         break;
}
}
.
.
.
.
.
.
LaunchPad_Lab5
.//******************************************************************************
//LaunchPad Lab5 - Timer Toggle P1.6 with Low Power Mode
//
//                MSP430G2452
//             -----------------
//         /|\|            XIN|-
//          | |               |
//          --|RST          XOUT|-
//            |               |
//            |             P1.6|-->LED
//
//******************************************************************************
#include<msp430g2452.h>

void main(void)
{
        WDTCTL = WDTPW + WDTHOLD;               // Stop watchdog timer

        if (CALBC1_1MHZ == 0xFF || CALDCO_1MHZ == 0xFF)
        {
                while(1);                              // If calibration constants erased, trap CPU!!
        }

        BCSCTL1 = CALBC1_1MHZ;                   // Set range
        DCOCTL = CALDCO_1MHZ;                  // Set DCO step + modulation
        BCSCTL3 |= LFXT1S_2;                      // LFXT1 = VLO

        P1DIR = 0x40;                           // P1.6 output (green LED)
        P1OUT = 0;                              // LED off

        IFG1 &= ~OFIFG;                           // Clear OSCFault flag
        BCSCTL1 |= DIVA_3;                                                  // ACLK = VLO/8
        BCSCTL2 |= SELM_3 + DIVM_3 + DIVS_3;      // MCLK = DCO/8, SMCLK = DCO/8

        //TACTL = TASSEL_2 +MC_1;          // SMCLK, UP mode
        TACTL = TASSEL_1 +MC_1;          // ACLK, UP mode
        CCR0 = 5100;                                        //Timer count 5100
        CCR1 = 2000;                                                //Timer count 100
        CCTL0 = CCIE;                  //interrupt enabled
        CCTL1 = CCIE;                  //interrupt enabled

        _BIS_SR(LPM0_bits + GIE);                        //Enter Low Power Mode

        for(;;);
}

// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A0 (void)
{
        P1OUT |= BIT6;                // P1.6 output High
}

// Timer_A2 Interrupt Vector (TA0IV) handler
#pragma vector=TIMER0_A1_VECTOR
__interrupt void Timer_A1(void)
{
        switch( TA0IV )
        {
        case2: P1OUT &= ~BIT6;                  // P1.6 output Low
         break;
        case 10:
         break;
        }
}
.
.
.
.
.
.
LaunchPad_Lab6//******************************************************************************
//LaunchPad Lab5 - ADC10, Sample A10 Temp and Convert to oC and oF
//
//                MSP430G2452
//             -----------------
//         /|\|            XIN|-
//          | |               |
//          --|RST          XOUT|-
//            |               |
//            |A10            |
//
//******************************************************************************
#include"msp430g2553.h"

long temp;
long IntDegF;
long IntDegC;

void main(void)
{
WDTCTL = WDTPW + WDTHOLD;               // Stop WDT
//Configure ADC10
ADC10CTL1 = INCH_10 + ADC10DIV_3;         // Choose ADC Channel as Temp Sensor
ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE;        //Choose ADC Ref source
__enable_interrupt();                     // Enable interrupts.
TACCR0 = 30;                              // Delay to allow Ref to settle
TACCTL0 |= CCIE;                        // Compare-mode interrupt.
TACTL = TASSEL_2 | MC_1;                  // TACLK = SMCLK, Up mode.
LPM0;                                     // Wait for delay.
TACCTL0 &= ~CCIE;                         // Disable timer Interrupt
__disable_interrupt();

while(1)
{
    ADC10CTL0 |= ENC + ADC10SC;             // Sampling and conversion start
    __bis_SR_register(LPM0_bits + GIE);      // LPM0 with interrupts enabled

    // oF = ((A10/1024)*1500mV)-923mV)*1/1.97mV = A10*761/1024 - 468
    temp = ADC10MEM;
    IntDegF = ((temp - 630) * 761) / 1024;

    // oC = ((A10/1024)*1500mV)-986mV)*1/3.55mV = A10*423/1024 - 278
    temp = ADC10MEM;
    IntDegC = ((temp - 673) * 423) / 1024;

    __no_operation();                     // SET BREAKPOINT HERE
}
}

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
__bic_SR_register_on_exit(LPM0_bits);      // Clear CPUOFF bit from 0(SR)
}

#pragma vector=TIMER0_A0_VECTOR
__interrupt void ta0_isr(void)
{
TACTL = 0;
__bic_SR_register_on_exit(LPM0_bits);      // Clear CPUOFF bit from 0(SR)
}.
.
.
.
.
.
GUI是用于第一个实验的PC上位机{:2_27:}
页: [1]
查看完整版本: MSP430-LAUNCHPAD 6个官方教学例程+GUI