//-----------------------------------------------------------------------------
// 声明:外部引脚的脉冲输入CEX0为P0^0,
//-----------------------------------------------------------------------------
#include <c8051f410.h> // SFR declarations
#include"UART.h"
#include <stdio.h>
#define SYSCLK 24500000 // SYSCLK frequency in Hz
#define BAUDRATE 9600 // Baud rate of UART in bps
sfr16 PCA0CP0 = 0xFB; // PCA0 Compare Register Definition
unsigned int CLKNUM = 0;
float period;
void OSCILLATOR_Init (void);
void PORT_Init (void);
void PCA0_Init (void);
static unsigned int capture_period;
unsigned int counter;
void main (void)
{
PCA0MD = 0x00; // Disable watchdog timer
PORT_Init (); // Initialize crossbar and GPIO
OSCILLATOR_Init (); // Initialize oscillator
PCA0_Init (); // Initialize PCA0
UART0_Init ();
EA = 1;
while (1)
{
printf("CLKNUM= %d us\n",CLKNUM);
printf("period= %f us\n",period);
}
}
void OSCILLATOR_Init (void)
{
OSCICN = 0x87; // Set internal oscillator to run
// at its maximum frequency
CLKSEL = 0x00; // Select the internal osc. as
// the SYSCLK source
RSTSRC = 0x04; // enable missing clock detector(串口需要)
}
void PORT_Init (void)
{
XBR0 = 0x01; //串口通信使能
XBR1 = 0x41; // Route CEX0 to P0.0,
// Enable crossbar and weak pull-ups
}
void PCA0_Init (void)
{
PCA0CN = 0x00; // Stop counter; clear all flags
PCA0MD = 0x08; // Use 系统时钟 as time base
PCA0CPM0 = 0x21; // Module 0 = Rising Edge Capture Mode
// enable CCF flag.
EIE1 = 0x10; // Enable PCA interrupts
CR = 1; // Start PCA counter
}
void PCA0_ISR (void) interrupt 11
{
static unsigned int current_capture_value, previous_capture_value;
if (CCF0) // If Module 0 caused the interrupt
{
// CLKNUM++;
CCF0 = 0; // Clear module 0 interrupt flag.
current_capture_value = PCA0CP0; // Store most recent capture value
capture_period = current_capture_value - previous_capture_value; // Calculate capture period from last two values.
previous_capture_value = current_capture_value; // Update previous capture value with most recent info.
period = capture_period *4.0816326530612244897959183673469e-8;
}
else // Interrupt was caused by other bits.
{
PCA0CN &= ~0x86; // Clear other interrupt flags for PCA
}
}
|