//
//
//NJR4265sensor.c by PIC18F25K22. 窓防犯システムNJR4265＋赤外線リモコン。
// with NJR4265controller.c
//
//UART1/2 外部発信器OSC 8MHZ クロックの4逓倍で32MHZ
//
//UART1はNJR4265との9600BPI通信(パリティ奇数)
//UART2は赤外線リモコン受光素子からの300BPI通信
//RA0とRA1にプッシュスイッチ入力。受光部切換と外部出力ON/OFF
//RA4とRA5にDIPスイッチ入力。赤外線識別コード設定用。
//RB0とRB1は赤外線受光部の切換のため74HC02を制御
//ICSP書込み対応。電源スイッチ(DIP2P)のON/OFFは行う事。
//閾値端のとき、パイロットLEDの点滅は速くなる
//閾値変更又はスイッチ動作後5秒してから、EEPROM書込み追加。0番地threshold下位バイト、1番地にthreshold上位バイト、2番地にsw_dat1、3番地にsw_dat2
//
//  create 2015.12.17
//  update
//
#include <18f25K22.h>    //*
//#include <string.h>
#fuses HSM,PLLEN,PRIMARY,NOFCMEN,PUT,NOBROWNOUT,WDT128,NOPBADEN,MCLR,NOLVP,NODEBUG,NOPROTECT//クロックの4逓倍のためPLLEN、WDTは4ms×128=512ms
//ESM High speed Osc, medium power 4MHz-16MHz
//PLLEN     HW PLL enabled
//PRIMARY Primary clock is system clock when scs=00
//NOFCMEN Fail-safe clock monitor disabled
#use delay(CLOCK =  32000000)                            //クロック周波数指定。setup_oscillatorと一致させること。
#use rs232(BAUD = 9600, XMIT = PIN_C6, RCV = PIN_C7, PARITY=O, STREAM=njr4265)//パリティ有り★
//#use rs232(BAUD = 9600, XMIT = PIN_C6, RCV = PIN_C7, STREAM=njr4265)//
#use rs232(BAUD = 300, XMIT = PIN_B6,RCV = PIN_B7, ERRORS, STREAM=ir, )// ERRORSフリーズ防止のつもり
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(E)
//ポートのio指定は以下で行うこと。
#define port_a_io       0b10110011//
//RA0   :in Push switch アクティブLOW
//RA1   :in Push switch アクティブLOW
//RA2   :out 外部出力DEP(離反)
//RA3   :out 外部出力APP(接近)
//RA4   :in DIP switch アクティブLOW
//RA5   :in DIP switch アクティブLOW
//RA6   :out osc2
//RA7   :in  osc1
#define     in_pushsw1  PIN_A0
#define     in_pushsw2  PIN_A1
#define     out_dep     PIN_A2
#define     out_app     PIN_A3
#define     in_dipsw0   PIN_A4//識別コード設定用。
#define     in_dipsw1   PIN_A5//識別コード設定用。
//
#define port_b_io         0b10000000
//RB0   :out 受光部切換
//RB1   :out 受光部切換
//RB2   :out led
//RB3   :out led
//RB4   :out led
//RB5   :out led
//RB6   :out TX2.usart2
//RB7   :in  RX2.usart2
#define     out_hc02_1              PIN_B0//lowアクティブ 受光屋内
#define     out_hc02_2              PIN_B1//lowアクティブ 受光屋外　
#define     led_output_rr           PIN_B2//外部出力ON/OFF  right red
#define     led_switchout_rg        PIN_B3//受光部切換　屋外right green
#define     led_outdoor_dep_rb      PIN_B4//屋外離反        right blue
#define     led_indoor_pilot        PIN_B5//屋内パイロット
//
#define port_c_io       0b10000000
//RC0   :out led 
//RC1   :out led 
//RC2   :out led
//RC3   :out led
//RC4   :out led
//RC5   :out led
//RC6   :out TX1.njr4265
//RC7   :in  RX1.njr4265
#define   led_outdoor_app_lr    PIN_C0//屋外接近            left red
#define   led_outdoor_pilot     PIN_C1//屋外パイロット
#define   led_receive_lg        PIN_C2//赤外線受信          left green
#define   led_switchin_lb       PIN_C3//受光部切換　屋内    left blue
#define   led_indoor_dep_rb     PIN_C4//屋内離反            right blue
#define   led_indoor_app_lr     PIN_C5//屋内接近            left red
//#define   out_app_led      PIN_C0
//#define   out_dep_led      PIN_C1
//#define   out_max_led      PIN_C2
//#define   out_min_led      PIN_C3
//#define   out_aux_led      PIN_C4
//#define   out_rcv_led      PIN_C5
//
#define port_e_io       0b1000//
//RE0   :out not used.ピン無し
//RE1   :out not used.ピン無し
//RE2   :out not used.ピン無し
//RE3   :in  MCKR
//////
#define timer0_intervalr5  0xC2F7  //0.5秒=( (1/clock= 32MHZ)×4×256×15625 )。2^16-15625=49911(C2F7)からスタートしてオーバーフローさせて割込0を使用。
#define timer0_intervalr25 0xE17B  //0.25秒=( (1/clock= 32MHZ)×4×256×7813 )。2^16-15625=57723(E17B)からスタートしてオーバーフローさせて割込0を使用。
#define timer1_interval 0xFC18  //1ms=( (1/clock= 32MHZ)×4×8×1000 )。2^16-1000=64536(FC18)からスタートしてオーバーフローさせて割込1を使用。
#ROM 0xF00000 = {0x0258,0x0002}  //プログラムをライターで書き込み際にeeprom記録。
//0番地threshold下位バイト、1番地にthreshold上位バイト(threshold=600)、2番地にsw_dat1=2(屋外受光部)、3番地にsw_dat2=0
////// その他
static int1     timer0_500ms = 0;       //0.5秒毎に1となるフラグ。
static int1     sw_out_rcv;             //赤外線受信時のLED点灯時間制御
static int1     sw_in_pushsw1;              //スイッチ印加して初めてかそれ以降かを区別する
static int1     sw_in_pushsw2;              //スイッチ印加して初めてかそれ以降かを区別する
//static int1     timer1_1ms = 0;       //1ms毎に1となるフラグ。
//static int1     timer1_50ms = 0;      //50ms毎に1となるフラグ。
static int      result;//
static int      code;
static long     threshold;              //閾値
static long     received;               //
static long     wrk;                    //
static long     wrkhigh_16;
static long     wrklow_16;
static int32    wrkhigh_32;
static int32    wrklow_32;
static int1     int_njr4265;            //njr4265外部割込フラグ
static int1     int_ir;                 //ir外部割込フラグ
static char     string1[30] = {""};     //string1[0]、[1]、[2]にデータは入る。UART(ir)のデータ。
//static char     string2[30] = {""};
static int      cnt;                    //UART(ir)の受信バイト数
static int      cnt_ms;                 //UART(ir)3バイト受信に必要時間上限のカウント。ms
static int      cnt_0R5s;               //スイッチ又はUART(ir)からの入力後からの経過時間 0.5s
static int1     f_eeprom;
static int      cnt_err;                //UART(ir)通信エラー。RCSTAXレジスタのエラービット
static long     cnt_out_rcv;            //赤外線受信時のLED点灯時間制御
static long     cnt_in_pushsw1;             //スイッチ1の印加時間のカウントms
static long     cnt_in_pushsw2;             //スイッチ2の印加時間のカウントms
static int      sw_dat1;//1、2、3 　    //「in_pushsw1」の印加によって、受信モジュールを変更する際のカウンタ。1=無し　2=屋外　3=屋内
static int1     sw_dat2;//0/1           //「in_pushsw2」の印加によって、外部出力を出すか出さないか変更する際のカウンタ　0=出力しない　1=出力する
static int      distinction;            //識別コード(混信防止コード)
static char     chr[20] = {""};         //UART(njr4265)のデータが入る。可変。
//
void eeprom_write(void);
void send_threshold(void);
//
////////////////////////////////////////////////////////////////////
////// タイマー0割り込み0.5秒毎又は0.25秒毎
////////////////////////////////////////////////////////////////////
#INT_TIMER0
void intval1()
    {
        if (  (threshold == 1) || (threshold == 999) )
            {
                set_timer0(timer0_intervalr25);
            }
        else
            {
                set_timer0(timer0_intervalr5);
            }
        if ( sw_dat1 == 2 )//屋外のときだけパイロットLED点滅
            {
                output_toggle(led_outdoor_pilot);
            }
        else
            {
                output_low(led_outdoor_pilot);
            }
        output_toggle(led_indoor_pilot);
        cnt_0R5s = cnt_0R5s + 1;
        timer0_500ms = 1;
    }
////////////////////////////////////////////////////////////////////
////// タイマー1割り込み1ms毎
////////////////////////////////////////////////////////////////////
#INT_TIMER1
void intval2()
    {
        set_timer1(timer1_interval);
        //output_toggle(PIN_B1);
        cnt_ms = cnt_ms + 1;
        if ( cnt_ms > 120 )
            {
                cnt = 0;
                cnt_ms = 0;
                string1[0] = 0x0;
                string1[1] = 0x0;
                string1[2] = 0x0;
                //timer1_50ms = 1;
            }
        else
            {
            }
        //timer1_1ms = 1;
        cnt_out_rcv = cnt_out_rcv + 1;
        if ( cnt_out_rcv > 250 )
            {
                cnt_out_rcv = 0;
                sw_out_rcv = 1;
            }
        cnt_in_pushsw1 = cnt_in_pushsw1 + 1;
        cnt_in_pushsw2 = cnt_in_pushsw2 + 1;
    }
////////////////////////////////////////////////////////////////////
/////// USART1外部割込み　usart1が1byte受信したら、フラグを立てる
////////////////////////////////////////////////////////////////////
#INT_RDA
void intval4()
    {
        int_njr4265 = 1;
        fgets(chr,njr4265);//0d(\r)迄
        result = fgetc(njr4265);//0a(\n)
        clear_interrupt(INT_RDA);
        restart_wdt();
    }
////////////////////////////////////////////////////////////////////
/////// USART2外部割込み　usart2が1byte受信したら、フラグを立てる
////////////////////////////////////////////////////////////////////
//#INT_RDA2
//void intval5()
//    {
//        int_ir = 1;
//        //result = fgetc(ir);//0d(\r)
//        clear_interrupt(INT_RDA2);
//    }
//
////////////////////////
void  main() {
    //setup_oscillator(OSC_NORMAL);   //外部発振子
    //
    //SETUP_WDT(WDT_OFF);
    //disable_interrupts(INT_RDA);//UASRT1割り込み不許可
    //disable_interrupts(INT_TIMER0);//タイマー0許可
    //disable_interrupts(INT_TIMER1);//タイマー0許可
    //disable_interrupts(GLOBAL);//全割り込み許可
    //
    output_low(led_outdoor_app_lr);
    output_low(led_outdoor_dep_rb);
    output_low(led_indoor_app_lr);
    output_low(led_indoor_dep_rb);
    set_tris_E(port_e_io);
    set_tris_C(port_c_io);
    set_tris_B(port_b_io);
    set_tris_A(port_a_io);
    output_low(out_app);
    output_low(out_dep);
    output_low(led_outdoor_app_lr);
    output_low(led_outdoor_dep_rb);
    output_low(led_indoor_app_lr);
    output_low(led_indoor_dep_rb);
    output_low(led_outdoor_pilot);
    output_low(led_indoor_pilot);
    output_low(led_switchout_rg);
    output_low(led_switchin_lb);
    output_low(led_output_rr);
    output_low(led_receive_lg);
    output_high(out_hc02_1);
    output_high(out_hc02_2);

    //
    setup_adc_ports(NO_ANALOGS);
//
    //output_high(PIN_C3);//テスト用　クロック1出力
    int_njr4265 = 0;
    int_ir = 0;
    sw_out_rcv = 0;
    cnt_out_rcv = 0;
    cnt = 0;
    cnt_ms = 0;
    cnt_0R5s = 0;
    string1[0] = 0x0;
    string1[1] = 0x0;
    string1[2] = 0x0;
    cnt_err = 0;
    f_eeprom = 0;
//
    output_high(led_outdoor_app_lr);
    output_high(led_outdoor_dep_rb);
    output_high(led_indoor_app_lr);
    output_high(led_indoor_dep_rb);
    output_high(led_outdoor_pilot);
    output_high(led_indoor_pilot);
    delay_ms(200);
    output_low(led_outdoor_app_lr);
    output_low(led_outdoor_dep_rb);
    output_low(led_indoor_app_lr);
    output_low(led_indoor_dep_rb);
    output_low(led_outdoor_pilot);
    output_low(led_indoor_pilot);
    delay_ms(200);
    restart_wdt();
    output_high(led_outdoor_app_lr);
    output_high(led_outdoor_dep_rb);
    output_high(led_indoor_app_lr);
    output_high(led_indoor_dep_rb);
    output_high(led_outdoor_pilot);
    output_high(led_indoor_pilot);
    delay_ms(200);
    output_low(led_outdoor_app_lr);
    output_low(led_outdoor_dep_rb);
    output_low(led_indoor_app_lr);
    output_low(led_indoor_dep_rb);
    output_low(led_outdoor_pilot);
    output_low(led_indoor_pilot);
    /////////////////////////////////////////
    restart_wdt();
  //output_high(led_indoor_app_lr);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_indoor_app_lr);
    //
  //output_high(led_switchin_lb);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_switchin_lb);
    //
  //output_high(led_receive_lg);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_receive_lg);
    //
  //output_high(led_output_rr);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_output_rr);
    //
  //output_high(led_indoor_dep_rb);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_indoor_dep_rb);
    //
  //output_high(led_switchout_rg);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_switchout_rg);
    /////////////////////////////////////////////
  //output_high(led_outdoor_app_lr);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_outdoor_app_lr);
    //
  //output_high(led_switchin_lb);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_switchin_lb);
    //
  //output_high(led_receive_lg);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_receive_lg);
    //
  //output_high(led_output_rr);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_output_rr);
    //
  //output_high(led_outdoor_dep_rb);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_outdoor_dep_rb);
    //
  //output_high(led_switchout_rg);
  //delay_ms(400);
  //restart_wdt();
  //output_low(led_switchout_rg);
    //
   ////////////////
//
//  while ( kbhit(njr4265) == 1 )//usart1が受信したら1つ受信。バッファクリア
//      {
//        result = fgetc(njr4265);
//      }
//  delay_ms(2000);
//      while ( kbhit(njr4265) == 1 )//usart1が受信したら1つ受信。バッファクリア
//      {
//        result = fgetc(njr4265);
//      }
//  delay_ms(2000);
//      while ( kbhit(njr4265) == 1 )//usart1が受信したら1つ受信。バッファクリア
//      {
//        result = fgetc(njr4265);
//      }
//
    //ext_int_edge( L_TO_H );             //RB0/INT立ち上がり
//
    setup_timer_0(T0_INTERNAL | T0_DIV_256 );//内部クロック(Fosc/4、プリスケラー1/256、16bitレジスタ(記述無し)タイマー0の0.5s間隔用。
    //setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256 );//内部クロック(Fosc/4、プリスケラー1/256、16bitレジスタ(記述無し)タイマー0の0.5s間隔用。旧記述法
    setup_timer_1( T1_INTERNAL | T1_DIV_BY_8 );//内部クロック(Fosc/4、プリスケラー1/8、16bitレジスタ(記述無し)タイマー0の1.5ms間隔用。
//  未使用モジュールの停止
    setup_timer_2(T2_DISABLED,0,1); //未使用タイマーを止める
    setup_timer_3(T3_DISABLED);     //未使用タイマーを止める
    SETUP_CCP1(CCP_OFF);            //未使用モジュールを止める
    SETUP_CCP2(CCP_OFF);            //未使用モジュールを止める
    setup_adc(ADC_OFF);
//
//WDTテスト。foscクロック可変のテスト
    //SETUP_WDT(WDT_ON);              //WDTの開始(FUSEにて128倍(4ms×128)=約512msの指定有り)
    restart_wdt();
//
    //disable_interrupts(INT_EXT);//外部割り込み0許可
    enable_interrupts(INT_RDA);//UASRT1割り込み許可
    //disable_interrupts(INT_RDA2);//UASRT2割り込み不許可
    enable_interrupts(INT_TIMER0);//タイマー0許可
    enable_interrupts(INT_TIMER1);//タイマー0許可
    enable_interrupts(GLOBAL);//全割り込み許可
//
    clear_interrupt(INT_RDA);
    //clear_interrupt(INT_RDA2);
    clear_interrupt(INT_TIMER0);
    clear_interrupt(INT_TIMER1);
    //sw_dat1 = 2;
    //sw_dat2 = 0;
//
    set_timer0(timer0_intervalr5);
    set_timer1(timer1_interval);
    timer0_500ms = 0;
    //timer1_1ms = 0;
    //timer1_50ms = 0;
    //
    //EEPROMの読み込み
    result = read_eeprom(0);
    code = read_eeprom(1);
    threshold = make16(code,result);
    sw_dat1 = read_eeprom(2);
    sw_dat2 = read_eeprom(3);
//
    while(1)
        {
            //
            code = ( input_a() & 0b110000 )>>4;//DIPスイッチより識別コードの読み込み
            //
            if ( int_njr4265 == 1 )//usart1が受信したら 割込み処理内でデータ受信しているのでkbhit()を使わない。
                {
                    int_njr4265 = 0;
                    //
                    switch (chr[1])
                        {
                            case 0x45://アルファベットE
                                reset_cpu();
                                break;
                            case 0x57://アルファベットW
                                restart_wdt();
                                fprintf(njr4265,"@SP1\r\n");//NJR4265へ閾値=1設定コマンド送信 最低値を送って送信を止める
                                fprintf(njr4265,"@SM1\r\n");//NJR4265へ閾値=1設定コマンド送信 最低値を送って送信を止める
                                fprintf(njr4265,"@SP%Lu\r\n",threshold);//NJR4265へ閾値設定コマンド送信
                                fprintf(njr4265,"@SM%Lu\r\n",threshold);//NJR4265へ閾値設定コマンド送信
                                //reset_cpu();
                                break;
                            case 0x43://アルファベットC　接近
                                if ( sw_dat1 == 2 )//屋外のときだけ点灯
                                    {
                                        output_high(led_outdoor_app_lr);
                                    }
                                output_high(led_indoor_app_lr);
                                output_low(led_outdoor_dep_rb);
                                output_low(led_indoor_dep_rb);
                                if ( sw_dat2 == 1 )//外部出力有りの時だけトランジスタ出力on
                                    {
                                        output_high(out_app);
                                    }
                                break;
                            case 0x4c://アルファベットL　離反
                                if ( sw_dat1 == 2 )//屋外のときだけ点灯
                                    {
                                        output_high(led_outdoor_dep_rb);
                                    }
                                output_high(led_indoor_dep_rb);
                                output_low(led_outdoor_app_lr);
                                output_low(led_indoor_app_lr);
                                if ( sw_dat2 == 1 )//外部出力有りの時だけトランジスタ出力on
                                    {
                                        output_high(out_dep);
                                    }
                                break;
                            case 0x4e://アルファベットN　なし
                                output_low(led_outdoor_dep_rb);
                                output_low(led_indoor_dep_rb);
                                output_low(led_outdoor_app_lr);
                                output_low(led_indoor_app_lr);
                                output_low(out_app);
                                output_low(out_dep);
                                break;
                            default:
                                break;
                        } 
                }
            //
            if ( kbhit(ir) == 1 )//usart2が受信したら　300bpiと低速なので連続2バイトデータではあるが、1つずつ受信
                {
                    if ( cnt == 0 )
                        {                          
                            cnt_ms = 0;
                            cnt_err = 0;
                            string1[cnt] = fgetc(ir);//最上位バイトデータ
                            if ( ( RS232_ERRORS & 0x01 ) || ( RS232_ERRORS & 0x02 ) || ( RS232_ERRORS & 0x04 ) )//レジスタRCSTAXの下位3ビット
                                {
                                    cnt_err = cnt_err + 1;
                                }
                            cnt = 1;
                        }
                    else//
                        {
                            if ( cnt == 1)
                                {
                                    string1[cnt] = fgetc(ir);//中位バイトデータ
                                    if ( ( RS232_ERRORS & 0x01 ) || ( RS232_ERRORS & 0x02 ) || ( RS232_ERRORS & 0x04 ) )//レジスタRCSTAXの下位3ビット
                                        {
                                            cnt_err = cnt_err + 1;
                                        }
                                    cnt = 2;
                                }
                            else// cnt == 2
                                {
                                    string1[cnt] = fgetc(ir);//最下位バイトデータ
                                    if ( ( RS232_ERRORS & 0x01 ) || ( RS232_ERRORS & 0x02 ) || ( RS232_ERRORS & 0x04 ) )//レジスタRCSTAXの下位3ビット
                                        {
                                            cnt_err = cnt_err + 1;
                                        }
                                    wrkhigh_32 = make32(0,string1[0],string1[1],string1[2]);
                                    string1[0] = 0x0;
                                    string1[1] = 0x0;
                                    string1[2] = 0x0;
                                    wrklow_32 = 0b111111111111  & wrkhigh_32;
                                    wrklow_16 =  wrklow_32;
                                    wrkhigh_32 = wrkhigh_32>>12;
                                    wrkhigh_16 = wrkhigh_32;
                                    wrk = wrklow_16 | wrkhigh_16;
                                    received = wrklow_16 & 0b1111111111;//赤外線リモコンからの受信データ
                                    distinction = (make8(wrklow_16,1))>>2;
                                    if ( (wrk == 0xfff) && (cnt_err == 0) && (distinction == code) )//補数とのorがfff、エラーもなく、識別コードも一致
                                        {
                                            if ( (received > 0) && (received < 1000) )//この範囲のデータは閾値そのもの
                                                {
                                                    threshold = received;//受信した閾値を使う
                                                    send_threshold();//閾値変更
                                                }
                                            else                                    //範囲外のデータから±データがあるかチェック
                                                {
                                                    if ( received == 1005 )//+1
                                                        {
                                                            if ( threshold <= 998 )
                                                                {
                                                                    threshold = threshold + 1;//本機の現在の閾値より増減する
                                                                    send_threshold();//閾値変更
                                                                }
                                                        }
                                                    if ( received == 1009 )//-1
                                                        {   
                                                            if ( threshold >= 2 )
                                                                {
                                                                    threshold = threshold - 1;
                                                                    send_threshold();//閾値変更
                                                                }
                                                        }
                                                    if ( received == 1013 )//+10
                                                        {
                                                            if ( threshold <= 989 )
                                                                {
                                                                    threshold = threshold + 10;
                                                                    send_threshold();//閾値変更
                                                                }
                                                        }
                                                    if ( received == 1017 )//-10
                                                        {   
                                                            if ( threshold >= 11 )
                                                                {
                                                                    threshold = threshold - 10;
                                                                    send_threshold();//閾値変更
                                                                }
                                                        }
                                                    }
                                        }
                            }//++
                        }
                }
            //
            //赤外線受信時のLED点灯時間制御
            if ( sw_out_rcv == 1)
                {
                    output_low(led_receive_lg);//赤外線エラー無く受信LED消灯
                }
            //
            //sw1(RA4)の印加
            if  ( input(in_pushsw1) == 0 )
                {
                    if  ( sw_in_pushsw1 == 0 )
                        {
                            sw_in_pushsw1 = 1;
                            cnt_in_pushsw1 = 0;
                        }
                    else
                        {
                            if ( cnt_in_pushsw1 == 50 )//50ms　印加確定
                                {
                                    f_eeprom = 1;//EEPROM書込みまでのカウント開始
                                    cnt_0R5s = 0;//EEPROM書込みまでのカウント開始
                                    cnt_in_pushsw1 = 51;//1msの割込み中にこのロジックを再び通ってしまうので、1度だけになるよう強制的に51にする。
                                    if ( sw_dat1 == 3 )
                                        {
                                            sw_dat1 = 1;
                                        }
                                    else
                                        {
                                            sw_dat1 = sw_dat1 + 1;
                                        }
                                }
                        }
                }
            else
                {
                    sw_in_pushsw1 = 0;
                    cnt_in_pushsw1 = 0;
                }
            //
            //sw2(RA5)の印加
            if  ( input(in_pushsw2) == 0 )
                {
                    if  ( sw_in_pushsw2 == 0 )
                        {
                            sw_in_pushsw2 = 1;
                            cnt_in_pushsw2 = 0;
                        }
                    else
                        {
                            if ( cnt_in_pushsw2 == 50 )//50ms 印加確定
                                {
                                    f_eeprom = 1;//EEPROM書込みまでのカウント開始
                                    cnt_0R5s = 0;//EEPROM書込みまでのカウント開始
                                    cnt_in_pushsw2 = 51;//1msの割込み中にこのロジックを再び通ってしまうので、1度だけになるよう強制的に51にする。
                                    if ( sw_dat2 == 0 )
                                        {
                                            sw_dat2 = 1;
                                        }
                                    else
                                        {
                                            sw_dat2 = 0;
                                        }
                                }
                        }
                }
            else
                {
                    sw_in_pushsw2 = 0;
                    cnt_in_pushsw2 = 0;
                }
            //
            //スイッチ1の状態を受光部切換とLEDへ反映する
            if ( sw_dat1 == 1)//1=無し
                {
                output_high(out_hc02_1);
                output_high(out_hc02_2);
                output_low(led_switchout_rg);
                output_low(led_switchin_lb);
                }
            if ( sw_dat1 == 2)//2=屋外
                {
                output_high(out_hc02_1);
                output_low(out_hc02_2);
                output_high(led_switchout_rg);
                output_low(led_switchin_lb);
                }
            if ( sw_dat1 == 3)//3=屋内
                {
                output_low(out_hc02_1);
                output_high(out_hc02_2);
                output_low(led_switchout_rg);
                output_high(led_switchin_lb);
                }
//
            //スイッチ2の状態をLEDへ反映する
            if ( sw_dat2 == 1 )
                {
                    output_low(led_output_rr);//外部出力する
                }
            else
                {
                    output_high(led_output_rr);//外部出力しない
                }
            //
            restart_wdt();
            //スイッチ又はUART(ir)からの入力後から一定経過後、EEPROMへ書込み
            if ( (f_eeprom == 1) && (cnt_0R5s > 10) )//5秒後
                {
                    f_eeprom = 0;
                    eeprom_write();
                }
            //
        }
}
//閾値変更
void send_threshold()
{
    f_eeprom = 1;//EEPROM書込みまでのカウント開始
    cnt_0R5s = 0;//EEPROM書込みまでのカウント開始
    output_high(led_receive_lg);//赤外線エラー無く受信LED点灯
    sw_out_rcv = 0;
    cnt_out_rcv = 0;
    fprintf(njr4265,"@SP1\r\n");//NJR4265へ閾値=1設定コマンド送信 最低値を送って送信を止める
    fprintf(njr4265,"@SM1\r\n");//NJR4265へ閾値=1設定コマンド送信 最低値を送って送信を止める
    fprintf(njr4265,"@SP%Lu\r\n",threshold);//NJR4265へ閾値設定コマンド送信
    fprintf(njr4265,"@SM%Lu\r\n",threshold);//NJR4265へ閾値設定コマンド送信
    //
    return;
}
//EEPROMへの書き込み
void eeprom_write()
{
    output_high(led_output_rr);//メイン内で元に戻る
    output_high(led_receive_lg);
    output_high(out_hc02_1);//赤外線受信の停止　メイン内で元に戻る
    output_high(out_hc02_2);//赤外線受信の停止　メイン内で元に戻る
    restart_wdt();
    fprintf(njr4265,"@SP1\r\n");//NJR4265へ閾値=1設定コマンド送信 最低値を送って送信を止める
    restart_wdt();
    fprintf(njr4265,"@SM1\r\n");//NJR4265へ閾値=1設定コマンド送信 最低値を送って送信を止める
    restart_wdt();
    delay_ms(100);
    restart_wdt();
    delay_ms(100);
    restart_wdt();
    delay_ms(100);
    restart_wdt();
    delay_ms(100);
    restart_wdt();
    delay_ms(100);
//
    result = make8(threshold,0);
    WRITE_EEPROM(0,result);
    result = make8(threshold,1);
    WRITE_EEPROM(1,result);
    WRITE_EEPROM(2,sw_dat1);
    WRITE_EEPROM(3,sw_dat2);
    restart_wdt();
    fprintf(njr4265,"@SP%Lu\r\n",threshold);//NJR4265へ閾値設定コマンド送信
    restart_wdt();
    fprintf(njr4265,"@SM%Lu\r\n",threshold);//NJR4265へ閾値設定コマンド送信
    output_low(led_receive_lg);
   //
    return;
}/////////////////////////////////////////////////////////
























