Page 554 - 완) I MDP 프로젝트 작품 보고서(전체과 1학년)1.6
P. 554

EIMSK  = 1<<INT0;      //외부인터럽트  INT0  Enable
                EICRA =  (1<<CS01)|(1<<CS00);    //rising  edge
                sei();


                while(1){
                    pulse();    //10usec High  Pulse Output
                        if(pulse_count<8)  PORTB  =  1;   //8cm보다  작으면  led가  켜집니다.
                        else  PORTB  =  0;  //그외  조건에서는  꺼집니다.
                }
            }


            또한  응용하여,  아두이노에서  다음과  같이  사용  가능하다.
            int  Trig_pin  =  12;    //초음파  센서의  Trig핀
            int  Echo_pin  =  13;    //초음파  센서의  Echo핀

            void  setup()
            {
                Serial.begin(9600);              //시리얼  포트  초기화
               pinMode(Trig_pin,OUTPUT);  //트리거  핀을  출력으로  설정
               pinMode(Echo_pin,INPUT);    //에코핀을  입력으로  설정
            }

            void  loop(){
                Serial.print(UltrasonicMeasure());        //초음파  센서로  거리를  측정하여  출력
                Serial.println("cm");

                delay(300);
            }

            long  UltrasonicMeasure(void)        //초음파  센서로  거리를  출력하기  위한  함수
            {
                long  duration;
                digitalWrite(Trig_pin,  LOW);
                delayMicroseconds(2);
                digitalWrite(Trig_pin,  HIGH);
                delayMicroseconds(10);
                digitalWrite(Trig_pin,  LOW);
               duration  = pulseIn(Echo_pin,HIGH)  /  29 / 2;

                return  duration;

            }



                                                         -  547  -
   549   550   551   552   553   554   555   556   557   558   559