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

📄 test44x_ca04.c

📁 ADC12操作 DATA FLASH操作 EEPROM操作 FLASH读写操作 LCD点阵操作 TIMER_A_操作 Timer_B操作 比较器A操作 端口操作 基
💻 C
字号:
#include  "msp430x44x.h"
//******************************************************************************
//  MSP430-TEST44X Demo - Comp_A Slope ADC to Detect Temp Level Set P5.1 > 25c
//
//  Description: Comparator_A is used to detect a resistance threashold.
//  Discharge times of a 0.1uf capacitor through a 10k-NTC (25c) and 10k ohm
//  reference resistor are compared.  If the NTC discharge time is is lower
//  than the 10k referece, P5.1 is set.  If the NTC discharge time is higher
//  than refernce, P5.1 is reset.  The LED is ON if the temperature is greater
//  than 25c.
//  ACLK = n/a, MCLK = SMCLK = default DCO =1M
//
//               MSP430F449
//            -----------------
//       /|\ |              XIN|-
//        |  |                 |
//        ---|RST          XOUT|-
//           |                 |
//     +-10k-|P1.0             |
//     |     |                 |	
//     +-NTC-|P1.7             |
//     |     |                 |
//     +-----|P1.6         P5.1|-->LED
//     |     |                 |
//    ===.1uf|                 |
//     |     |                 |			
//     ------|VSS
//
//           NTC = 10k @25c, (P/N 271-110A Radio Shack)
//
unsigned int sensor_time;
unsigned int ref_time;
unsigned int timer_count;
unsigned int measure(int);  // measures the time to discharge the capacitor

#define Ref     (0x01)      // P1.0 = Reference
#define Sensor  (0x80)      // P1.7 = Sensor
//
//  Lierda, Inc
//  February 2006.03
//  Built with IAR Embedded Workbench Version: 3.40A
//  ID:  MSP430-TEST44X
//  DATA:2005-7-12
//  REV: 2.0A
//
//******************************************************************************

void main (void)
{
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  P5OUT &= ~0x02;                           // P5.1 reset
  P5DIR |= 0x02;                            // P5.1 output direction
  P1OUT &= ~(Ref + Sensor);                 // Ref set
  P1DIR |= Ref + Sensor;                    // Ref output
  CACTL1 = CARSEL + CAREF0 + CAON;          // -Comp = 0.25*Vcc - on
  CACTL2 = P2CA0;                           // +Comp = 1.6
  TACTL = TASSEL_2 + MC_2;                  // SMCLK, clear TAR
  _EINT();                                  // Enable interrupts

  while (1)                                 // Mainloop
  {
     sensor_time = measure(Sensor);         // Measure discharge time through sensor
     ref_time = measure(Ref);               // Measure discharge time through Ref
     if (sensor_time < ref_time)            // if sensor time < ref time ( >25c )
      P5OUT |= 0x02;                        // LED on - Set P5.1
     else P5OUT &= ~0x02;                   // LED off - Reset P5.1
  }
}

unsigned int measure(int source) {
  P1OUT |= Ref;                             // Ref Set
  P1DIR |= Ref;                             // Ref output
  CCR1 = TAR + 5000;                        // CCR1 ~ TAR+5ms
  CCTL1 = CCIE;                             // Comp,interrupt
  LPM0;                                     // Wait for CCR1 interrupt
  P1DIR &= ~Ref;                            // Ref = HiZ, Charge complete
  P1OUT &= ~Ref;                            // Ref = Reset
  CCTL1 = CM_2 + CCIS_1 + CAP + CCIE;       // Neg, CCIB,Cap,interrupt
  timer_count = TAR;                        // TAR at before discharge
  P1DIR |= source;                          // Temp = Sensor or Ref
  LPM0;                                     // Wait for CCR1 interrupt
  timer_count = CCR1 - timer_count;         // timer_count = discharge time
  P1DIR &= ~(Sensor + Ref);                 // Disable Sensor or Ref
  CCTL1 = 0x00;                             // Disable CCTL1
  return(timer_count);
}

#pragma vector=TIMERA1_VECTOR
__interrupt void Timer_A (void)
{
 switch( TAIV )
 {
   case  2: LPM0_EXIT;                      // ccr1
            break;
 }
}

⌨️ 快捷键说明

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