//********************************************************************** /* <超音波人体検出ユニット> */ //********************************************************************** #define LED PORTA.F2 #define SW PORTA.F3 //********************************************************************** unsigned int measurement() { unsigned int ad; unsigned char cnt; // ad = 0; for (cnt = 0; cnt < 50; cnt++) { ad += Adc_Read(1); } return (ad / 50); } //********************************************************************** void Pwm_Change_DutyEx(unsigned int duty_ratio) { CCPR1L = duty_ratio >> 2; CCP1CON.F6 = duty_ratio & 0b00000001; CCP1CON.F7 = (duty_ratio & 0b00000010) >> 1; } //********************************************************************** void main() { unsigned int ad, ave, offset; unsigned char buf[10], cnt, tmp; // OSCCON = 0b01110000; // クロックは8Mhz CMCON = 0b00000111; // コンパレータは使用しない。 ANSEL = 0b00000010; // AD1を使用する。 TRISA = 0b00111010; TRISB = 0b00001110; // Lcd_Custom_Config(&PORTB, 4, 5, 6, 7, &PORTA, 0, 7, 6); Lcd_Custom_Cmd(LCD_CURSOR_OFF); Lcd_Custom_Out(1, 1, "UltraSoundV2 "); Lcd_Custom_Out(2, 1, " JF3SFB 2007.12"); Delay_ms(1000); Lcd_Custom_Cmd(LCD_CLEAR); // Pwm_Init(40000); // 40kHz Pwm_Change_DutyEx((PR2 * 4) / 2); Pwm_Stop(); // LED = 0; offset = 512; // 1024÷2 // while (1) { // 40KHzの超音波を2msec間出力する。 Pwm_Start(); Delay_ms(2); Pwm_Stop(); Delay_ms(2); // 反射波を測定する。 ad = measurement(); // 測定値を表示する。 WordToStr(ad, buf); Lcd_Custom_Out(1, 1, buf); // 測定値をバー表示する。 tmp = ad / 57; for (cnt = 0; cnt < 16; cnt++) { if (tmp > cnt) Lcd_Custom_Chr(2, cnt + 1, 0xFF); else Lcd_Custom_Chr(2, cnt + 1, ' '); } // 測定値をオフセット値と比較し、検出していればLEDを1秒点灯する。 WordToStr(offset, buf); Lcd_Custom_Out(1, 9, buf); if (ad > (offset + 50)) { LED = 1; Delay_ms(1000); LED = 0; } // SWが押されていればオフセット値をセットする。 if (SW == 0) { offset = ad; } } } //**********************************************************************