cap_sample.c

来自「最新版IAR FOR ARM(EWARM)5.11中的代码例子」· C语言 代码 · 共 272 行

C
272
字号
/*************************************************************************************/
/*                                                                                   */
/*      Copyright (C) 2005 Oki Electric Industry Co., LTD.                           */
/*                                                                                   */
/*      System Name     :  ML674051/ML67Q4061                                        */
/*      Module Name     :  Capture sample program                                    */
/*      File   Name     :  cap_sample.c                                              */
/*      Revision        :  1.00                                                      */
/*      Date            :  2005/02/14                                                */
/*                         Initial version                                           */
/*                                                                                   */
/*************************************************************************************/
#include "ML674061.h"
#include "common.h"
#include "irq.h"

#ifdef __IAR__
#include "intrinsics.h"
#endif

/* constants */
#define MHz      (1000000L)
#define SYSCLK	(33)			/* SYSCLK (MHz)*/
#define	PWMCYC	(10)			/* (msec) */
#define VALUE_OF_FTMnR          /* reload value of timer */\
                (65536 - (PWMCYC * SYSCLK * 1000) / 16)
#define DUTY_VALUE	(70)

#define VALUE_OF_FTMnGR			/* FTMnGR value */\
				((((65536 - VALUE_OF_FTMnR) * DUTY_VALUE) / 100 ) + VALUE_OF_FTMnR)
				
/* functions */
int main(void);                     /* main routine */
static void reg_irq_handler(void);  /* registration of IRQ handler */
static void cap1_handler(void) ;	/* cap1 handler */
static void set_pwm(void) ;			/* setting of pwm */
static void set_cap(void) ;			/* setting of cap */
static void measure_PulseWidth(void) ;	/* measuring of Pulse Width */


/* global variables */
static volatile unsigned int int_flg ;            /* cap interrupt count flag */
static unsigned int pulse_start_time ;	          /* first captured time */
static unsigned int pulse_end_time ;              /* second captured time */
static volatile unsigned int PW_VALUE ;		  /* value of measured Pulse Width */
static volatile unsigned int PW_VALUE_TIME ;	  /* time of measured Pulse Width */


/* LED lighting pattern table */
UHWORD LED_TABLE [18] = {LED_0,LED_1,LED_2,LED_3,   /* "0","1","2","3" */
                         LED_4,LED_5,LED_6,LED_7,   /* "4","5","6","7" */
                         LED_8,LED_9,LED_A,LED_b,   /* "8","9","A","b" */
                         LED_C,LED_d,LED_E,LED_F,   /* "C","d","E","F" */
                         LED_all,LED_off};          /* all on ,all off */

/****************************************************************************/
/*  Entry point                                                             */
/*  Function : main                                                         */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   0                                                   */
/****************************************************************************/
int main(void)
{
	/* initialize IRQ */
	init_irq();
	
	/* registration of IRQ handler */
	reg_irq_handler();
	
	/* initialize LED */
	init_led();   /* set output mode */
	
	/* light LED start pattern */
	led_on(LED_START_PATTERN);
	
	/* set pwm */
	set_pwm() ;
	
	/* start PWM */
	put_hvalue(FTMEN, 0x1) ;

      /* enable IRQ */
#ifdef __IAR__
      __enable_interrupt();
#else
    irq_en();
#endif

	/* light LED off */
	led_on(LED_off);

	/* set CAP */
	set_cap() ;

	/* measure Pulse Width */
	measure_PulseWidth() ;
	
	/* disable IRQ */
#ifdef __IAR__
      __disable_interrupt();
#else
    irq_dis();
#endif

    	put_wvalue(PORTSEL3,0);	

	/* turn on lower square LED */
	led_on(LED_NORMAL_END_PATTERN);
	
	return 0 ;

}

/****************************************************************************/
/*  Registration of IRQ Handler                                             */
/*  Function : reg_irq_handler                                              */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/*  Note : Initialize of IRQ needs to be performed before this process.     */
/****************************************************************************/
void reg_irq_handler(void)
{
    /* register IRQ handlers into handler table */
    IRQ_HANDLER_TABLE[INT_TIMER1] = cap1_handler;

    /* setup interrupt level */
    set_wbit(EXILCA, ILC_ILC16 & ILC_INT_LV1);  /* timer1(nIRQ[17]) -> level1 */

    return;
}

/*****************************************************************************/
/*  Capture 1 handler                                                        */
/*  Function : cap1_handler                                                  */
/*     Parameters                                                            */
/*          Input   :   Nothing                                              */
/*          Output  :   Nothing                                              */
/*****************************************************************************/
void cap1_handler(void)
{
	/* clear cap interrupt */
	set_hbit(FTM1ST,0x1) ;
	
	if(int_flg == 0)
	{
		/* save captured time to global variable A */
		pulse_start_time = get_hvalue(FTM1GR) ;	
		int_flg = 1 ;
		
		/* set falling edge */
		put_hvalue(FTM1IOLV, 0x2) ;
	}
	else
	{
		/* save captured time to global variable B */
		pulse_end_time = get_hvalue(FTM1GR) ;
		if(get_hvalue(FTM1ST) & 0x2)
			pulse_end_time += 0xFFFF ;
			
		int_flg = 2 ;
		
		/* disable cap1 interrupt */
		clr_hbit(FTM1IER, 0x1) ;
		
	}
	
	/* clear ovf interrupt */
	set_hbit(FTM1ST,0x2) ;
	
	
    return;
}

/************************************************************************/
/*	Setting of PWM (FTM0)						*/
/*	Function	:	set_pwm					*/
/*	Parameters							*/
/*		Input :	Nothing						*/
/*		Output:	Nothing						*/
/************************************************************************/
void set_pwm(void)
{
	unsigned int portsel_value = 0 ;
	
	/* set PF0 second function */
	portsel_value = get_wvalue(PORTSEL3) ;
	put_wvalue(PORTSEL3, (portsel_value | 0x10000)) ;	
	
	/* set FTM0 to PWM mode */
	put_hvalue(FTM0CON, 0x10) ;
	
	/* set PWM cycle */
	put_hvalue(FTM0R, VALUE_OF_FTMnR) ;

	/* set PWM duty */
	put_hvalue(FTM0GR, VALUE_OF_FTMnGR) ;
	
	/* set PWM out */
	put_hvalue(FTM0IOLV, 0x1) ;

	/* set PWM clock */
	put_hvalue(FTM0CON, ((get_hvalue(FTM0CON) | 0x7) & 0xFFFC)) ;	/* APB_CLK/16 */
	
	
	return ;
}
/************************************************************************/
/*	Setting of CAP (FTM1)												*/
/*	Function	:	set_cap												*/
/*	Parameters															*/
/*		Input :	Nothing													*/
/*		Output:	Nothing													*/
/************************************************************************/
void set_cap(void)
{
	unsigned int portsel_value = 0 ;
	
	/* set PF1 second function */
	portsel_value = get_wvalue(PORTSEL3) ;
	put_wvalue(PORTSEL3, (portsel_value | 0x40000)) ;
	
	/* set FTM1 to CAP mode */
	put_hvalue(FTM1CON, 0x18) ;

	/* set CAP cycle */
	put_hvalue(FTM1R, 0x00) ;
	
	/* set CAP clock */
	put_hvalue(FTM1CON, ((get_hvalue(FTM1CON) | 0x7) & 0xFFFC)) ;	/* APB_CLK/16 */

	/* set rising edge to capture trigger */
	put_hvalue(FTM1IOLV, 0x01) ;

	/* start CAP */
	put_hvalue(FTMEN, 0x2) ;
	
	/* cap interrupt request clear */
	set_hbit(FTM1ST, 0x0001) ;
	
	/* enable cap interrupt */
	set_hbit(FTM1IER, 0x0001) ;

	
	return ;
}

/************************************************************************/
/*	Measuring of Pulse Width											*/
/*	Function	:	measure_PulseWidth									*/
/*	Parameters															*/
/*		Input :	Nothing													*/
/*		Output:	Nothing													*/
/************************************************************************/
void measure_PulseWidth(void)
{
	/* wait for finish of CAP */
	while(int_flg != 2)
	{
		;
	}
	
	PW_VALUE = pulse_end_time - pulse_start_time ;
	
	PW_VALUE_TIME = 10 * PW_VALUE / (65536 - VALUE_OF_FTMnR) ;
	
	
	return ;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?