本帖最后由 FSL_TICS_A 于 2014-4-1 10:06 编辑
请教各位老师,这里的
time1 = time2;
time2 = ECT_TC0;
delaytime = time2-time1;
有什么作用?(XET256MAL)
/*************************************************************/
/* 输入捕捉中断函数 */
/*************************************************************/
#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt void capture(void)
{
if(ECT_T标志寄存器1_C0F == 1)
ECT_T标志寄存器1_C0F = 1;
time1 = time2;
time2 = ECT_TC0;
delaytime = time2-time1;
LEDCPU = ~LEDCPU;
}
#pragma CODE_SEG DEFAULT
全部的程序代码:
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#define LEDCPU PORTK_PK4
#define LEDCPU_dir DDRK_DDRK4
#define BUS_CLOCK 32000000 //总线频率
#define OSC_CLOCK 16000000 //晶振频率
unsigned int delaytime;
unsigned int time1;
unsigned int time2;
/*************************************************************/
/* 初始化锁相环 */
/*************************************************************/
void INIT_PLL(void)
{
CLKSEL &= 0x7f; //set OSCCLK as sysclk
PLLCTL &= 0x8F; //Disable PLL circuit
CRGINT &= 0xDF;
#if(BUS_CLOCK == 40000000)
SYNR = 0x44;
#elif(BUS_CLOCK == 32000000)
SYNR = 0x43;
#elif(BUS_CLOCK == 24000000)
SYNR = 0x42;
#endif
REFDV = 0x81; //PLLCLK=2×OSCCLK×(SYNR+1)/(REFDV+1)=64MHz ,fbus=32M
PLLCTL =PLLCTL|0x70; //Enable PLL circuit
asm NOP;
asm NOP;
while(!(CRG标志寄存器&0x08)); //PLLCLK is Locked already
CLKSEL |= 0x80; //set PLLCLK as sysclk
}
/************************************************************/
/* 初始化ECT模块 */
/************************************************************/
void initialize_ect(void){
ECT_TSCR1_TFFCA = 1; // 定时器标志位快速清除
ECT_TSCR1_TEN = 1; // 定时器使能位. 1=允许定时器正常工作; 0=使主定时器不起作用(包括计数器)
ECT_TIOS = 0xfe; //指定通道0为输入捕捉方式
ECT_TCTL4 = 0x03; // 设置通道0为捕捉下降沿方式
ECT_DLYCT = 0x00; // 延迟控制功能禁止
ECT_ICOVW = 0x00; // 对应的寄存器允许被覆盖; NOVWx = 1, 对应的寄存器不允许覆盖
ECT_ICSYS = 0x00; // 禁止IC及PAC的保持寄存器
ECT_TIE = 0x01; // 使能通道0定时中断
ECT_TSCR2 = 0x07; // 预分频系数pr2-pr0:111,,时钟周期为4us,
ECT_T标志寄存器1 = 0xff; // 清除各IC/OC中断标志位
ECT_T标志寄存器2 = 0xff; // 清除自由定时器中断标志位
}
/************************************************************/
/* 延时程序 */
/************************************************************/
void delay(void)
{
unsigned int i,j;
for(j=0;j<50;j++) {
for(i=0;i<50000;i++);
}
}
/*************************************************************/
/* 输入捕捉中断函数 */
/*************************************************************/
#pragma CODE_SEG __NEAR_SEG NON_BANKED
interrupt void capture(void)
{
if(ECT_T标志寄存器1_C0F == 1)
ECT_T标志寄存器1_C0F = 1;
time1 = time2;
time2 = ECT_TC0;
delaytime = time2-time1;
LEDCPU = ~LEDCPU;
}
#pragma CODE_SEG DEFAULT
/*************************************************************/
/* 主函数 */
/*************************************************************/
void main(void) {
DisableInterrupts;
INIT_PLL();
initialize_ect();
//init_pwm();
LEDCPU_dir = 1;
EnableInterrupts;
LEDCPU = 0;
DDRK_DDRK1=1;
for(;;)
{
PORTK_PK1=1;
delay();
PORTK_PK1=0;
delay();
}
} |