/******************************************** * C言語プログラム例 * File Name : Illumi2.c * ソフトPWMでフルカラーLEDのイルミネーション ********************************************/ // ヘッダファイルインクルード #include <12f675.h> #include // コンフィギュレーション設定 #fuses HS, NOWDT, PUT, NOPROTECT, MCLR /// 色相変化周期 Interval値 #define Max 1023 /// 変数宣言 long Interval, Period, R_Duty, G_Duty, B_Duty; int Base, Color; int R_Flag, G_Flag, B_Flag; // 昇降フラグ /// タイマ1割り込み処理 /// 各色のデューティを上下 /// 約13msec周期 13×1024=約13秒 #INT_TIMER1 void isr(void) { Interval++; // 色相インターバル更新 if(R_Flag) { // 赤色デューティ更新 R_Duty--; // 下降中 if(R_Duty <= 1) R_Flag = 0; } else { R_Duty++; // 上昇中 if(R_Duty >= Max) R_Flag = 1; } if(G_Flag) { // 緑色シューティ更新 G_Duty--; // 下降中 if(G_Duty <= 1) G_Flag = 0; } else { G_Duty++; // 上昇中 if(G_Duty >= Max) G_Flag = 1; } if(B_Flag) { // 青色デューティ更新 B_Duty--; // 下降中 if(B_Duty <= 1) B_Flag = 0; } else { B_Duty++; // 上昇中 if(B_Duty >= Max) B_Flag = 1; } } // メインプログラム void main(){ output_a(0); // LED全オフ /// Setup Timer1 // 約13msec周期 setup_timer_1(T1_INTERNAL | T1_DIV_BY_1); set_timer1(0); /// Initial set R_Flag = 0; // 昇降フラグオフ G_Flag = 0; B_Flag = 0; Period = 0; // デューティ周期 Interval = 0; // 色相周期 Base = read_eeprom(1); // ランダマイズ Base++; // 基数更新 格納 write_eeprom(1, Base); srand(Base); // デューティ初期値設定 R_Duty = (int)rand() << 2; G_Duty = (int)rand() << 2; B_Duty = (int)rand() << 2; Color = (int)rand() & 0x07; // 色相初期値セット if(Color==0) Color = 0x07; /// タイマ1割り込み許可 enable_interrupts(INT_TIMER1); enable_interrupts(GLOBAL); /// メインループ while(1) { /// デューティの制御 if(Period >=1024) { /// 色相の制御 if(Interval >= Max * 10) { Interval = 0; Color = (int)rand() & 0x07; if(Color==0) Color = 0x07; } /// デューティの最初でLEDオン Period = 0; output_a(Color); } else{ /// デューティ一致でLEDオフ if(Period == R_Duty) output_low(PIN_A1); if(Period == G_Duty) output_low(PIN_A0); if(Period == B_Duty) output_low(PIN_A2); Period++; } } }