最近依舊在研究比較器的使用
設定兩個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
//-----------------------------------------------------------------------------