📄 main.c
字号:
/************************************************************************************
* *
* File Name : main.c *
* Contents : Main program of Timer RA Pulse Period Measurement Mode *
* sample program for R8C/25 Group *
* Copyright(C)2007, Renesas Technology Corp. *
* Copyright(C)2007, Renesas Solutions Corp. *
* All rights reserved. *
* Version : 2.00 *
* note : 1.00 : First version *
* : 2.00 : Waiting time for oscillation stabilization added *
* : Loop of main process modified to measure pulse period *
* : Timer RA interruption process added *
************************************************************************************/
#include "sfr_r825.h" /* Definition of the R8C/25 SFR */
/* Definition of RAM area */
unsigned char f_edge = 0; /* Active edge flag */
unsigned char undf_cnt = 0; /* Underflow counter */
unsigned char present_tra; /* Present content of TRA register */
unsigned short measurement_value; /* Measurement value */
/* Declaration of function prototype */
void timer_ra_init(void); /* Initial setting of Timer RA SFR */
void TRA_int(void); /* Timer RA interruption process */
/************************************************************************************
Name: main
Parameters: None
Returns: None
Description: User main
************************************************************************************/
void main(void)
{
unsigned int i = 0;
asm("FCLR I"); /* Interrupt disabled */
/*-----------------------------------------------------------
- Set High-speed on-chip oscillator clock to System clock -
-----------------------------------------------------------*/
prc0 = 1; /* Protect off */
cm14 = 0; /* Low-speed on-chip oscillator on */
fra2 = 0x00; /* Selects Divide-by-2 mode */
fra00 = 1; /* High-speed on-chip oscillator on */
while(i <= 255) i++; /* This setting is an example of waiting time for the */
/* oscillation stabilization. Please evaluate the time */
/* for oscillation stabilization by a user. */
fra01 = 1; /* Selects High-speed on-chip oscillator */
cm16 = 0; /* No division mode in system clock division */
cm17 = 0;
cm06 = 0; /* CM16 and CM17 enable */
prc0 = 0; /* Protect on */
/*-----------------------------------
- Initialize Timer RA SFR -
-----------------------------------*/
timer_ra_init(); /* Initial setting of Timer RA SFR */
asm("FSET I"); /* Interrupt enable */
/*-----------------------------------
- Loop of main -
-----------------------------------*/
while(1){ /* Main processing */
if ( f_edge == 1 ) { /* Active edge received ? */
if ( undf_cnt == 0 ){ /* Timer RA underflows ? */
measurement_value = 0xFF - present_tra;
} else {
measurement_value = (0x0100*undf_cnt) + 0xFF - present_tra;
undf_cnt = 0; /* Underflow counter cleared */
}
f_edge = 0; /* Active edge flag cleared */
}
}
}
/************************************************************************************
Name: timer_ra_init
Parameters: None
Returns: None
Description: Initial setting of Timer RA SFR
************************************************************************************/
void timer_ra_init(void)
{
unsigned int i = 0;
pd1 &= 0x7F; /* P1_7(TRAIO):Input mode */
tstart_tracr = 0; /* Stop Timer RA operation */
while(tcstf_tracr != 0); /* Count stop ? */
traic = 0x00; /* Disable Timer RA Interrupt */
tstop_tracr = 1; /* The TRAPRE and TRA registers are initialized. */
/* The TSTART and TCSTF bits in the TRACR register */
/* are initialized too. */
/* Timer RA prescaler underflow : 40MHz * f2(FRA2) * f8(TCK2-TCK0) * 25 = 10us */
trapre = 25 - 1; /* Set (25 - 1) in TRAPRE register */
tra = 0xFF; /* Set 0xFF in TRA register */
tracr = 0x00; /* Set timer ra control regsiter */
/* TEDGF : Active edge not received */
/* TUNDF : No underflow */
traioc = 0x00; /* Set timer ra i/o control register */
/* Measures measurement pulse from one rising edge */
/* to next rising edge */
/* Select INT1/TRAIO pin (P1_7) */
/* TRAIO input filter : No filter */
tramr = 0x14; /* Set timer ra mode register */
/* Pulse period measurement mode */
/* Timer RA Count Source : f8 */
/* Provides count source */
traic = 0x07; /* Set Timer RA interrupt Control Register */
/* Interrupt priority level is set to level 7 */
tstart_tracr = 1; /* Start Timer RA operation */
while(tcstf_tracr != 1); /* During count ? */
while(i <= 255) i++; /* When using the pulse period measurement mode, */
/* leave two periods or more of Timer RA prescaler */
/* immediately after count starts, */
tracr = 0x21; /* then set the TEDGF bit to "0". */
}
/************************************************************************************
Name: TRA_int
Parameters: None
Returns: None
Description: Timer RA interruption process
************************************************************************************/
#pragma INTERRUPT TRA_int
void TRA_int(void)
{
if ( tedgf_tracr != 0 ) { /* Active edge received? */
tracr = 0x01; /* When set to "0" by a program, use a MOV instruction */
/* to write "0" to the TEDGF bit and the TUNDF bit */
/* in the TRACR register. */
present_tra = tra; /* Read tra register */
f_edge = 1; /* Active edge flag set */
}
if ( tundf_tracr != 0 ) { /* Timer RA underflows? */
tracr = 0x11; /* When set to "0" by a program, use a MOV instruction */
/* to write "0" to the TUNDF bit in the TRACR register. */
/* At the same time, write "1" to the TEDGF bit */
/* in the TRACR register. */
undf_cnt++; /* Underflow counter increment */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -