/////////////////////////////////////
// Generated Initialization File //
/////////////////////////////////////
#include "C8051F020.h"
unsigned long FRQ;
sfr16 PCA0CP0=0XFB;
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void PCA_Init()
{
PCA0CN = 0x40;
PCA0MD = 0x02;
PCA0CPM0 = 0x11;
PCA0CPM1 = 0x46;
PCA0CPH1 = 0x64;
}
void Port_IO_Init()
{
// P0.0 - CEX0 (PCA), Open-Drain, Digital
// P0.1 - CEX1 (PCA), Push-Pull, Digital
// P0.2 - Unassigned, Open-Drain, Digital
// P0.3 - Unassigned, Open-Drain, Digital
// P0.4 - Unassigned, Open-Drain, Digital
// P0.5 - Unassigned, Open-Drain, Digital
// P0.6 - Unassigned, Open-Drain, Digital
// P0.7 - Unassigned, Open-Drain, Digital
// P1.0 - Unassigned, Open-Drain, Digital
// P1.1 - Unassigned, Open-Drain, Digital
// P1.2 - Unassigned, Open-Drain, Digital
// P1.3 - Unassigned, Open-Drain, Digital
// P1.4 - Unassigned, Open-Drain, Digital
// P1.5 - Unassigned, Open-Drain, Digital
// P1.6 - Unassigned, Open-Drain, Digital
// P1.7 - Unassigned, Open-Drain, Digital
// P2.0 - Unassigned, Open-Drain, Digital
// P2.1 - Unassigned, Open-Drain, Digital
// P2.2 - Unassigned, Open-Drain, Digital
// P2.3 - Unassigned, Open-Drain, Digital
// P2.4 - Unassigned, Open-Drain, Digital
// P2.5 - Unassigned, Open-Drain, Digital
// P2.6 - Unassigned, Open-Drain, Digital
// P2.7 - Unassigned, Open-Drain, Digital
// P3.0 - Unassigned, Open-Drain, Digital
// P3.1 - Unassigned, Open-Drain, Digital
// P3.2 - Unassigned, Open-Drain, Digital
// P3.3 - Unassigned, Open-Drain, Digital
// P3.4 - Unassigned, Open-Drain, Digital
// P3.5 - Unassigned, Open-Drain, Digital
// P3.6 - Unassigned, Open-Drain, Digital
// P3.7 - Unassigned, Open-Drain, Digital
P0MDOUT = 0x02;
XBR0 = 0x10;
XBR2 = 0x40;
}
void CloseWDT(void)//系统配置
{
WDTCN = 0xDE;// 关闭 WDT
WDTCN = 0xAD; //关闭看门狗
}
void Oscillator_Init()
{
OSCICN = 0x07;
}
void getfrq(void) interrupt 9//捕获下降沿中断,第一次捕捉把PCA0计数器清空,第二次捕捉时根据PCA0CP0中存放的PCA0的值乘以时间就是周期,
{
EA=0;
PCA0L=0x00;
PCA0H=0x00;
FRQ=1000000.0/(PCA0CP0*0.25);//16MHZ,1/4f,t=0.25us//我测出两次下降沿的时间,即一个周期的时间,此时系统走了计数器*1/4MHZ(秒)
EA=1;
CCF0=0;
}
// Initialization function for device,
// Call Init_Device() from your main program
void Init_Device(void)
{ CloseWDT();
Oscillator_Init();
PCA_Init();
Port_IO_Init();
}
void main(void)
{
Init_Device();
EA=1;
while(1);
}
用Configuration Wizard生成的程序,自己加了看门狗关闭和中断程序,PCA0开启,CEX0用于测频捕捉,CEX1产生20Khz的信号且推挽输出,用线把这两个直接连起来,但是调试的时候FRQ的值始终为0 |