RX210のPORTB3を使用してパルスの幅を測定するプログラムです。
- #include <stdio.h>
- #include <machine.h>
- #include "iodefine.h"
- #include "vect.h"
- unsigned short mtu0_ovf_cnt = 0; /* MTU0.TCNT Overflow counter */
- volatile unsigned long pulse_cnt = 0; /* Pulse counter */
- unsigned char start_flag = 0; /* Measurement start flag */
- unsigned char error_flag = 0; /* Error flag */
- unsigned int pulseIn(void)
- {
- pulse_cnt = 0; // パルス幅リセット
- start_flag = 0; // スタートフラグリセット
- while (pulse_cnt == 0)
- ; // 割り込みが発生するまで待つ
- return pulse_cnt;
- }
- void MTU0_inputcapture_init(void)
- {
- /* ==== MTU module ON ==== */
- SYSTEM.PRCR.WORD = 0x0A502; /* protect off */
- MSTP(MTU) = 0; /* MTU(MTU0 to MTU5) module stop state is canceled */
- SYSTEM.PRCR.WORD = 0x0A500; /* protect on */
- /* ==== Disable Interrupt ==== */
- IEN(MTU0, TGIA0) = 0; /* Disable TGIA0 interrupt */
- IEN(MTU0, TCIV0) = 0; /* Disable TCIV0 interrupt */
- MTU0.TIER.BIT.TGIEA = 0; /* TGIA0 Interrupt Request Disabled */
- MTU0.TIER.BIT.TCIEV = 0; /* TCIV0 Interrupt Request Disabled */
- /* ==== MTU Setting ==== */
- MTU.TSTR.BIT.CST0 = 0; /* MTU0 Count Stop */
- MTU.TSYR.BIT.SYNC0 = 0; /* MTU0 Count Independent */
- MTU0.TCNT = 0x0000; /* Count Clear */
- MTU0.TGRA = 0x0000; /* TGRA Clear */
- /* ---- MPC Setting ---- */
- PORTB.PDR.BIT.B3 = 0; /* PORTB3 is input port */
- PORTB.PMR.BIT.B3 = 0; /* select I/O port */
- MPC.PWPR.BIT.B0WI = 0; /* enable writing PFSWE bit */
- MPC.PWPR.BIT.PFSWE = 1; /* enable writing PFS register */
- MPC.PB3PFS.BYTE = 0x01; /* Select MTIOC0A */
- MPC.PWPR.BIT.PFSWE = 0; /* disable writing PFS register */
- MPC.PWPR.BIT.B0WI = 1; /* disable writing PFSWE bit */
- PORTB.PMR.BIT.B3 = 1; /* select peripheral functions */
- MTU0.TCR.BYTE = 0x21; /* PCLKB/4 */
- /* Count at rising edge */
- /* TCNT cleared by TGRA input capture */
- // MTU0.TIORH.BYTE = 0x08; /* MTIOC0A(PORTB3) */
- MTU0.TIORH.BYTE = 0x0A; /* Input capture at rising edge 両エッジでキャプチャ*/
- MTU0.TMDR.BYTE = 0x00; /* Normal mode */
- /* ==== ICU Setting ==== */
- IPR(MTU0, TGIA0) = 3; /* TGIA0 interrupt request level 3 */
- IPR(MTU0, TCIV0) = 4; /* TCIV0 interrupt request level 4 */
- IR(MTU0, TGIA0) = 0; /* Clear TGIA0 interrupt request */
- IR(MTU0, TCIV0) = 0; /* Clear TGIV0 interrupt request */
- /* ==== Enable Interrupt ==== */
- MTU0.TIER.BIT.TGIEA = 1; /* TGIA0 Interrupt Request Enabled */
- MTU0.TIER.BIT.TCIEV = 1; /* TCIV0 Interrupt Request Enabled */
- IEN(MTU0, TGIA0) = 1; /* Enable TGIA0 interrupt */
- IEN(MTU0, TCIV0) = 1; /* Enable TCIV0 interrupt */
- MTU.TSTR.BIT.CST0 = 1; /* ---- MTU0 Start ---- */
- }
- void error_proc(void)
- {
- pulse_cnt = 0xffffffff;
- }
- void Excep_MTU0_TCIV0(void)
- {
- /* ---- During the pulse period measurement ---- */
- if (start_flag == 1)
- {
- if (mtu0_ovf_cnt < 0xFFFF)
- { /* Confirmation of the greater than 65535 times */
- mtu0_ovf_cnt++; /* Increment the overflow counter */
- }
- else
- {
- error_flag = 1; /* Measurement error */
- error_proc();
- }
- }
- }
- void Excep_MTU0_TGIA0(void)
- {
- unsigned short tmp_tgra; /* Temporary variable is MTU0.TGRA */
- tmp_tgra = MTU0.TGRA; /* MTU0.TGRA set */
- if (start_flag == 0)
- {
- start_flag = 1; /* Start pulse period measurement */
- }
- else
- {
- /* Calculation of the pulse period */
- pulse_cnt = ((unsigned long)mtu0_ovf_cnt << 16) + (unsigned long)tmp_tgra;
- }
- mtu0_ovf_cnt = 0; /* Clear the overflow counter */
- }
- void main(void)
- {
- unsigned long pulse;
- clrpsw_i();
- change_oscillation_PLL(); /* クロックを水晶振動子50MHzに変更 */
- MTU0_inputcapture_init();
- setpsw_i();
- PORTB.PDR.BIT.B7 = 1;
- while (1)
- {
- PORTB.PODR.BIT.B7 = 1;
- for (int i = 0; i < 18500; i++)
- {
- nop();
- }
- PORTB.PODR.BIT.B7 = 0;
- pulse = pulseIn();
- printf("%d\n", pulse);
- for (int i = 0; i < 8500000; i++)
- {
- nop();
- }
- }
- }