發新話題
打印

比較器的中斷處理

比較器的中斷處理

最近在研究比較器的使用
Sample code 提供的內容沒有使用到比較器的中斷

想請教一下比較器的中斷是如何執行的
是當比較器的CP0 OR CP1 有輸出的時候
中斷就會自己執行嗎?
或著和計時器的中斷雷同?
還是它有獨特之處?

請版主指導
謝謝

TOP

這裡只能回答你 Silicon Labs C8051Fxxx MCU 內建比較器的中斷原理,如下:
1. 當 CP0+ < CP0- 時,會發生中斷,此時 Falling-Edge Flag(CP0FIF) 會被設立
2. 當 CP0+ > CP0- 時,會發生中斷,此時 Rising-Edge Flag(CP0RIF) 會被設立
接著就看你程式怎麼運用這些中斷產生的後續處理。

TOP

最近依舊在研究比較器的使用
設定兩個port
一個接地
一個輸入5V
寫一個迴圈判斷如果比較器的中斷有執行
則LED開始閃爍

但比較器的中斷一直沒有執行
不知道是不是初始化的地方沒有設定好
還是用法錯誤


附上完整的程式碼
請版主指導
謝謝
// Include Files
//-----------------------------------------------------------------------------

#include <c8051F410.h>

//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define SYSCLK             24500000    // Clock speed in Hz

sbit LED    = P2^1;                    // Green Led


//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------

void OSCILLATOR_Init (void);           // Configure the system clock
void PORT_Init (void);                 // Configure the Crossbar and GPIO
void Comparator1_Init (void);          // Configure Comparator0
                     
void Timer0_Delay (unsigned int us);   // Used as a delay

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main (void)
{
   PCA0MD &= ~0x40;                    // Disable the watchdog timer

   OSCILLATOR_Init();                  // Initialize the system clock
   PORT_Init ();                       // Initialize crossbar and GPIO
   Comparator0_Init();                 // Initialize Comparator0
        EA = 1;
      while (1)
      {
                                 

         if(CPT1CN == 0xD2)
                 {
                 Timer0_Delay (30000);         // Delay
         LED = ~LED;
                 }                   // Flip LED state
      }
   // Last reset was due to a power-on reset or something else
  
}

//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// OSCILLATOR_Init
void OSCILLATOR_Init (void)
{
   OSCICN |= 0x07;                     // SYSCLK is internal osc.
}                                      // running at 24.5 MHz

//-----------------------------------------------------------------------------
// PORT_Init
void PORT_Init (void)
{
   P0SKIP   = 0x03;                    // Skip P0.0 and P0.1 in
                                       // crossbar assignment
   XBR0     = 0x00;                             
   XBR1     = 0x40;                    // Enable crossbar and weak pull-ups
   P0MDIN  =  0xFC;                   // Comparator 0 inputs (P0.0 and P0.1)
                                       // are analog inputs.
   P2MDOUT  = 0x02;                    // LED is push-pull output
}

//-----------------------------------------------------------------------------
// Comparator0_Init
//
// Return Value : None
// Parameters   : None
//
// This function configures the comparator to pins P0.0 and P0.1
//-----------------------------------------------------------------------------

void Comparator1_Init (void)
{
   CPT1CN = 0x00;
  
   

   CPT1CN = 0x82;                      // Comparator enabled
                                       // 10 mV negative hysteresis
   CPT1MX = 0x00;                      // P0.1 = Negative Input  
                                       // P0.0 = Positive Input
   CPT1MD = 0x11;

   EIE1 = 0x40;
}

//-----------------------------------------------------------------------------
// Timer0_Delay
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

void Timer0_Delay (unsigned int us)
{
   unsigned i;                         // Millisecond counter

   TCON  &= ~0x30;                     // STOP Timer0 and clear overflow flag
   TMOD  &= ~0x0f;                     // Configure Timer0 to 16-bit mode
   TMOD  |=  0x01;
   CKCON |=  0x04;                     // Timer0 counts SYSCLKs

   for (i = 0; i < us; i++)            // Count microseconds
   {         
      TCON &= ~0x10;                   // STOP Timer0
      TH0 = (-SYSCLK/1000000) >> 8;    // Set Timer0 to overflow in 1us
      TL0 = -SYSCLK/1000000;
      TCON |= 0x10;                    // START Timer0
      while (TF0 == 0);                // Wait for overflow
      TCON &= ~0x20;                   // Clear overflow indicator
   }
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

TOP

你的比較器中斷函式沒有寫阿? 當有中斷時會跳到中斷相量...

加上去看看,你可以用IDe Debug!

J.K

TOP

再請教一個問題,目前我使用F410的比較器1作為電壓檢測的方法
根據datasheet的說明,當CP1+ > CP1- 或CP1+ < CP1-
皆會生成中斷,那是在CP1+ > CP1-或CP1+ < CP1-的瞬間才會生成中斷嗎?
如果我的CP1+ 在初始化時設定成電壓大於 CP1+-
那這個時候中斷會生成嗎?

第二個問題是,CPT1MD    = 0x30;  這樣的寫法會不會有問題
目的是想要達到當電壓不管是大於或小於,中斷都會生成

實際測試下發現當CPT1MD    = 0x30;時,若電壓CP1+ > CP1-時中斷會生成
但如果經過一小段時間後,電壓降下來變成CP1+ < CP1- ;中斷並不會生成
//-----------------------------------------------------------------------------------------
比較器的程式碼

void Comparator_Init()
{
    int i = 0;
        CPT1CN    = 0x8A;                        //enable
        for (i = 0; i < 35; i++);  // Wait 10us for initialization
    CPT1CN    &= ~0x30;
        CPT1MX    = 0xA1;                        //+ P0.2;        -P2.5
        P0SKIP    = 0x04;
                P2SKIP    = 0x20;
                CPT1MD    = 0x10;                        //falling        P0.2 < P2.5
        //CPT1MD    = 0x22;                        //rising        P0.2 > P2.5
        EIE1 |= 0x40;
        //EIP1 |= 0x40;

}
//
void Comparator_ISR(void) interrupt 13
{
        if(CPT1CN == 0x9A)
        {
                LED = ~LED;
        }

}
//---------------------------------------------------------------------------------------

[ 本帖最後由 餅乾盒 於 2010-4-16 12:57 編輯 ]

TOP

發新話題