📄 pca_pz_test.c
字号:
/***************************************************************
功能:实现用PCA2模块上下沿捕捉试验,脉冲信号由T3定时发出,P2.4输出
引线到P0.2输入(PCA2的入口口),PCA0、PCA1模块打开空闲不用(也可以用PCA0、PCA1)。
作者:ZDP
时间:2005-11-30
版本:V1.0
***************************************************************/
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051f020.h> // SFR declarations
#include <INTRINS.H>
sfr16 RCAP3 = 0x92; // Timer3 reload value
sfr16 TMR3 = 0x94; // Timer3 counter
#define SYSCLK 3062500 // approximate SYSCLK frequency in Hz
#define TR3 TMR3CN |=0x04 // T3 定时启动
#define TF3 TMR3CN &=~0x80 // 清T3 定时中断标致位
sbit LED = P2^4; // rad LED: '1' = ON; '0' = OFF
//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------
void PORT_Init (void);
void Timer3_Init (int counts);
void Timer3_ISR (void);
//------------------------------------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
void main (void) {
// disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;
PORT_Init ();
Timer3_Init (SYSCLK / 12 / 10); // Init Timer3 to generate interrupts
// at a 10 Hz rate.
EA = 1; // enable global interrupts
while (1) { // spin forever
}
}
//------------------------------------------------------------------------------------
// PORT_Init
//------------------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports
//
void PORT_Init (void)
{ XBR0 = 0x18; //交叉开关设置
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P2MDOUT |= 0x10; // enable P2.4 (LED) as push-pull output
PCA0CN = 0x40; //启动PCA计数
PCA0CPM2 = 0x31; //启用PCA2模块上下沿捕捉及中断使能
}
//------------------------------------------------------------------------------------
// Timer3_Init
//------------------------------------------------------------------------------------
//
// Configure Timer3 to auto-reload and generate an interrupt at interval
// specified by <counts> using SYSCLK/12 as its time base.
//
//
void Timer3_Init (int counts)
{
TMR3CN = 0x00; // Stop Timer3; Clear TF3;
// use SYSCLK/12 as timebase
RCAP3 = -counts; // Init reload values
TMR3 = 0xffff; // set to reload immediately
EIE2 |= 0x01; // enable Timer3 interrupts
EIE1 = 0x08;
TR3; // start Timer3
}
//------------------------------------------------------------------------------------
// Interrupt Service Routines
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// Timer3_ISR
//------------------------------------------------------------------------------------
// This routine changes the state of the LED whenever Timer3 overflows.
//
// NOTE: The SFRPAGE register will automatically be switched to the Timer 3 Page
// When an interrupt occurs. SFRPAGE will return to its previous setting on exit
// from this routine.
//
void Timer3_ISR (void) interrupt 14
{
TF3; // 清除 TF3
LED = ~LED; // T3定时改变I/O的高低电位,产生脉冲信号
}
void PCA_ISR (void) interrupt 9
{ PCA0CN &= 0x40; //清PCA2模块中断标致位
_nop_(); //在此设断点观察,P2.4的上、下沿变化时都会产生中断
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -