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

📄 f4270_altimeter_sb_iar.c

📁 德州仪器的msp430单片机
💻 C
📖 第 1 页 / 共 3 页
字号:
//------------------------------------------------------------------------------
// MSP430F4270 Altimeter Demo - C Language Version - SoftBaugh LCD
//
// Description:
//   PRESSURE SENSOR IS SAMPLED/AVG'D 64X WITH OSR=1024, fSD16=32kHz, 16-bit
//   TEMP SENSOR IS SAMPLED/AVG'D 1X WITH OSR=1024, fSD16=32kHz, 16-bit
//
//                  MSP430F4270
//               +---------------+     SoftBaugh SBLCDA4 LCD
//               |               |    +----------------------+
//   IN+     o---|A0+      S0-Sxx|--->|                      |
//   IN-     o---|A0-   COM0-COM3|--->|            4-Mux LCD |
//   VBridge o--+|P6.6           |    +----------------------+
//              +|P6.7           |
//           o---|VRef           |
//               |               |
//               |       XIN/XOUT|<---32.768KHz Watch Crystal
//               |           P1.0|<---Button (low-active)
//               |           P1.1|<---Button (low-active)
//               +---------------+
//
// Andreas Dannenberg
// Texas Instruments Inc.
// June 23rd, 2005
// Built with IAR Embedded Workbench Version: 3.30A
//------------------------------------------------------------------------------
// 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 <math.h>
#include "msp430x42x0.h"

// Circuit related definitions
#define BRIDGE_SUPPLY       (0xc0)              // IO pins P6.6/P6.7 for
                                                // pos. bridge rail
#define PUSH_BUTTON1        (0x01)              // Button on pin P1.0
#define PUSH_BUTTON2        (0x02)              // Button on pin P1.1

// Constants for math calculations (for altitude/pressure measurement)
#define VREF_VOLT_EXT   (1.26923077f)           // External ref voltage
#define GAIN_32         (28.35f)                // Typ. gain for 32x
#define NR_BITS         (32767)                 // 15 bits of resolution
#define UV_PER_LSB      (1000000.0f * VREF_VOLT_EXT / 2 / GAIN_32 / NR_BITS)
#define MB_PER_UV       (10.0f / 120.0f)        // 10mbar / 120uV @ V_Bridge = 3V
#define NORMAL_P        (1013.25f)              // Normal air pressure in mbar

// Constants for math calculations (for temperature measurement)
#define VREF_VOLT_INT   (1.20f)                 // Internal ref voltage
#define MV_PER_LSB      (1000.0f * VREF_VOLT_INT / 2 / 32767)
#define K_PER_MV        (1.0f / 1.32f)

// Misc definitions
#define MODE_TIMEOUT    (240)                   // Max time for A/P measurement
                                                // (in s)

enum
{
  PM_MEASURE_A,                                 // Mode - Measure Altitude
  PM_MEASURE_P,                                 // Mode - Measure Pressure
  PM_DISPLAY_TIME,                              // Mode - Display Time
  PM_MEASURE_TEMP,                              // Mode - Measure Temperature
  PM_DISPLAY_CONTR,                             // Mode - Display Contrast
  PM_CAL_A,                                     // Mode - Cal Altitude
  PM_CAL_P,                                     // Mode - Cal Pressure
  PM_SET_TIME,                                  // Mode - Set Time
  PM_CAL_TEMP,                                  // Mode - Cal Temperature
  PM_SET_CONTR                                  // Mode - Set Contrast
};

// LCD segment definitions (SoftBaugh SBLCDA4)
#define SEG_D   0x80                            //  AAAA
#define SEG_C   0x40                            // F    B
#define SEG_B   0x20                            // F    B
#define SEG_A   0x10                            //  GGGG
#define SEG_H   0x08                            // E    C
#define SEG_E   0x04                            // E    C
#define SEG_G   0x02                            //  DDDD
#define SEG_F   0x01

const char LCD_Tab[] = {
  SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F,          // Displays "0"
  SEG_B + SEG_C,                                          // Displays "1"
  SEG_A + SEG_B + SEG_D + SEG_E + SEG_G,                  // Displays "2"
  SEG_A + SEG_B + SEG_C + SEG_D + SEG_G,                  // Displays "3"
  SEG_B + SEG_C + SEG_F + SEG_G,                          // Displays "4"
  SEG_A + SEG_C + SEG_D + SEG_F + SEG_G,                  // Displays "5"
  SEG_A + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G,          // Displays "6"
  SEG_A + SEG_B + SEG_C,                                  // Displays "7"
  SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F + SEG_G,  // Displays "8"
  SEG_A + SEG_B + SEG_C + SEG_D + SEG_F + SEG_G,          // Displays "9"
  SEG_A + SEG_B + SEG_C + SEG_E + SEG_F + SEG_G,          // Displays "A"
  SEG_B + SEG_C + SEG_E + SEG_F + SEG_G,                  // Displays "H"
  SEG_A + SEG_D + SEG_E + SEG_F,                          // Displays "C"
  SEG_D + SEG_E + SEG_F,                                  // Displays "L"
  SEG_A + SEG_B + SEG_E + SEG_F + SEG_G,                  // Displays "P"
  0x00                                                    // Displays Blank
};

#define DIG_MINUS   (SEG_G)                               // Displays '-'
#define DIG_T       (SEG_D + SEG_E + SEG_F + SEG_G)       // Displays 't'
#define DIG_DEGR    (SEG_A + SEG_B + SEG_F + SEG_G)
#define DIG_F       (SEG_A + SEG_E + SEG_F + SEG_G)       // Displays 'F'
#define DIG_L       (SEG_D + SEG_E + SEG_F)               // Displays 'L'
#define DIG_O       (SEG_A + SEG_B + SEG_C + SEG_D + SEG_E + SEG_F) // Displays 'O'
#define DIG_H       (SEG_B + SEG_C + SEG_E + SEG_F + SEG_G)         // Displays 'H'
#define DIG_I       (SEG_B + SEG_C)                          // Displays 'I'
#define DIG_C       (SEG_A + SEG_D + SEG_E + SEG_F)          // Displays 'C'
#define DIG_D       (SEG_B + SEG_C + SEG_D + SEG_E + SEG_G)  // Displays 'd'
#define DIG_N       (SEG_C + SEG_E + SEG_G)                  // Displays 'n'
#define DIG_E       (SEG_A + SEG_D + SEG_E + SEG_F + SEG_G)  // Displays 'E'
#define DIG_S       (SEG_A + SEG_C + SEG_D + SEG_F + SEG_G)  // Displays 'S'

// Global vars for SD16 operation
static unsigned int SD16TempCtr;                // Number of resuts collected
static long SD16Temp;                           // Temp sum register
static long SD16Result;                         // Final averaged result
static unsigned int SD16NrConversions;

// Global vars that control program flow
static unsigned char ProgramMode = PM_DISPLAY_TIME; // Current program mode
static unsigned char PB1DownCtr = 0;            // Keeps track of button press
static unsigned char PB2DownCtr = 0;            // duration
static unsigned int StatusFlags = 0;            // Status flag register
static unsigned char ModeCtr = 0;               // Time in current mode [s]

#define SF_BT_TICK        0x0001                // Status flags bit definitions
#define SF_PB1_RELEASED   0x0002
#define SF_PB2_RELEASED   0x0004
#define SF_SD16_START     0x0008
#define SF_SD16_READY     0x0010
#define SF_UPD_DISPLAY    0x0020
#define SF_PB1_PRESSED    0x0040
#define SF_PB2_PRESSED    0x0080
#define SF_PB1_DOWN       0x0100
#define SF_PB2_DOWN       0x0200
#define SF_HOLD_DISPL     0x0400

#define AF_DISPLAY_DEGF   0x01

static int CalConstA;                           // Temporary cal vars
static int CalConstP;
static int CalConstT;
static char DispContr;                          // Display contrast setting
static char AppFlags;                           // Misc application flags

// Global vars for RTC
static unsigned char Secs = 0x00;               // RTC, default time 12:00:00
static unsigned char Mins = 0x00;
static unsigned char Hrs = 0x12;

#pragma dataseg = INFOA                         // Info Flash Memory Block A
__no_init static int CalConstA_Flash;           // Cal const for altitude
__no_init static int CalConstP_Flash;           // Cal const for pressure
__no_init static int CalConstT_Flash;           // Cal const for temperature
__no_init static char DispContr_Flash;          // Const for display contrast
__no_init static char AppFlags_Flash;           // Misc application flags
#pragma dataseg = default

// Function prototypes
void Init_Sys(void);
void RTC_Tick(unsigned int TickMins);
void RTC_Dec(unsigned int TickMins);
void InitConversion(void);
void StartNextConversion(void);
void StopConversion(void);
void StoreCalInFlash(void);
void Disp_Value(unsigned int ShiftLeft, int Value);
void Disp_BCD(unsigned long Value);
//------------------------------------------------------------------------------
void main(void)
{
  Init_Sys();

  CalConstA = CalConstA_Flash;                  // Load calibration constants
  CalConstP = CalConstP_Flash;
  CalConstT = CalConstT_Flash;
  DispContr = DispContr_Flash == 0xff ? 0 : DispContr_Flash;
  AppFlags = AppFlags_Flash;

  __enable_interrupt();

  Disp_BCD(0x08888888);                         // Test LCD (all segs on)

  while (1)
  {
    __bis_SR_register(LPM3_bits);               // Enter LPM3, wait for wakeup

    if (StatusFlags & SF_BT_TICK)
    {
      RTC_Tick(0);                              // Advance RTC seconds

      switch (ProgramMode)
      {
        case PM_MEASURE_A :
        case PM_MEASURE_P :
          if (++ModeCtr > MODE_TIMEOUT)         // Timeout?
          {
            StopConversion();
            ProgramMode = PM_DISPLAY_TIME;      // Exit current mode
            StatusFlags |= SF_UPD_DISPLAY;      // Update RTC display
          }
          break;
        case PM_CAL_A  :
        case PM_CAL_P  :
                                                // No action here. Next SD16
                                                // conversion is started
                                                // directly after current.
          break;
        case PM_MEASURE_TEMP :
        case PM_CAL_TEMP :
          StatusFlags |= SF_SD16_START;         // Trigger SD16 conversion
          break;
        case PM_DISPLAY_TIME :
        case PM_SET_TIME :
          StatusFlags |= SF_UPD_DISPLAY;        // Update RTC display
          break;
      }
      StatusFlags &= ~SF_BT_TICK;               // Event processed
    }

    if (((StatusFlags & (SF_PB1_PRESSED + SF_PB2_PRESSED)) ==
        SF_PB1_PRESSED + SF_PB2_PRESSED) && (PB1DownCtr > 62) &&
        (PB2DownCtr > 62))

    // Both buttons pressed for >1s
    {
      switch (ProgramMode)                      // Yes, update Program Mode
      {
        case PM_MEASURE_A :
          ProgramMode = PM_CAL_A;
          Disp_BCD(0x0fcadfff);                 // Display 'CAL    '
          StatusFlags |= SF_HOLD_DISPL;         // Hold display contents
          break;
        case PM_MEASURE_P :
          ProgramMode = PM_CAL_P;
          Disp_BCD(0x0fcadfff);                 // Display 'CAL    '
          StatusFlags |= SF_HOLD_DISPL;         // Hold display contents
          break;
        case PM_DISPLAY_TIME :
          ProgramMode = PM_SET_TIME;
          LCDM1 = 0x00;                         // Display 'SEt    '
          LCDM2 = DIG_S;
          LCDM3 = DIG_E;
          LCDM4 = DIG_T;
          LCDM5 = 0x00;
          LCDM6 = 0x00;
          LCDM7 = 0x00;
          StatusFlags |= SF_HOLD_DISPL;         // Hold display contents
          break;
        case PM_MEASURE_TEMP :
          ProgramMode = PM_CAL_TEMP;
          Disp_BCD(0x0fcadfff);                 // Display 'CAL    '
          StatusFlags |= SF_HOLD_DISPL;         // Hold display contents
          break;

⌨️ 快捷键说明

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