📄 main.c
字号:
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051f020.h> // SFR declarations
//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------
#define SYSCLK 2000000 // approximate SYSCLK frequency in Hz
sbit LED1 = P1^6; // green LED: '1' = ON; '0' = OFF
sbit LED2 = P3^5;
sbit LED3 = P3^6;
//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------
void PORT_Init (void);
void SYSCLK_Init ()
{
int i; // 延时计数器
OSCXCN = 0x67; // 开启外部振荡器24.000000MHz晶体
for (i=0; i < 256; i++) ; // 等待振荡器启振
while (!(OSCXCN & 0x80)); // 等待晶体振荡器稳定
OSCICN = 0x88; // 选择外部振荡器为系统时钟源并允许丢失
}
void PORT_Init (void)
{
//XBR2 = 0x40; // Enable crossbar and weak pull-ups
// XBR0 = 0x07;
// XBR1 = 0x80;
XBR2 = 0x40;
//P1MDOUT |= 0x40; // enable P1.6 (LED) as push-pull output
}
//------------------------------------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
void main (void) {
// disable watchdog timer
WDTCN = 0xde;
WDTCN = 0xad;
SYSCLK_Init ();
PORT_Init ();
//EX7 中断配置
P3IF|=0x80;
EIE2|=0x20;
//Timer2 mode0 config
CKCON&=~0x20;
T2CON=0x09;
TL2=0;
TH2=0;
ET2=1;
//enable interrupt
EA=1;
while(1);
}
//上升沿中断处理程序
void ex7_ISR(void)interrupt 19 using 1
{
P3IF&=~0x80;
//开启定时器
TR2=1;
}
//捕捉中断服务程序
void cap_ISR(void) interrupt 5 using 2
{
unsigned char temp;
EXF2=0;
TR2=0;
TL2=0;
TH2=0;
//RCAP2H RCAP2L
//1 1 0 0, 0 0 0 0, 0 0 0 0, 0 0 0 0
temp=RCAP2H;
if(temp&0x80==0x80)
LED1=0;
else
LED1=1;
if(temp&0x40==0x40)
LED2=0;
else
LED2=1;
if(temp&0x20==0x20)
LED3=0;
else
LED3=1;
//计算脉冲宽度
//根据结果显示
}
//step 1 用上升,开启定时器
//step 2 下降沿捕捉
//step 3 计算脉冲宽度,根据一定规则输出到LED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -