//-------------------------------------------------------------------------- // LEDの表示制御プログラム 16F876 //-------------------------------------------------------------------------- // 2016-05-05 Copyright@VividHobby //-------------------------------------------------------------------------- //------------------------------------------------------------------------- // プログラム初期設定 //------------------------------------------------------------------------- #include #include #include #pragma config FOSC = HS //高速オシレーター(4MHz以上)使用 #pragma config WDTE = OFF //ウォッチドッグタイマーOFF #pragma config CP = OFF //コードプロテクションOFF #pragma config PWRTE = ON //パワーアップタイマーON #pragma config LVP = OFF//PIC16F84Aでは未サポート //PICのクロック設定 #define _XTAL_FREQ 20000000 //PICのクロックをHzで設定(20MHz) //----------------------------------------------------------------------- // 点滅設定定義エリア //----------------------------------------------------------------------- // 各ポートごとに点滅するLEDの点滅方法を設定する // {n,m} n = 0 : ランダム点滅  m = 数字(0-F)で20msecから2000msecまで // n = 1 - C : 順次点灯消灯 m = 数字 (0-F) で点滅間隔設定 // n = D : 一定間隔で点滅繰り返し m = 数字 (0-F) で点滅間隔設定 //----------------------------------------------------------------------- const unsigned char parameter[13][3] = {// {1, 1, 3}, // ポートRA0用の設定 {2, 2, 3}, // ポートRA1用の設定。以下、RA2-RA4とRB0-RB7と続く {3, 4, 3}, // RA2 {13, 8, 5}, // RA3 {13, 16, 5}, // RA4 {13, 1, 8}, // RB0 {13, 2, 8}, // RB1 {0, 4, 5}, // RB2 {0, 8, 5}, // RB3 {0, 16, 5}, // RB4 {0, 32, 5}, // RB5 {0, 64, 5}, // RB6 {0, 128, 5}, // RB7 }; // ここまで //------------------------------------------------------------------------ // 変数初期設定 //------------------------------------------------------------------------ unsigned char portcnt; unsigned char smax; unsigned char scnt; unsigned char led_cnt[13] = {0}; //ポート毎のLED点滅周期のカウント値を記憶初期値0 int tmp; signed int m, n; //------------------------------------------------------------------------ // 関数定義 //------------------------------------------------------------------------ void serial_max(void); //シリアル点滅のポート数を計算する //void interrupt tmr0_int(void); //割り込み処理ルーチン void rndm(void); //ランダム点滅ルーチン void regular(void); //定期点滅ルーチン void seri(void); //順次点滅ルーチン void port_out(void); //指定ポートの点滅 void port_off(void); void port_on(void); //------------------------------------------------------------------------- // メインプログラム //------------------------------------------------------------------------- main() { //--------------------------------------------- // PICのPORT設定の初期化 //--------------------------------------------- //PICのポート設定 ADCON0 = 0b10000000; //アナログ使用しない ADCON1 = 0b00000110; //For degital I/O TRISA = 0b00000000; TRISB = 0b00000000; PORTA = 0b11111111; // ポートの初期値を設定する(Hで通常、LでON) PORTB = 0b11111111; //---------------------------------------------------------------- // TRM0 setting 割り込み使用、プリスケーラ256設定@20MHz // 割り込みは約13msec毎に発生する //---------------------------------------------------------------- OPTION_REG = 0b10000111; /// Prescaller 1/256 (3msec interval) INTCON = 0b00100000; GIE = 0; // 全体割り込み禁止 T0IE = 1; // TMR0 使用 T0IF = 0; // TMR0 割り込みフラッグクリア //---------------------------------------------------------------- // 変数初期値設定 //---------------------------------------------------------------- portcnt = 1; scnt = 1; smax = 0; RA0=0; __delay_ms(2000); RA0=1; __delay_ms(2000); //---------------------------------------------------------------- // メイン処理開始 //---------------------------------------------------------------- serial_max(); //シリアル設定の最大個数計算 while (1) { for (portcnt = 0; portcnt <= 12; portcnt++) { if( parameter[portcnt][0]==scnt)seri(); if( parameter[portcnt][0]==0)rndm(); if( parameter[portcnt][0]==13)regular(); } __delay_ms(50); } } //-------------------------------------------------------------------- // シリアル点滅のポート数を計算する //-------------------------------------------------------------------- void serial_max(void) { m = 0; smax = 0; for (m = 0; m <= 12; m++) { if ((parameter[m][0] >= 1)&(parameter[m][0] <= 12)) { if (smax < parameter[m][0]) smax = parameter[m][0]; } } } //-------------------------------------------------------------------- // ランダム点滅ルーチン //-------------------------------------------------------------------- void rndm(void) { if (led_cnt[portcnt] <= 0) { port_out(); //LED出力を反転 n = parameter[portcnt][2]; m = rand() / 5000*n; led_cnt[portcnt] = m; }else{ led_cnt[portcnt]--; } } //-------------------------------------------------------------------- // 定期点滅ルーチン //-------------------------------------------------------------------- void regular(void) { if (led_cnt[portcnt] <= 0) { port_out(); //LED出力を反転 led_cnt[portcnt] = parameter[portcnt][2]; //ポート毎の点滅時間をセット }else{ led_cnt[portcnt]--; } } //-------------------------------------------------------------------- // 順次点滅ルーチン //-------------------------------------------------------------------- void seri(void){ if(led_cnt[portcnt]==0){ led_cnt[portcnt]=parameter[portcnt][2]; port_on(); } led_cnt[portcnt]--; if(led_cnt[portcnt]==0){ port_off(); scnt++; if(scnt>smax)scnt=1; } } //-------------------------------------------------------------------- // 指定ポート出力の反転(配列の最大数を超えない様に) //------------------------------------------------------------------- void port_out(void) { if ((portcnt >= 0)&(portcnt <= 4)) { PORTA = parameter[portcnt][1]^PORTA; //portcntが1-4ならPORTA } else { PORTB = parameter[portcnt][1]^PORTB; //portcntが5-12ならPORTB } __delay_ms(1); } //-------------------------------------------------------------------- // 指定ポート出力をOFF(配列の最大数を超えない様に) //------------------------------------------------------------------- void port_off(void) { if ((portcnt >= 0)&(portcnt <= 4)) { PORTA = parameter[portcnt][1] | PORTA; //portcntが1-4ならPORTA } else { PORTB = parameter[portcnt][1] | PORTB; //portcntが5-12ならPORTB } __delay_ms(1); } //-------------------------------------------------------------------- // 指定ポート出力をON(配列の最大数を超えない様に) //------------------------------------------------------------------- void port_on(void) { if ((portcnt >= 0)&(portcnt <= 4)) { PORTA = ~(parameter[portcnt][1]) & PORTA; //portcntが1-4ならPORTA } else { PORTB = ~(parameter[portcnt][1]) & PORTB; //portcntが5-12ならPORTB } __delay_ms(1); } //---------------------------- end of program --------------------------