//********************************************************************** /* 【簡易フラッシャーV2】 */ //********************************************************************** #define BYTE unsigned short #define WORD unsigned int #define DWORD unsigned long // #define FLASH GPIO.B5 #define ON 1 #define OFF 0 // #define SW GPIO.B3 //********************************************************************** extern void main(); extern void flash_proc(); extern int ad_measurement(unsigned short channel); extern void PWM1_custom_Set_Duty(WORD duty_ratio); extern void led_blink(short cnt); //********************************************************************** //■■■メイン関数■■■ void main() { int ad, threshold; // OSCCON = 0b01110000; //clock=8MHz CMCON0 = 0b00000111; ANSEL = 0b00001011; TRISIO = 0b00011011; FLASH = OFF; //ADC初期化 ADC_Init(); ADCON0.VCFG = 1; //PWM初期化 PWM1_Init(5000); //pwm=5kHz PR2 = 0xFF; PWM1_custom_Set_Duty(150); //duty≒15% PWM1_Stop(); //LED点滅 led_blink(10); //基準照度測定 //threshold = ad_measurement(3); //基準照度読み込み threshold = EEPROM_Read(1); threshold = (threshold << 8) | EEPROM_Read(0); // while (1) { //照度測定 ad = ad_measurement(3); //基準照度と照度の比較 if (threshold > ad) { led_blink(1); // flash_proc(); //フラッシュ点灯 } //基準照度測定&書き込み if (SW == 0) { while (SW == 0) { led_blink(1); } led_blink(10); threshold = ad_measurement(3); EEPROM_Write(0, threshold & 0xFF); EEPROM_Write(1, (threshold >> 8) & 0xFF); } } } //********************************************************************** //■■■フラッシュ点灯関数■■■ void flash_proc() { double hv; // PWM1_Start(); // hv = 0.0; while (hv < 250000.0) { hv = ad_measurement(0); hv = hv * 2.4365234375 * 221.0; } // FLASH = ON; Delay_ms(10); FLASH = OFF; // PWM1_Stop(); } //********************************************************************** //■■■アナログデータ測定関数■■■ int ad_measurement(unsigned short channel) { long ad; int cnt; // ad = 0; for (cnt = 0; cnt < 100; cnt++) { ad += ADC_Get_Sample(channel); } return (ad /= 100); } //********************************************************************** //■■■PWMデューティ設定関数■■■ void PWM1_custom_Set_Duty(WORD duty_ratio) { CCPR1L = duty_ratio >> 2; CCP1CON.DC1B0 = (duty_ratio & 0b00000001) != 0 ? 1 : 0; CCP1CON.DC1B1 = (duty_ratio & 0b00000010) != 0 ? 1 : 0; } //********************************************************************** //■■■LED点滅関数■■■ void led_blink(short cnt) { while (cnt > 0) { FLASH = ON; Delay_ms(100); FLASH = OFF; Delay_ms(100); // cnt--; } } //**********************************************************************