⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 top6_ss.c

📁 瑞萨CAN通讯单片机程序
💻 C
字号:
/*""FILE COMMENT""*****************************************************
 *	M32R C Programming		Rev. 1.00
 *		< Sample Program for TOP6 single-short output mode >
 *
 *	Copyright (c) 2003 Renesas Technology Corporation
 *			   And Renesas Solutions Corporation
 *			   All Rights Reserved
 *********************************************************************/

/************************************************************************/
/*		Include file						*/
/************************************************************************/

#include		"..\inc\sfr32170_pragma.h"

/************************************************************************/
/*		Definition of external reference			*/
/************************************************************************/

extern	void		DisInt( void );				/* Interrupt disable function */
extern	void		EnInt( void ); 				/* Interrupt enable function */

/************************************************************************/
/*		Function prototype declaration				*/
/************************************************************************/

	void		TOP6_SS_init( void );			/* Initialize TOP6 single-shot output mode */
	unsigned long	TOP6_SS_out( unsigned short );		/* Start TOP6 single-shot output */
	unsigned long	TOP6_SS_cc( signed short );		/* Correct single-shot output time */

/************************************************************************/
/*		Define macro						*/
/************************************************************************/

#define OK	1
#define NG	0

/*** single-shot(TOP6) ***/

							/* 0123 4567 89AB CDEF	*/
#define	TOP6_MASK		(unsigned short)0x0373	/* 0000 0011 0111 0011B	*/
#define	TOP6_SS			(unsigned short)0x0010	/* 0000 0000 0001 0000B	*/
							/*	  ||  |||   ++-- Select clock bus 0			*/
							/*	  ||  +++------- Don't care				*/
							/*	  ||	(For the 32171, use of B'000 is inhibited) 	*/
							/*	  ++------------ Set TOP6 single-shot output mode 	*/

							/* 0123 4567 89AB CDEF	*/
#define	FF6_TOP6M		(unsigned short)0x0001	/* 0000 0000 0000 0001B	*/
							/*		     +- FF6 source : TOP6 unselected */

/*""FUNC COMMENT""*******************************************************
 * Function name: TOP6_SS_init()
 *-----------------------------------------------------------------------
 * Description	: FF6 source: TOP6 unselectedInitial settings necessary to drive TOP6 in single-shot mode
 * 		: - While using TOP6 in single-shot output mode, this program function a single-shot pulse from TO6
 * 		: - The count source used for this operation is clock bus 0.
 *-----------------------------------------------------------------------
 * Argument	: -
 *-----------------------------------------------------------------------
 * Returns	: -
 *-----------------------------------------------------------------------
 * Notes	: The prescaler, clock bus, etc. are set separately from the above
 * 		: Must be executed while interrupts are disabled
 *""FUNC COMMENT END""***************************************************/
void	TOP6_SS_init()
{

	unsigned char	temp;

/*** Setting P116(TO6) output Initializ("L"output) ***/

	FFS0 &= ~FF6_TOP6M;					/* FF6 source: TOP6 selected */

	FFP0 = ~FP6;						/* Enable F/F6 rewrite */
	FFD0 = 0x0000;				/* F/F6 low(0) output (inverted to high during single-shot output) */
	P11MOD |= 0x02;						/* Select TO6(TOP6 output) for output) */

/*** Setting single-shot mode (TOP6) ***/

	TOP67CR = ( TOP67CR & ~TOP6_MASK) | TOP6_SS;		/* Setting single-shot mode(TOP6) */
	temp = TOPIR2;
	temp |= ( TOPIS7 | TOPIM6);				/* Disable TOP6 interrupt */
	TOPIR2 = temp;
}

/*""FUNC COMMENT""*******************************************************
 * Function name: TOP6_SS_out()
 *-----------------------------------------------------------------------
 * Description	: Drive TOP6 in single-shot mode
 *-----------------------------------------------------------------------
 * Argument	: unsigned short	PW	pulse width
 *-----------------------------------------------------------------------
 * Returns	: Terminated normally		1
 * 		: Terminated abnormally		0
 * 		:	 - Pulse width = 0
 * 		:	 - Timer in operation
 *-----------------------------------------------------------------------
 * Notes	: The rewrite timing judgment value needs to be calculated according to the count source
 *""FUNC COMMENT END""***************************************************/
unsigned long	TOP6_SS_out( unsigned short PW)
{
	unsigned long	work;

	if( PW == 0) {						/* Determine pulse width */
		return( NG);
	}

	if(( TOPCEN & TOP6CEN) != 0) {				/* Determine rewrite timing */
		return( NG);
	}

/*** Setting reload register ***/

	TOP6RL = PW - 1;

/*** Starting count ***/

	DisInt();						/* Disable interrupt */

	TOPPRO = ~TOP6PRO;					/* Enable TOP6 enable protect rewrite */
	TOPCEN = 0xffff;					/* Start TOP6 count */

	while(( FFD0 & FD6) == 0);				/* Wait until count start */

	EnInt();						/* Enable interrupt */

	return( OK);
}

/*""FUNC COMMENT""*******************************************************
 * Function name: TOP6_SS_cc()
 *-----------------------------------------------------------------------
 * Description	: Correct TOP6 single-shot output time
 *-----------------------------------------------------------------------
 * Argument	: signed short cc	Correction time
 *-----------------------------------------------------------------------
 * Returns	: Terminated normally		1
 * 		: Terminated abnormally		0
 * 		:	 - Pulse width = 0
 * 		:	 - No sufficient time for correction (immediately before end of single-shot output)
 * 		:	   Timing at (3) below
 * 		:
 * 		:    (1)	(2)    (3)
 * 		:	   |	       | |
 * 		:     Starting count   | |
 * 		:	   +-------------+
 * 		:	   |		 |
 * 		:  --------+		 +----------
 * 		:
 *-----------------------------------------------------------------------
 * Notes	: Starting countThe rewrite timing judgment value needs to be calculated according to the count source
 * 		: (Note that correction is made synchronously with the next active clock transition)
 * 		: Overflow after correction is not taken into consideration
 *""FUNC COMMENT END""***************************************************/
unsigned long	TOP6_SS_cc( signed short cc)
{
	if( cc == 0) {
		return( NG);
	}

	DisInt();						/* Disable interrupt */

	if(( TOPCEN & TOP6CEN) == 0) {				/* Count halted? */
		TOP6RL += cc;					/* Timing at (1) */
	} else if( TOP6CT >= 2) {
		TOP6CC = cc - 1;				/* Timing at (2) */
	} else {
		return( NG);					/* Timing at (3) */
	}
	EnInt();						/* Enable interrupt */

	return( OK);
}

⌨️ 快捷键说明

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