Monday, March 18, 2013

Programmable for C Language to final year project


This is coding for ultrasonic sensor and PIC16F877A and voice module.

#include <16f877a.h>
#use delay(clock=20000000)
#fuses hs, noprotect, nowdt, nolvp

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N)

#byte PORTA=5
#byte PORTB=6
#byte PORTC=7

#define  sensor_sig  PIN_C0 //Pulse trigger input

long range;
long sensor_read();

void main()
{
  set_tris_a(0b00011111);
  set_tris_b(0b00000000); //RB as output
  set_tris_d(0b10000000);

   //initialize all ports
   porta=0;
   portb=0;
   portc=0;



   do{
      range=sensor_read()/10;  //read ultrasonics signal


      if
         (range>=0&&range<=50)

         {
          output_high(pin_d0); // voice command
          output_high(pin_d1); //  buzzer
          output_low(pin_d2);  // voice power supply
          delay_ms(3000);
          output_high(pin_d2);  // voice power supply


          }

      else if
         (range>=51&&range<=100)

        {
          output_high(pin_d0); // voice command
          output_low(pin_d1); //  buzzer
          output_low(pin_d2);  // voice power supply
          delay_ms(3000);
          output_high(pin_d2);  // voice power supply


          }

      else


         {
         output_low(pin_d0);
          output_low(pin_d1);
           output_high(pin_d2);
         }

      delay_ms(500);    //wait 500ms




   }while(1);
}

long sensor_read()
{
   long time_out=0;
   long sensor_cnt=0;

   output_low(sensor_sig);
   bit_clear(PORTC,0);
   delay_ms(100);
   output_high(sensor_sig);   //send a pulse out
   delay_us(5);               //for 5us
   output_low(sensor_sig);    //stop sending
   delay_us(600);             //wait for 600us
   bit_set(PORTC,0);

   //wait for feedback
   do{
      time_out++;
      delay_us(1);
   }while(time_out!=400 && input(sensor_sig)==0);

   //start count pulse
   if(time_out!=400)
   {
      do
      {
         sensor_cnt++;
         delay_us(1);
      }while(sensor_cnt!=20000 && input(sensor_sig)==1);
      delay_us(200);

      if(sensor_cnt==20000){
         return(0);
      }else{
         return(sensor_cnt);
      }
   }
   else
   {
      return(0);
   }
}

No comments:

Post a Comment