⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msp430f2012 tilt sensor 0-00.c

📁 桦宣430研讨会资料盘里的msp430f2013+cc2500的pcb设计资料
💻 C
字号:
//*****************************************************************************
//  Code for application report SLAAxxx -
//  "Low-Power Tilt Sensor using the MSP430F2012"
//
//  Version 0-00
//
//  Version Summary:
//  0-00 released 04-21-2006: Initial release
//
//  J. Fahrenbruch
//  Texas Instruments Inc.
//  April 2006
//  Built with IAR Embedded Workbench Version: 3.41A
//*****************************************************************************
// THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR
// REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
// INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR
// COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE.
// TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET
// POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY
// INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR
// YOUR USE OF THE PROGRAM.
//
// IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
// CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY
// THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT
// OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM.
// EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF
// REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS
// OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF
// USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S
// AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF
// YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS
// (U.S.$500).
//
// Unless otherwise stated, the Program written and copyrighted
// by Texas Instruments is distributed as "freeware".  You may,
// only under TI's copyright in the Program, use and modify the
// Program without any charge or restriction.  You may
// distribute to third parties, provided that you transfer a
// copy of this license to the third party and the third party
// agrees to these terms by its first use of the Program. You
// must reproduce the copyright notice and any other legend of
// ownership on each copy or partial copy, of the Program.
//
// You acknowledge and agree that the Program contains
// copyrighted material, trade secrets and other TI proprietary
// information and is protected by copyright laws,
// international copyright treaties, and trade secret laws, as
// well as other intellectual property laws.  To protect TI's
// rights in the Program, you agree not to decompile, reverse
// engineer, disassemble or otherwise translate any object code
// versions of the Program to a human-readable form.  You agree
// that in no event will you alter, remove or destroy any
// copyright notice included in the Program.  TI reserves all
// rights not specifically granted under this license. Except
// as specifically provided herein, nothing in this agreement
// shall be construed as conferring by implication, estoppel,
// or otherwise, upon you, any license or other right under any
// TI patents, copyrights or trade secrets.
//
// You may not use the Program in non-TI devices.
//
//******************************************************************************

#include "MSP430x20x2.h"

#define LED1_ON P1DIR=0x0C;P1OUT=0x08
#define LED2_ON P1DIR=0xF8;P1OUT=0x10
#define LED3_ON P1DIR=0xEC;P1OUT=0x04
#define LED4_ON P1DIR=0xF8;P1OUT=0x08
#define LED5_ON P1DIR=0xF4;P1OUT=0x04
#define LED6_ON P1DIR=0xF4;P1OUT=0x10
#define LED7_ON P1DIR=0x7C;P1OUT=0x40
#define LED8_ON P1DIR=0xDC;P1OUT=0x80
#define LED9_ON P1DIR=0x7C;P1OUT=0x20
#define LED10_ON P1DIR=0xDC;P1OUT=0x40
#define LED11_ON P1DIR=0xBC;P1OUT=0x20
#define LED12_ON P1DIR=0xBC;P1OUT=0x80

const int MAXCOUNTS = 15;              // Measurements before entering LPM4
const int WINDOW = 0x17;               // Window variable for tilt calculation
int delay25ms = 25000;                 // 25ms delay = 25ms * 10^6
int delay450ms;                        // 450ms delay (Calculate this below)

void main(void)
{
  int xyarray[2];
  int whichled, oldled = 0;
  int oncounter = 0;
  int xoffset, yoffset, x, y;
  unsigned int vlo_counts;

  WDTCTL = WDTPW + WDTHOLD;            // Stop WDT
  BCSCTL1 = CALBC1_1MHZ;               // Set DCO to 1MHz
  DCOCTL = CALDCO_1MHZ;
  BCSCTL3 = LFXT1S_2 + XCAP_1;

  // Initalize the I/O
  P1OUT = 0x00;
  P1SEL = 0x03;
  P1DIR = 0xFC;
  P2SEL = 0x00;
  P2DIR = 0x40;
  P2IES = 0x80;
  P2REN = 0x80;
  P2OUT = 0x80;

  // Calibrate Time Constants
  CCTL0 = CM_1 + CCIS_1 + CAP;         // Rising edge, CAP, ACLK
  TACTL = TASSEL_2 + MC_2 + TACLR;
  while (!(CCIFG & CCTL0));            // Wait for capture
  CCTL0 &= ~CCIFG;                     // Capture occured, clear flag
  vlo_counts = TACCR0;                 // Save result
  while (!(CCIFG & CCTL0));            // Wait for capture
  TACCR0 = TACCR0 - vlo_counts;        // Calculate delta
  delay25ms = delay25ms/TACCR0;        // Set up 25ms delay offset
  delay450ms = delay25ms*18;           // Set up 450ms delay offset

  // Initalize ADC10
  ADC10CTL1 = INCH_1 + CONSEQ_1;
  ADC10CTL0 = ADC10ON + MSC + ADC10IE;
  ADC10DTC1 = 0x02;                    // 2 conversions
  ADC10AE = 0x03;                      // Specify active channels

  // Calibrate sensor
  P2OUT |= BIT6;                       // Turn the sensor on
  TACCR0 = delay25ms;                  // Setup delay
  TACTL = TASSEL_1 + MC_2 + TACLR;     // start timer with ACLK in cont mode
  TACCTL0 = CCIE;                      // Initalize TACTL0 Interrupts
  _BIS_SR(LPM3_bits + GIE);            // Enter LPM3 while ADXL powers up
  ADC10SA = (short)&xyarray[0];        // Load to SRAM
  ADC10CTL0 |= ENC + ADC10SC;          // Sampling and conversion start
  _BIS_SR(LPM3_bits + GIE);            // LPM0, ADC10_ISR will force exit
  yoffset = xyarray[1];
  xoffset = xyarray[0];

  // Cycle through all LEDs
  for (oncounter=0;oncounter<=11;oncounter++)
  {
    switch (oncounter)
    {
      case 0:  LED1_ON;  break;
      case 1:  LED2_ON;  break;
      case 2:  LED3_ON;  break;
      case 3:  LED4_ON;  break;
      case 4:  LED5_ON;  break;
      case 5:  LED6_ON;  break;
      case 6:  LED7_ON;  break;
      case 7:  LED8_ON;  break;
      case 8:  LED9_ON;  break;
      case 9:  LED10_ON; break;
      case 10: LED11_ON; break;
      case 11: LED12_ON; break;
    }
    TACCR0 = TACCR0 + delay25ms;       // TACCR0 + 25ms
    _BIS_SR(LPM3_bits + GIE);          // LPM3, Timer_A will force exit
  }

  // Main loop starts here
  for (;;)
  { P2OUT |= BIT6;                     // Turn the sensor on
    TACCR0 = TACCR0 + delay25ms;       // Setup delay (Validated)
    _BIS_SR(LPM3_bits + GIE);          // Enter LPM3 while ADXL powers up

    ADC10SA = (short)&xyarray[0];      // Initalize Data buffer start
    ADC10CTL0 |= ADC10SC + ENC;        // Start ADC10 conversion
    _BIS_SR(LPM3_bits + GIE);          // Enter LPM3, ADC10_ISR will force exit
    P2OUT &= ~BIT6;                    // Turn the sensor off

    // Determine which LED to Turn On
    if (xyarray[1] > xoffset) // +X
    {
      if (xyarray[0] > yoffset) //+Y
      { //+Y +X
        x = xyarray[0] - xoffset;
        y = xyarray[1] - yoffset;
        if (x > (y + WINDOW))
         {
           LED12_ON;
           whichled = 12;
         }
        else if ((y - WINDOW) <= x)
         {
           LED11_ON;
           whichled = 11;
         }
        else
         {
           LED10_ON;
           whichled = 10;
         }
      }
      else //-Y
      { //+X -Y
        x = xyarray[0] - xoffset;
        y = yoffset - xyarray[1];
        if (x < (y - WINDOW))
        {
          LED7_ON;
          whichled = 7;
        }
        else if (x >= (y + WINDOW))
        {
          LED9_ON;
          whichled = 9;
        }
        else
        {
          LED8_ON;
          whichled = 8;
        }
      }
    }
    else // -X
    {
      if (xyarray[0] > yoffset) //+Y
      { // -X +Y
        x = xoffset - xyarray[0];
        y = xyarray[1] - yoffset;
        if (x > (y + WINDOW))
          {
            LED3_ON;
            whichled = 3;
          }
        else if (x <= (y - WINDOW))
          {
            LED1_ON;
            whichled = 1;
          }
        else
          {
            LED2_ON;
            whichled = 2;
          }
      }
      else
      { //-X -Y
        x = xoffset - xyarray[0];
        y = yoffset - xyarray[1];
        if (x > (y + WINDOW))
          {
            LED6_ON;
            whichled = 6;
          }
        else if (x < (y - WINDOW))
          {
            LED4_ON;
            whichled = 4;
          }
        else
          {
            LED5_ON;
            whichled = 5;
          }
      }
    }

    TACCR0 = TACCR0 + delay25ms;       // Setup 25ms delay
    _BIS_SR(LPM3_bits + GIE);          // Wait in LPM3 with LEDs ON
    P1DIR = 0xFC;P1OUT = 0x00;         // Turn ALL LEDs OFF
    TACCR0 = TACCR0 + delay450ms;      // CCCR0 + 450ms
    _BIS_SR(LPM3_bits + GIE);          // Wait in LPM3

    // Enter LPM4 if same LED has flashed for > MAXCOUNTS
    if (whichled == oldled)
    {
      oncounter++;
      if (oncounter > MAXCOUNTS)
      {
        P1DIR = 0xFC;P1OUT = 0x00;     // Turn LEDs OFF
        P2IFG = 0x00;
        P2IE  = 0x80;
        _BIS_SR(LPM4_bits + GIE);      // No activity wait in LPM4 for button
        P2IE  = 0x00;
        oncounter = 0;
      }
    }
    else
    {
       oldled = whichled;
       oncounter = 0;
    }
  }// End for (;;)
}

// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
  ADC10CTL0 &= ~ADC10IFG;              // Clear ADC10IFG
  LPM3_EXIT;                           // Exit LPM3 on return
}

//Timer_A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void TIMERA0_ISR(void)
{
  LPM3_EXIT;                           // Exit LPM3 on return
}

#pragma vector=PORT2_VECTOR
__interrupt void PORT2_ISR(void)
{
  P2IFG = 0x00;
  LPM4_EXIT;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -