📄 top0_dss.c
字号:
/*""FILE COMMENT""*****************************************************
* M32R C Programming Rev. 1.00
* < Sample Program for TOP0 delayed 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 TOP0_DSS_init( void ); /* Initialize TOP0 delayed single-shot output mode */
unsigned long TOP0_DSS_out( unsigned short, unsigned short ); /* Start TOP0 delayed single-shot output */
unsigned long TOP0_DSS_cc( signed short ); /* Correct single-shot output time */
/************************************************************************/
/* Define macro */
/************************************************************************/
#define OK 1
#define NG 0
/*** Delayed single-shot(TOP0) ***/
/* 0123 4567 89AB CDEF */
#define TOP0_MASK (unsigned short)0x0373 /* 0000 0011 0111 0011B */
#define TOP0_DSS (unsigned short)0x0100 /* 0000 0001 0000 0000B */
/* || ||| ++- Select clock bus 0 */
/* || +++------ don't care */
/* ++-------- Set TOP0 delayed single-shot output mode */
/*""FUNC COMMENT""*******************************************************
* Function name: TOP0_DSS_init()
*-----------------------------------------------------------------------
* Description : Set TOP0 delayed single-shot output modeInitial settings necessary to drive TOP0 in
* : delayed single-shot mode
* : - While using TOP0 in delayed single-shot mode, this function outputs a single-shot waveform from
* : the TO0 pin
* : - 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 TOP0_DSS_init()
{
/*** Initializing P110 (TO0) output(low level output) ***/
FFP0 = ~FP0; /* Enable F/F0 rewrite */
FFD0 = 0x0000; /* F/F0 low(0) output (inverted to high during single-shot output) */
P11MOD |= 0x80; /* Select TO0 (TOP0 output) for output */
/*** Setting delayed single-shot(TOP0) ***/
TOP05CR0 = ( TOP05CR0 & ~TOP0_MASK) | TOP0_DSS; /* Set TOP0 delayed single-shot */
TOPIR1 |= TOPIM0; /* Disable TOP0 interrupt */
}
/*""FUNC COMMENT""*******************************************************
* Function name: TOP0_DSS_out()
*-----------------------------------------------------------------------
* Description : Drive TOP0 in delayed single-shot mode
*-----------------------------------------------------------------------
* Argument : unsigned short delay delay time
* : unsigned short PW pulse width
*-----------------------------------------------------------------------
* Returns : Terminated normally 1
* : Terminated abnormally 0
* : - delay time = 0
* : - pulse width = 0
* : - Immediately before single-shot output or during output
*-----------------------------------------------------------------------
* Notes : The rewrite timing judgment value needs to be calculated according to the count source
*""FUNC COMMENT END""***************************************************/
unsigned long TOP0_DSS_out( unsigned short delay, unsigned short PW)
{
unsigned short work;
if(( delay == 0) || ( PW == 0)) { /* Determine delay time and pulse width */
return( NG);
}
if((( TOPCEN & TOP0CEN) != 0) /* Determine rewrite timing */
&& (( TOP0CT <= 1) || (( FFD0 & FD0) != 0))) { /* Multiple startup or immediately before end */
return( NG);
}
/*** Setting counter and reload register ***/
work = delay - 1;
TOP0CT = work;
TOP0RL = PW - 1;
/*** Starting count (even while counting) ***/
DisInt(); /* Disable interrupt */
TOPPRO = ~TOP0PRO; /* Enable TOP0 enable protect rewrite */
TOPCEN = 0xffff; /* Start TOP0 count */
while( TOP0CT == work); /* Wait until count start */
EnInt(); /* Enable interrupt */
return( OK);
}
/*""FUNC COMMENT""*******************************************************
* Function name: TOP0_DSS_cc()
*-----------------------------------------------------------------------
* Description : Correct TOP0 single-shot output time
*-----------------------------------------------------------------------
* Argument : signed short cc correction time
*-----------------------------------------------------------------------
* Returns : Terminated normally 1
* : Terminated abnormally 0
* : correction time = 0
* : - No sufficient time for correction (immediately before end of single-shot output)
* : Timing at (5) below
* :
* : (1) (2) (3) (4) (5)
* : | | | | |
* : Count start | | | |
* : | +-------------+
* : V | |
* : ------------------+ +----------
* :
*-----------------------------------------------------------------------
* Notes : The 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 TOP0_DSS_cc( signed short cc)
{
if( cc == 0) {
return( NG);
}
DisInt(); /* Disable interrupt */
if(( TOPCEN & TOP0CEN) == 0) { /* Count halted? */
TOP0RL += cc; /* Timing at (1) */
} else if( TOP0CT >= 2) {
if(( FFD0 & FD0) != 0) { /* During delay? */
TOP0CC = cc - 1; /* Timing at (4) */
} else {
TOP0RL += cc; /* Timing at (2) */
}
} else {
if(( FFD0 & FD0) != 0) { /* During delay? */
return( NG); /* Timing at (5) */
} else {
while(( FFD0 & FD0) == 0); /* Wait for end of delay, timing at (3) */
TOP0CC = cc - 1;
}
}
EnInt(); /* Enable interrupt */
return( OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -