Monday 1 October 2012

WEEK 9

Next, I do the Visual Basic programming. This program is show the detected of the rat and store it as record on your computer. Below is the flow chart of Visual Basic:
  

The program of  Visual Basic:

Dim record_en As Integer
Dim mydata As String
Dim mysave As String

Private Sub Command1_Click()
    Text1.Text = ""
End Sub

Private Sub Command2_Click()

End Sub

Private Sub Form_Load() 'when program open
    record_en = 1
    MSComm1.PortOpen = True
    Open "c:\tikus\record.txt" For Append As #2    'open data file
    Close #2    'close file
End Sub

Private Sub Form_Unload(Cancel As Integer)  'when program close
    MSComm1.PortOpen = False
End Sub

Private Sub MSComm1_OnComm()    'if any data in
    mydata = MSComm1.Input
  
    If mydata = "1" Then
        Label1.Caption = "Rat Detected"
  
        If record_en = 1 Then
            Open "c:\tikus\record.txt" For Append As #2     'open new file
            mysave = "Rat Detected" & "   " & Day(Date) & "-" & Month(Date) & "-" & Year(Date) & "    " & Format(Now, "h:nn:ss")
            Text1.Text = Text1.Text & vbCrLf & mysave
            Print #2, mysave  'save new record
            record_en = 0
            Close #2    'close file
        End If
    Else
        Label1.Caption = "No Rat Detected"
        record_en = 1
    End If

End Sub


Programming for Transmitter:

#include <16f877a .h=".h">  //use pic16f877a
#use delay(clock=20000000) //operating speed 20mhz
#fuses hs,nowdt,protect //default fuse setting
#use rs232(baud=4800, xmit=PIN_C6, rcv=PIN_C7, parity=N) //rf setting
#define use_portb_lcd TRUE //use portb for lcd
#include   //call lcd.c library

//set pic port address
#byte PORTA=5
#byte PORTB=6 
#byte PORTC=7 
#byte PORTD=8 
#byte PORTE=9 

void main()
{
   int i;

   //set io for each pic pin
   set_tris_a(0b00000000);
   set_tris_b(0b10000000);
   set_tris_c(0b10000000);
   set_tris_d(0b00000000);
   set_tris_e(0b00000000);

   setup_ccp2(CCP_PWM);
   output_high(pin_c4);  //on transmitter

   output_high(pin_e1);  //on green led
   output_high(pin_e0);  //on red led
   output_high(pin_e2);  //on blue led

   setup_timer_2(T2_DIV_BY_16, 149, 1);   //DO, 2097Hz
   set_pwm2_duty(75);                        
   delay_ms(500);             

   setup_timer_2(T2_DIV_BY_16, 133, 1);   //RE, 2350Hz
   set_pwm2_duty(66);                        
   delay_ms(500);                           

   setup_timer_2(T2_DIV_BY_16, 119, 1);   //MI, 2626Hz
   set_pwm2_duty(60);          
   delay_ms(500);             

   setup_timer_2(T2_DIV_BY_4, 31, 1);   //40khz
   set_pwm2_duty(0);    //off output                          
   delay_ms(100);             

   do
   {
      output_high(pin_e2); //on blue led

      if(input(pin_b7)==1) //if motion detected
      {
         set_pwm2_duty(15);   //output 40khz                              

         output_low(pin_e1);  //off green led
         output_high(pin_e0); //on red led

         //send data out
         for(i=0;i<10 br="br" i="i">         {
            putc('(');
            putc('1');
            putc(')');
            delay_ms(10);
         }

         delay_ms(5000);   //wait for 5sec
      }
      else  //if no motion detected
      {
         set_pwm2_duty(0);    //off output                          

         output_high(pin_e1);  //on green led
         output_low(pin_e0);  //off red led

         //send data out
         for(i=0;i<10 br="br" i="i">         {
            putc('(');
            putc('0');
            putc(')');
            delay_ms(10);
         }     
      }

      output_low(pin_e2); //off blue led
      delay_ms(500);             

   }while(1);


}



 Programming For Receiver:

#include <16f877a .h=".h"> //use pic16f877a
#use delay(clock=20000000) //set pic speed 20mhz
#fuses hs,noprotect,nowdt,nolvp  //default setting
#use rs232(baud=4800, xmit=PIN_C6, rcv=PIN_C7, parity=N) //set serial interfacing

#byte PORTA=5
#byte PORTB=6
#byte PORTC=7
#byte PORTD=8
#byte PORTE=9

int rx_temp1=0;
int rx_temp3=0;
int rcvdata;
int mystat=0;

//receive data from transmitter
#int_rda
void serial_isr()
{
   rx_temp1=getch();
   if(rx_temp1=='(')
   {
      rcvdata=getc();
      rx_temp3=getch();
      if(rx_temp3==')')
      {
         mystat=1;
      }
   }

   rx_temp1=0;
   rx_temp3=0;

}

void main()
{
   set_tris_a(0b00000000);    //set i/o pin for port a
   set_tris_b(0b00000000);    //set i/o pin for port b
   set_tris_c(0b10000000);    //set i/o pin for port c
   set_tris_d(0b00000000);    //set i/o pin for port d
   set_tris_e(0b00000000);    //set i/o pin for port e

   //initialize interrupt
   enable_interrupts(int_rda);
   enable_interrupts(GLOBAL);

   output_high(pin_c5);  //on receiver
   output_high(pin_c0);  //on blue led
   delay_ms(500);
   output_low(pin_c0);   //off blue led
   delay_ms(500);

   do
   {
      //if RF receiver received valid data
      if(mystat==1)
      {
         mystat=0;
         output_high(pin_c0);  //on blue led
         delay_ms(250);
         output_low(pin_c0);   //off blue led
         delay_ms(250);
        
         if(rcvdata=='1')  //if receive 1
         {        
            printf("1");   //send 1 to pc
         }
         if(rcvdata=='0')  //if receive 0
         {        
            printf("0");   //send 0 to pc
         }
      }

   }while(1);
}













No comments:

Post a Comment