//********************************************************************** /*   <簡易近接センサー> */ //********************************************************************** #define BYTE unsigned short #define WORD unsigned int #define DWORD unsigned long #define ON 1 #define OFF 0 sbit LED at GPIO.B4; sbit SW at GPIO.B3; //********************************************************************** WORD measurement() { BYTE cnt; WORD ad; // ad = 0; for (cnt = 0; cnt < 50; cnt++) { ad += ADC_Get_Sample(0); } return(ad / 50); } //********************************************************************** void main() { WORD ad, limit; // OSCCON = 0b01110000; CMCON0 = 0b00000111; ANSEL = 0b00000001; TRISIO = 0b00101001; // PWM1_Init(1000); PWM1_Set_Duty(PR2 / 2); PWM1_Stop(); // ADC_Init(); // LED = OFF; limit = EEPROM_Read(0); limit <<= 8; limit |= EEPROM_Read(1); // while (1) { ad = measurement(); if (limit < ad) { LED = ON; PWM1_Start(); Delay_ms(100); LED = OFF; PWM1_Stop(); Delay_ms(100); } if (SW == 0) { limit = ad; EEPROM_Write(0, (limit >> 8) & 0xFF); EEPROM_Write(1, limit & 0xFF); } } } //**********************************************************************