///// Stimulator ///// ///// Stimulation用プログラム ///// ///// 2006.10.31 Ver.2.00 ///// ///// 技術課  伊藤 昭光 ///// ///// 自然科学研究機構 生理学研究所 ///// #include <16f88.h> #fuses HS, NOWDT, PUT, NOPROTECT, BROWNOUT, NOMCLR, NOLVP, NOCPD #use delay(CLOCK = 20000000) //クロック周波数指定 #use fast_io(B) //固定入出力モード // 定数設定 #define DTIME 300 //ms // グローバル変数宣言 static int icount0, icount1, icount2; static int start = 0; static int delay1 = 10; static int repeat = 2; static int f_switch = 0; static int f_mode = 0; //モードフラグ 0:roop, 1:external // BCDからセグメントデータへの変換テーブル int const LED_SEG[16] = {0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10, 0x08, 0x03, 0x46, 0x21, 0x06, 0x0E}; int const REP_H[5] = {0, 0, 1, 2, 4}; int const REP_L[5] = {1, 2, 2, 2, 0}; int const REP_N[5] = {1, 2, 12, 22, 40}; // タイマ0割り込み処理関数 #int_timer0 void intval0() { set_timer0(0x3C); //タイマ0を10msecに再セット icount0++; //回数カウントアップ output_high(PIN_B4); delay_us(300); //0.3msec output_low(PIN_B4); if (icount0 >= delay1) //0〜100ms待ち { disable_interrupts(INT_TIMER0); set_timer2(59); enable_interrupts(INT_TIMER2); output_high(PIN_B5); delay_us(300); //0.3msec output_low(PIN_B5); icount2 = 0; icount0 = 0; //カウンタの再初期化 } } // タイマ1割り込み処理関数 #int_timer1 void intval1() { set_timer1(0x0BDC); //タイマ1を100msecに再セット icount1++; //回数カウントアップ if (icount1 == 1) { set_timer0(0x3C); enable_interrupts(INT_TIMER0); output_high(PIN_B6); delay_us(300); //0.3msec output_low(PIN_B6); } if (icount1 >= 14) //1.4秒待ち { icount1 = 0; //カウンタの再初期化 } if (f_mode == 1) //外部割り込み時 disable_interrupts(INT_TIMER1); } // タイマ2割り込み処理関数 #int_timer2 void intval2() { set_timer2(59); //タイマ2を3msecに再セット output_high(PIN_B5); delay_us(300); //0.3msec output_low(PIN_B5); icount2++; //回数カウントアップ if (icount2 >= REP_N[repeat]) //2.85ms待ち { icount2 = 0; //カウンタの再初期化 disable_interrupts(INT_TIMER2); } } // メイン関数 void main() { icount0 = 0; //回数カウンタ0初期クリア icount1 = 0; //回数カウンタ1初期クリア icount2 = 0; //回数カウンタ2初期クリア // output_bit(PIN_A0, 1); //8bit LED デコード // output_bit(PIN_A1, 1); set_tris_b(0); //全ピン出力モード output_b(0xFF); //全ピンhigh output_b(LED_SEG[10]); output_high(PIN_A0); //1桁目連続点灯 output_low(PIN_A1); // タイマ0初期設定 setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256); // set_timer0(0x3C); //10msecに設定 // タイマ1初期設定 setup_timer_1(T1_INTERNAL | T1_DIV_BY_8); // set_timer1(0x0BDC); //100msecに設定 // タイマ2初期設定 setup_timer_2(T2_DIV_BY_16, 59, 16); // set_timer2(59); //10msecに設定 // 割り込み許可 // enable_interrupts(INT_TIMER0); //タイマ0割り込み許可 // enable_interrupts(INT_TIMER1); //タイマ1割り込み許可 // enable_interrupts(INT_TIMER2); //タイマ2割り込み許可 enable_interrupts(GLOBAL); // メインループ while(1) { //永久ループ if (start == 0) { //条件設定時のステップ if (input(PIN_A2)==0) { start = 1; icount1 = 0; delay_ms(DTIME); output_b(0xFF); //全ピンhigh output_high(PIN_A0); //8bit LED デコード output_low(PIN_A1); set_timer1(0x0BDC); enable_interrupts(INT_TIMER1); //タイマ1割り込み許可 } else { if (f_switch == 0) { output_high(PIN_A0); //ディレイ表示 output_low(PIN_A1); output_b(LED_SEG[delay1]); } else { output_low(PIN_A0); //刺激数表示 output_low(PIN_A1); output_b(LED_SEG[REP_H[repeat]]); delay_ms(3); output_high(PIN_A0); output_b(LED_SEG[REP_L[repeat]]); delay_ms(3); } if (input(PIN_A3)==0) { if (f_switch == 0) { delay1++; if (delay1 >= 11) delay1 = 0; } else { f_switch = 0; } delay_ms(DTIME); } if (input(PIN_A4) == 0) { if (f_switch == 1) { repeat++; if (repeat >= 5) repeat = 0; } else { f_switch = 1; } delay_ms(DTIME); } } } else { //連続刺激時のステップ if (f_mode == 0) { if (input(PIN_A2) == 0) { start = 0; disable_interrupts(INT_TIMER1); delay_ms(DTIME); } if (input(PIN_A4) == 0) { //連続刺激・外部刺激の切り替え f_mode = 1; disable_interrupts(INT_TIMER1); delay_ms(DTIME); } } else { if (input(PIN_A2) == 0) { icount1 = 0; output_b(0xFF); //全ピンhigh output_high(PIN_A0); //8bit LED デコード output_low(PIN_A1); set_timer1(0x0BDC); enable_interrupts(INT_TIMER1); //タイマ1割り込み許可 delay_ms(1000); } if (input(PIN_A4) == 0) { //連続刺激・外部刺激の切り替え f_mode = 0; output_b(0xFF); //全ピンhigh output_high(PIN_A0); //8bit LED デコード output_low(PIN_A1); set_timer1(0x0BDC); enable_interrupts(INT_TIMER1); //タイマ1割り込み許可 delay_ms(DTIME); } } } } }