/***********************************************************************/ /* */ /* FILE :FcountN.c */ /* DATE :Sat, Mar 20, 2010 */ /* DESCRIPTION :main program file. */ /* CPU GROUP :27 */ /* */ /* This file is generated by Renesas Project Generator (Ver.4.12). */ /* */ /***********************************************************************/ #include "sfr_r827.h" // Definiton of RAM area // Function Prototype void timer_ra_init(void); /* Initial setting of Timer RA SFR */ void timer_rb_init(void); /* Initial setting of Timer RB SFR */ void timer_rc_init(void); /* Initial setting of Timer RC SFR */ void DispLCD16x2_Init(void); void DispLCD16x2(unsigned char pos,_far char * str); // void LED_Data_Set(unsigned char uc); void trcint(void); #pragma INTERRUPT/B 7 trcint static unsigned long int upper, lower; // current counter value static unsigned char flag; // every 1 second interrupt processed flag void trcint(void) { unsigned short int trcbuf; char state; state = trcsr & 0x84; /* bit 7 -- overflow, bit 2 -- every 1 sec */ trcbuf = trcgrc; trcsr = 0; if ( state == 0x04 ) { /* every 1 second procedure only */ lower = (unsigned long int) trcbuf; flag = 1; // p4_5 = 1; // for DEBUG } else if ( state == 0x80 ) { /* overflow procedure only */ upper += 65536L; lower = 0L; // } else if ( trcbuf < 64 ) { /* overflow + 1_second procedure */ } else if ( trcbuf < 128 ) { /* overflow + 1_second procedure */ upper += 65536L; lower = (unsigned long int) trcbuf; flag = 1; // p4_5 = 1; // for DEBUG } else { /* 1_sec proc should be done before overflow proc */ lower = (unsigned long int) trcbuf; flag = 3; // p4_5 =1; } } void main(void) { unsigned int i; unsigned long int lastcount, countnew; unsigned long int count1, count2; // counter value unsigned long int dec; // 10**n char leadzero; // 1 if that char is leading 0 char delayovf; char num[11], dig; //----------------------------------------------------------------------- // 外部水晶発振子20MHz 使用 //----------------------------------------------------------------------- asm("FCLR I"); // 割り込み処理禁止 prc0 = 1; // cm0,cm1,ocd,har0,har1,har2 書き込み許可 cm13 = 1; // cm1_addr(0007h) bit3 P4-6,7 X'tal // cm15 = 1; // XIN-XOUT 駆動能力 強 cm05 = 0; // cm0_addr(0006h) bit5 X'tal 発振 cm06 = 0; // cm0_addr(0006h) bit6 cm16,cm17 有効 for(i=255;i>0;i--); ocd2 = 0; // ocd_addr(000Ch) bit3 メインクロック選択 prc0 = 0; // cm0,cm1,ocd,har0,har1,har2 書き込み禁止 //----------------------------------------------------------------------- // LCD Port を設定する //----------------------------------------------------------------------- pd1 = 0x0F; // ポートP1_3..0 出力モードに設定 DispLCD16x2_Init(); //----------------------------------------------------------------------- // P4_5 Port を設定する //----------------------------------------------------------------------- // pd4_5 = 1; // p4_5 = 0; //----------------------------------------------------------------------- // 時間初期化 //----------------------------------------------------------------------- // sec = 0; //----------------------------------------------------------------------- // 波形カウント 初期化 //----------------------------------------------------------------------- lastcount = 0L; // inten = 0x04; // INT1 割り込み有効 // int1ic = 0x00; // 割り込み禁止および割り込み要因クリア // int1ic = 0x01; // priority level 1 (lowest) //----------------------------------------------------------------------- // //----------------------------------------------------------------------- num[0] = '-'; num[1] = '-'; num[2] = '-'; num[3] = '-'; num[4] = '-'; num[5] = '-'; num[6] = '-'; num[7] = '-'; num[8] = 'H'; num[9] = 'z'; num[10] = '\0'; #define LINE1 0 // 1ラインめの開始位置 #define LINE2 16 // 2ラインめの開始位置 DispLCD16x2(LINE1,num); //----------------------------------------------------------------------- // タイマRA、RB を設定する // P3_1/TRBOから1secパルス出力 //----------------------------------------------------------------------- // taraioc = 0; // TRAをタイマモードに P1_7を/INT1に割り当て(初期値でOK) timer_ra_init(); /* Initial setting of Timer RA SFR */ timer_rb_init(); /* Initial setting of Timer RB SFR */ //----------------------------------------------------------------------- // タイマRCを設定する // -- Input capture mode -- // count source = TRCCLK(P3_3), input capture = TRCIOC(P5_3) //----------------------------------------------------------------------- timer_rc_init(); /* Initial setting of Timer RC SFR */ upper = 0L; flag = 0; asm("FSET I"); // 割り込み処理許可 /*----------------------------------- - Loop of main - -----------------------------------*/ while(1){ /* Main processing */ if (flag != 0) { /* Request interrupt acknowledged? */ countnew = upper + lower; count1 = countnew - lastcount; delayovf = flag >> 1; lastcount = countnew; flag = 0; if (delayovf != 0) { upper += 65536L; lower = 0L; } else ; dec = 10000000; leadzero = 1; for (dig = 0; dig < 8; dig++){ num[dig] = count1 / dec; count2 = count1 % dec; if (num[dig] > 0) leadzero = 0; if ((num[dig] == 0) && (leadzero == 1)) num[dig] = ' '; else num[dig] = num[dig] + '0'; count1 = count2; dec /= 10; } if (num[7] == ' ') num[7] = '0'; DispLCD16x2(LINE1,num); } } DispLCD16x2(27," STOP"); // this program never comes here. } /************************************************************************************ Name: timer_ra_init Parameters: None Returns: None Description: Initial setting of Timer RA SFR to timer mode ************************************************************************************/ void timer_ra_init(void){ // tramr = 10h; // Xin/8をカウントソースに // trapre = 249; // T = (1/20E6)*8*250*250 = 0.025 = 1/40 [sec] // tra = 249; // 1/40[sec]ごとに割り込み // tracr = 1; // タイマスタート tstart_tracr = 0; /* Stop Timer RA operation */ while(tcstf_tracr != 0); traic = 0x00; /* Disable Timer RA Interrupt */ tstop_tracr = 1; /* The TRAPRE and TRAPR registers are initialized */ /* The TSTART and TCSTF bits in the TRACR register are initialized too. */ /* Period between underflows : 20MHz * f8 * 250 * 250 = 25ms */ trapre = 250-1; /* Set (250 - 1) in TRAPRE register */ tra = 250-1; /* Set (250 - 1) in TRA register */ // tedgsel_traioc = 0; /* Set to "0" in timer mode */ // topcr_traioc = 0; /* Set to "0" in timer mode */ // toena_traioc = 0; /* Set to "0" in timer mode */ // tiosel_traioc = 0; /* Set to "0" in timer mode */ // tipf0_traioc = 0; /* set to no input filter */ // tipf1_traioc = 0; traioc = 0; // Set TRA timer mode without input filter tmod0_tramr = 0; /* Set to "000" in timer mode */ tmod1_tramr = 0; tmod2_tramr = 0; tck0_tramr = 1; /* Select "f8" in Count Source */ tck1_tramr = 0; tck2_tramr = 0; tckcut_tramr = 0; /* Provides count source */ tstart_tracr = 1; /* Start Timer RA operation */ while(tcstf_tracr != 1); } /************************************************************************************ Name: timer_rb_init Parameters: None Returns: None -- Programable wave form generation mode - 1 Hz square wave Description: Initial setting of Timer RB SFR ************************************************************************************/ void timer_rb_init(void){ tstart_trbcr = 0; /* Stop Timer RB operation */ while(tcstf_trbcr != 0); trbic = 0x00; /* Disable Timer RB Interrupt */ tstop_trbcr = 1; /* The TRBPRE and TRBPR registers are initialized */ /* The TSTART and TCSTF bits in the TRBCR register are initialized too. */ /* Period between underflows : 25ms * 10 * (2 + 2) = 1s */ trbpre = 10-1; /* Set (10 - 1) in TRBPRE register */ // trbpre = 40-1; /* Set (40 - 1) in TRBPRE register */ trbsc = 2-1; /* Set (2 - 1) in TRBSC register */ trbpr = 2-1; /* Set (2 - 1) in TRBPR register */ topl_trbioc = 0; /* Set to "0" -> pri=H sec=L */ tocnt_trbioc = 0; /* Set to "0" to get wave form output */ inostg_trbioc = 0; /* Set to "0" in this mode */ inoseg_trbioc = 0; /* Set to "0" in this mode */ tmod0_trbmr = 1; /* Set to "01" for programable wave gen mode */ tmod1_trbmr = 0; twrc_trbmr = 1; /* Write to reload register */ tck0_trbmr = 0; /* Select "RA underflow" in Count Source */ tck1_trbmr = 1; tckcut_trbmr = 0; /* Provides count source */ tstart_trbcr = 1; /* Start Timer RB operation */ while(tcstf_trbcr != 1); } /************************************************************************************ Name: timer_rc_init Parameters: None Returns: None -- Input capture mode -- count source = TRCCLK(P3_3), input capture = TRCIOC(P5_3) Description: Initial setting of Timer RC SFR ************************************************************************************/ void timer_rc_init(void){ int3en = 0; ssisel = 0; pd3_3 = 0; trccr1 = 0x50; // count source = TRCCLK(P3_3) // trccr1 = 0x40; // count source = f32 trcior1 = 0x04; // TRCGRC = input capture mode (rising edge) trc = 0; // TRC count value initialize to 0 trcier = 0x84; // TRC OVF and input caputure C interrupt enable trcic = 0x01; // enable TRC interrupt request level 1 tstart_trcmr = 1; // TRC count start }