📄 noise_cap.c
字号:
/* FILE COMMENT ****************************************************
* M32R C Programming Rev. 1.00
* < Sample Program for TIO5 noize processing input & TML0 Capture >
*
* Copyright (c) 2003 Renesas Technology Corporation
* And Renesas Solutions Corporation
* All Rights Reserved
**********************************************************************/
/************************************************************************/
/* Include file */
/************************************************************************/
#include "..\inc\sfr32170_pragma.h"
/************************************************************************/
/* Function prototype declaration */
/************************************************************************/
void main(void); /* Main function */
void init_func(void); /* Initial setup function */
void port_init(void); /* Initialize port */
void TIO5_noise_init( void ); /* Initialize TIO5 noise processing input mode */
void TML0_Cap_init( void ); /* Initialize TML0 capture input */
void TIO5_noiseInt( void ); /* Process TIO5 noise processing input interrupt (user processing) */
void TIO4_7_Int(void); /* Process TIO4-7 output interrupt */
/************************************************************************/
/* Definition of external reference */
/************************************************************************/
extern void DisInt( void ); /* Interrupt disable function */
extern void EnInt( void ); /* Interrupt enable function */
/************************************************************************/
/* Define macro */
/************************************************************************/
/*** Noise processing input (TIO5) ***/
/* 0123 4567 */
#define IEB03_MASK (unsigned char)0xc4 /* 1100 0100B Clock bus & input event bus control register */
#define IEB03_INIT (unsigned char)0x04 /* 0000 0100B */
/* || +----- Input event bus 0 :Select TIO5 (TIO5 output) for output */
/* ++---------- Input event bus 3 :TIN3 */
/* 0123 4567 89AB CDEF */
#define TIN3_MASK (unsigned short)0x0700 /* 0000 0111 0000 0000B */
#define TIN3_HLevel (unsigned short)0x0600 /* 0000 0110 0000 0000B */
/* +++------------ Set high on TIN3 to be the active level */
/* 0123 4567 */
#define TIO5_Noise (unsigned char)0x9e /* 1001 1110B */
/* |||| |+++--- Set TIO5 noise processing input mode */
/* |||+-+------ Measurement input source : */
/* ||| Input event bus 3 selected */
/* +++--------- Select clock bus 0 */
#define TIO5_NoiseTime (unsigned short)100 - 1 /* Count value for noise judgment time */
/*** Capture(TML0) ***/
/* 0123 4567 */
#define TML0SS0_MASK (unsigned char)0x81 /* 1000 0001B TML0 control register */
#define TML0SS0_IEB0 (unsigned char)0x80 /* 1000 0000B */
/* | +--- Select 1/2 internal peripheral clock */
/* +----------- Input event bus 0 selected */
/************************************************************************/
/* Global variable */
/************************************************************************/
unsigned long capture_time; /* Measurement count value */
/*""FUNC COMMENT""*******************************************************
* Function name: init_func()
*-----------------------------------------------------------------------
* Description : Initialize ICU
*-----------------------------------------------------------------------
* Argument : -
*-----------------------------------------------------------------------
* Returns : -
*-----------------------------------------------------------------------
* Notes :
*""FUNC COMMENT END""***************************************************/
void init_func(void)
{
port_init(); /* Initialize those related to port */
}
/*""FUNC COMMENT""*******************************************************
* Function name :port_init()
*-----------------------------------------------------------------------
* Description : Initialize port
*-----------------------------------------------------------------------
* Argument : -
*-----------------------------------------------------------------------
* Returns : -
*-----------------------------------------------------------------------
* Notes :
*""FUNC COMMENT END""***************************************************/
void port_init(void)
{
PIEN = PIEN0; /* Enable port input */
P11DATA = 0x00; /* Output data (must be set prior to mode) */
P11DIR = 0xff; /* P110-P117 : Output mode */
P11MOD = 0x00; /* P110-P117 : Input/output port */
}
/*""FUNC COMMENT""*******************************************************
* Function name : TIO5_noise_init()
*-----------------------------------------------------------------------
* Description : Initialize TIO5 noise processing input mode
* : - TIO5 is used in noise processing input mode to detect
* : high on TIN3 via input event bus 3
* : - Output TIO5 underflow to input event bus 0 (to TML0_Cap0)
* : - Use clock bus 0 as a count source
*-----------------------------------------------------------------------
* Argument : -
*-----------------------------------------------------------------------
* Returns : -
*-----------------------------------------------------------------------
* Notes : Set prescaler, clock bus, etc. separately
* : Set functions of ICU's interrupt control related registers separately
* : Port input function must be enabled
* : For M32R/E#1,2,3, PnMOD cannot be accessed for R/M/W
* : The function must be executed while interrupt is disabled
*""FUNC COMMENT END""***************************************************/
void TIO5_noise_init()
{
unsigned char temp;
PRS0 = ( 100 - 1); /* Set prescaler(10us@10MHz) */
/*** Setting input event bus 0,3 ***/
CKIEBCR = ( CKIEBCR & ~IEB03_MASK) | IEB03_INIT; /* Select input event bus 0,3 for input */
/*** Setting noise processing input mode (TIO5) ***/
TIO5CR = TIO5_Noise; /* Set TIO5 noise processing input mode */
TIO5RL0 = TIO5_NoiseTime; /* Set TIO5 noise judgment time */
/*** Setting P153 (TIN3) high level active ***/
TINCR0 = ( TINCR0 & ~TIN3_MASK) | TIN3_HLevel; /* Set high on TIN3 to be the active level */
P15MOD |= 0x10; /* Select P153 for TIN */
/*** Setting interrupt (TIO5) ***/
temp = TIOIR1;
temp |= ( TIOIS7 | TIOIS6 | TIOIS4);
temp &= ~( TIOIS5 | TIOIM5); /* Enable TIO5 interrupt */
TIOIR1 = temp;
IMJTOCR4 = 0x01; /* TIO4-7 ILEVEL=1 */
/*** Setting interrupt (TIN3) ***/
temp = TINIR1;
temp |= ( TINIS4 | TINIS5 | TINIS6) | TINIM3; /* Disable TIN3 interrupt */
TINIR1 = temp;
/*** Starting count ***/
TIOPRO = ~TIO5PRO; /* Enable TIO5 enable protect rewrite */
TIOCEN = 0xffff; /* Starting count TIO5 */
}
/*""FUNC COMMENT""*******************************************************
* Function name : TML0_Cap_init()
*-----------------------------------------------------------------------
* Description : Initialize TML0 capture input
* : - Capture upon input event bus 0 (TIO5 underflow)
*-----------------------------------------------------------------------
* Argument : -
*-----------------------------------------------------------------------
* Returns : -
*-----------------------------------------------------------------------
* Notes : TML0 counter is not initialized
* : Counter start counting upon reset
* : The function must be executed while interrupt is disabled
*""FUNC COMMENT END""***************************************************/
void TML0_Cap_init()
{
/*** Setting measure timer (TML0) ***/
TML0CR = ( TML0CR & ~TML0SS0_MASK) | TML0SS0_IEB0; /* TML0 measure 0 -> input event bus 0 (TIO5 underflow) */
}
/*""FUNC COMMENT""*******************************************************
* Function name: main()
*-----------------------------------------------------------------------
* Description : - Interrupt processing is executed when TIO5 is operated
* : in noise processing input mode and high-level duration
* : on TIN3 is 1ms or more (source clock frequency: 10 MHz)
* : LED (PORT11) is incremented when an interrupt occurred
*-----------------------------------------------------------------------
* Argument : -
*-----------------------------------------------------------------------
* Returns : -
*-----------------------------------------------------------------------
* Notes : -
*""FUNC COMMENT END""***************************************************/
void main(void)
{
DisInt(); /* Disable interrupt */
init_func();
TML0_Cap_init(); /* Set TML0 capture */
TIO5_noise_init(); /* Start TIO5 count */
EnInt(); /* Enable interrupt */
while(1);
}
/*""FUNC COMMENT""*******************************************************
* Function name : TIO4_7_Int()
*-----------------------------------------------------------------------
* Description : - Process TIO4-7 output interrupt
* : Clear request if the interrupt is TIO5 interrupt and
* : TIO5 interrupt processing will be executed
*-----------------------------------------------------------------------
* Argument : -
*-----------------------------------------------------------------------
* Returns : -
*-----------------------------------------------------------------------
* Notes : TIO4,6,7 output interrupt not considered
*""FUNC COMMENT END""***************************************************/
void TIO4_7_Int(void)
{
unsigned char temp;
/*** Interrupt judgment(TIO5) ***/
if(( TIOIR1 & TIOIS5) != 0) {
DisInt(); /* Disable interrupt */
temp = TIOIR1;
temp |= ( TIOIS4 | TIOIS6 | TIOIS7);
temp &= ~TIOIS5; /* Clear TIN0 interrupt request */
TIOIR1 = temp;
EnInt(); /* Enable interrupt */
TIO5_noiseInt(); /* Process TIO5 interrupt */
}
}
/*""FUNC COMMENT""*******************************************************
* Function name: TIO5_noiseInt()
*-----------------------------------------------------------------------
* Description : Process TIO5 interrupt
*-----------------------------------------------------------------------
* Argument : -
*-----------------------------------------------------------------------
* Returns : -
*-----------------------------------------------------------------------
* 拲堄帠崁 : In the noise processing mode, the counter is stopped upon
* : an underflow and count must be started over again to
* : restart processing. The interrupt processing time until
* : recount is not considered
*""FUNC COMMENT END""***************************************************/
void TIO5_noiseInt()
{
DisInt(); /* Disable interrupt */
TIOPRO = ~TIO5PRO; /* TIO5 Enable TIO5 enable protect rewrite */
TIOCEN = 0xffff; /* TIO5 Starting count TIO5 */
EnInt(); /* Enable interrupt */
(P11DATA)++; /* Increment the port when an interrupt occurred */
capture_time = TML0MR0; /* Copy the TML0 measure 0 register value to the RAM */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -