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

📄 fc430_es417_iar.c

📁 lcdcdode cor customized
💻 C
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
//  FC430 ES417 Demo - Digital Fan Control w/ Tachometer (IAR EW430 C Version)
//
//  Description: Drives cooling fan speed based on thermistor temperature
//  Results: Displays tachometer (RPM) and temperature (deg F) on SoftBaugh
//  SBLCDA4 LCD (ACLK = UCLK0 = LFXT1 = 32.768 kHz)
//  (MCLK = SMCLK = 32*ACLK = DCO = ~1.048576 MHz)
//  NOTE: external watch crystal is required on XIN XOUT for ACLK)	
//
//                  MSP430F417
//           ------------------------
//      /|\  |                   XIN|--- |
//       |   |                      |  32kHz
//       ----|RST               XOUT|--- |
//           |                      |
//   LED <---|P1.0        P2.1/TA1.1|---> PWM
//           |                      |
// Rsens --->|P1.4              P2.0|<--- TACH
//           |                      |
//  Rref --->|P1.5                  |
//           |                      |
//    Cm --->|P1.6/CA0              |
//           |                      |
//           |                      |
//           |  P2.2 -- P4.4, P5.1  |
//           ------------------------
//           | | | | | | | | | | | |
//           v v v v v v v v v v v v
//           ------------------------
//           |  SBLCDA4 LCD DISPLAY |
//           ------------------------
//
//
// Randy Wu
// Texas Instruments Inc.
// November 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  <msp430x417.h>
#include  "fc430_es417.h"

const char LCD_Table[] = {
  a+b+c+d+e+f,                              // Displays "0"
  b+c,                                      // Displays "1"
  a+b+d+e+g,                                // Displays "2"
  a+b+c+d+g,                                // Displays "3"
  b+c+f+g,                                  // Displays "4"
  a+c+d+f+g,                                // Displays "5"
  a+c+d+e+f+g,                              // Displays "6"
  a+b+c,                                    // Displays "7"
  a+b+c+d+e+f+g,                            // Displays "8"
  a+b+c+d+f+g,                              // Displays "9"
  a+b+c+e+f+g,                              // Displays "A"
  0x00,                                     // Displays  Blank
  a+d+e+f,                                  // Displays "C"
  a+b+f+g,                                  // Displays "degrees" o
  a+d+e+f+g,                                // Displays "E"
  a+e+f+g,                                  // Displays "F"
  h,                                        // Displays ":" or "."
  g,                                        // Displays "-"
  f+g+e+d                                   // Displays "t"
};

#undef a
#undef b
#undef c
#undef d
#undef e
#undef f
#undef g
#undef h

                                            // Negative Temperature Table:
                                            // Thermistor resistance values
                                            // Each already divided by 100 and
                                            // must be modified for thermistor
//const unsigned int resTabNegativeC[NEGRANGE] =
//{6860, 6385, 5946, 5539, 5163, 4816, 4493, 4195, 3918, 3661, 3423, 3201,
//2996, 2805, 2627, 2462, 2308, 2165, 2030, 1907, 1791, 1682, 1581, 1487,
//1399, 1317,1240, 1168, 1100, 1037, 978, 923, 871, 822, 777, 734, 694,
//656, 621, 587, 556, 526, 499, 473, 448, 425, 403, 382, 363, 345};

                                            // Positive Temperature Table:
                                            // Thermistor resistance values
                                            // Each already divided by 100 and
                                            // must be modified for thermistor
const unsigned int resTabPositiveC[POSRANGE] =
{311, 296, 281, 267, 254, 242, 231, 220, 210, 200, 190, 181, 173, 165,
157, 150, 143, 137, 131, 125, 120, 114, 109, 105, 100, 96, 92, 88, 84,
81, 77, 74, 71, 68, 65, 63, 60, 58, 55, 53, 51, 49, 47, 45, 44, 42,
40, 39, 37, 36, 35, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23,
22, 22, 21, 20, 19, 19, 18, 18, 17, 16, 16, 15, 15, 14, 14,
13, 13, 13, 12, 12, 11, 11, 11, 10, 10, 10, 10, 9, 9, 9,
8, 8, 8, 8, 7, 7, 7};

FanLevel level = FANSLEVEL1;                // Cooling level
Status status;                              // Alarm status of system
unsigned int tachCount = 0;                 // Count revolutions of fan
unsigned int tachRPM = 0;                   // Stores RPM of fan
unsigned int lastRPM = 0;                   // Stores RPM of last reading
signed int currentDegC = 0;                 // Stores temp (degC) of system
signed int currentDegF = 0;                 // Stores temp (degF) of system
                                            // Duty cycles for each level
unsigned char dutyCycles[ MAXLEVELSETTINGS] =
  { L0DUTYCYCLE, L1DUTYCYCLE, L2DUTYCYCLE,
    L3DUTYCYCLE, L4DUTYCYCLE, L5DUTYCYCLE};
static char *LCD = LCDMEM;                  // Pointer to the LCD memory
int unitMode = ENGLISH;                     // Units of the system


void main(void)
{
   SYS_init();                              // Initialize the SYStem
   LCD_init();                              // Initialize the LCD
   PWM_init();                              // Initialize the PWM Generator
   TEMP_init();                             // Initialize TEMP sensing

  _EINT();                                  // Enable Global Interrupts

  for (;;)
  {
    _BIS_SR(LPM0_bits + GIE);               // Drop into Low Power Mode 0

    // *** Watchdog Interval Timer wakes us up here ***
    refreshTach();                          // Update Tachometer
    sampleTemp();
    displayTemp();                          // Update temperature on LCD
    setFanSpeed();                          // Set fan speed
    displayLevel(level);                    // Display current level
  }
}

void TEMP_init()
{
  P1SEL &= ~(REF+THERM);                    // Select I/O function
  P1DIR |=  (REF+THERM);                    // Change R(ref) pin for output
  P1OUT &= ~(REF+THERM);                    // Clear resistor outputs
  CAPD = (REF+THERM+CA_IN);                 // Disable sensor inputs
  CACTL2 = P2CA0;                           // Set up comparator
}

signed int cnvtTempDegC(long int rSENSE)
{
  signed int tempDegC;
  int        i = 0, k = 1;

  rSENSE /= 100;                            // Convert resistance value
                                            // Before traversing tables
  if ( (rSENSE < 326) && (rSENSE >= resTabPositiveC[POSRANGE-1]) ){
    while(( resTabPositiveC[i] - rSENSE) > 0) {
                                            // Use positive temp table
      i++;                                  // Increment to next value
      k++;                                  // k = value of temperature
    }
    tempDegC = k + 1;                       // Temperature in deg C
  }
  else if (rSENSE >= 326) {// Temperature is zero deg C
    k = 0;                                  // Thermistor temp is 0
    tempDegC = k;                           // Set temp variable to 0
  }
  else
    tempDegC = OUTOFRANGE;                  // rSENSE is out of range

  return (tempDegC + CALFACTORDEGC);        // Return temperature
}

unsigned int measureCount(int temperatureSource)
{
  unsigned int TIMERCCR_COUNT;              // Temporary holder for CCR count
                                            // On measure routine
  unsigned int TIMERA_COUNT;                // Temporary holder for TA
                                            // Discharge count on measure
                                            // Capacitor is charged up with
                                            // Reference resistor
  P1OUT |= REF;                             // Ref set
  CAPD &= ~REF;                             // Must be enabled for output func
  CCR1 = PERIOD5MSEC;                       // CCR1 ~ TAR+5ms (5tu)
  CCTL1 = CCIE;                             // Compare mode, interrupt
  TA0CTL = TASSEL1+TACLR+MC1;               // SMCLK, clear, continuous
  LPM0;                                     // Wait for CCR1 interrupt
  CAPD |= REF;                              // Input buffer disabled for
                                            // tri-state
  P1OUT &= ~REF;                            // Ref = reset
                                            // Measuring discharge time
  CACTL1 = CARSEL+CAREF0+CAON;              // -comp = 0.25*Vcc, comp on
  TA0CCTL1 = CM1+CCIS0+CAP+CCIE;            // Neg, CCIB, cap, interrupt
  TIMERA_COUNT = TAR;                       // Store TAR count in temporary
                                            // holder at start of conversion
  CAPD &= ~temperatureSource;               // Must be enabled for output
                                            // function
  LPM0;                                     // Wait for CCR1 interrupt
  TIMERCCR_COUNT = CCR1;                    // Save TAR (CCR1) at EOC
  TIMERCCR_COUNT -= TIMERA_COUNT;           // Calculate discharge time
                                            // count
  CAPD |= temperatureSource;                // Input buffer disabled for
                                            // tri-state
  CACTL1 = 0x00;                            // Disable comparator
  TA0CCTL1 = 0x00;                          // Disable CCTL1
  TA0CTL = 0x00;                            // Disable timer_A
  P2IE |= (TACH);                           // Re-enable tachometer interrupt
  return(TIMERCCR_COUNT);                   // Return discharge time count
}

void sampleTemp(void)
{
  long int tREF, tSENSE;
  float    rTHERM;

  tREF = measureCount(REF);                 // R(ref) discharge count
  tSENSE = measureCount(THERM);             // R(sense) discharge count
                                            // Calculate thermistor resist.
  rTHERM = SLOPEADrREF*((float)tSENSE/tREF);
  currentDegC = cnvtTempDegC(rTHERM);       // Convert Rtherm to temp
  currentDegF = ((((9.0/5.0) *              // Convert degC to degF
                (currentDegC)) + 32.0) + CALFACTORDEGF);
}

void PWM_init(void)
{
  P2DIR |= PWM;                             // Configure output direction
  P2SEL |= PWM;                             // Select peripheral option
  TA1CCR0 = PERIOD050USEC;                  // Set up the PWM Period
  TA1CTL = TASSEL_2 + MC_1;                 // Use SMCLK, count up mode
  TA1CCTL1 = OUTMOD_1;                      // Keep fan(s) off initially
}

void changeLevelAndSpeed(FanLevel systemLevel)
{
  float dutyCycle;

  level = systemLevel;                      // Set to the desired level
                                            // Convert duty cycle to a %
  dutyCycle = ((float)(dutyCycles[level]))/100;

  if (dutyCycle > 0.99)
      TA1CCTL1 = OUTMOD_5;                  // Output constant LOW signal
  else {
    TA1CCR1 = (dutyCycle * PERIOD050USEC);  // Update the compare register
    TA1CCTL1 = OUTMOD_3;                    // Generate PWM via out mode 3
  }
}

void setFanSpeed(void)
{
   FanLevel level;

  // Update current fan speed based on current temperature reading
  if (currentDegF <= L0)                    // Check if below range
    return;
  else if (currentDegF <= L1toL2)
    level = FANSLEVEL1;                     // Currently in L1
  else if (currentDegF <= L2toL3)
    level = FANSLEVEL2;                     // Currently in L2
  else if (currentDegF <= L3toL4)
    level = FANSLEVEL3;                     // Currently in L3
  else if (currentDegF <= L4toL5)
    level = FANSLEVEL4;                     // Currently in L4
  else
    level = FANSLEVEL5;                     // Currently in L5

   changeLevelAndSpeed(level);              // Update PWM and speed
}

void LCD_init()
{
  P3SEL = 0xFF;                             // Set segment outputs for LCD
  P3DIR = 0xFF;                             // Set output direction
  P4SEL = 0xFF;                             // Set segment outputs for LCD
  P4DIR = 0xFF;                             // Set output direction
  P5SEL = 0xFC;                             // Set Rxx and COM pins for LCD
  P5DIR = 0xFF;                             // Set output direction

⌨️ 快捷键说明

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