Pic16F877 Sending data via serial port

Status
Not open for further replies.

manve_13

Junior Member level 1
Joined
Mar 1, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,455
Hello guys,

I'm using 16f877 and Pic C for my project. I need to send the data to the PC that I read from my sensor. I read lots of things about this but I couldn't figure out exactly.

I'm reading the data from a quadrature encoder sensor. And I need to send this data to the PC as soon as I read. i need to see the angle variation on PC screen.

How can I manage this? Is there any premade codes to send data ?

Thank you
 

Hello,
Step1: if you wat yo see the angle variation on PC screen You should use GUI LABVIEW for monitering you data.
Step 2: Study about Serial Communication... It's very easy in LABVIEW you can find its example.
Step 3: 16F877 --------> Labview --------> Ghraph Show on sceen.
 

you can see encoder data on Hyper Terminal but for that u need to make a code/program which read from sensor then their should be supporting hardware for this.
Do u have made/written any code for this.
 


you have to use Hit and Trial Method to get exact results...... Try to make you program in less lines use less instruction avoid any delay..... better to get data of this sensor using Interrupt.... u know minimum instruction handling time in 1 us so if u use a delay then out put of sensor will not reach to software (PC) exactly as it is detected in real time so try to use less instructions..... These sensors are very sensitive and provide digital o/p to detect motion, speed and position of object....

See these attachments these will help you coding is also given there....
 

Attachments

  • QE speed and motion.PDF
    164.5 KB · Views: 103
  • Quadrature Encoder.PDF
    1.2 MB · Views: 150

Thank you for your replies guys.

I've already written some codes about reading data with Pic C. After that my friend will write a software with Visual basic to show the values on PC.

For example I have a float variable as `angle`. I want to send this via Serial Port. How can I do this? Do I have to turn this value to char to send?

Here this is the codes of my software

Code:
#include <16f877.h>
#device ADC=8                 // 8-bit ADC
#fuses XT, NOWDT, NOPROTECT, NOBROWNOUT, NOLVP, NOPUT, NOWRT, NODEBUG, NOCPD
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N, stop=1)
#use fast_io(a)               // Perform of I/O for Port A
#use standard_io(b)
//#use standard_io(c)



int right=0;
int left=0; 
float angle_1=0;
float angle_2=0;
float diff=0;
int go=0;
float angle=0;


#int_rb
void rb_isr()
{

 if(input(PIN_B4)==1||input(PIN_B5)==1)
   {
       if(right==0&&left==0)
       {
            if((input(PIN_B4)==1)&&(input(PIN_B5)==0))    //Eger vurus yeni ise burada sag veya soldan geldýgýný anlýcak
            {
                  puts("\rright swing");
                  right=1;
                  //goto sag;
             }
      
             if((input(PIN_B4)==0)&&(input(PIN_B5)==1)) 
             {
                 puts("\rleft swing");
                 left=1;
                 //goto sol;
              }
   
       }
      
      if (right==1&&left==0)
      {
      right:
          
         if((input(PIN_B4)==1)&&(input(PIN_B5)==0))    //ýlk sensor kontrolu
         {
            puts("right sensor");
            angle_1=angle;            //ilk açýyý al
         }
         
         if((input(PIN_B4)==0)&&(input(PIN_B5)==1))    //Eger 2.sensor aktif ise buradan 2. aciyi alacak.
         {                                         //eger sensör rangeleri birbirini kapsar ise sagi buraya koyabilirz
            puts("left sensor");
            angle_2=angle;
            diff=angle_2-angle_1;
            go=1;                //ýsýmýzýn býttýgýný programa solucek degýsken
            right=0;               //býr sonraki vurus icin degiþkeni sýfýrla
         }     
           
      }
     
        if (right==0&&left==1)
            {
     left:
                
               if((input(PIN_B5)==1)&&(input(PIN_B4)==0))    //ýlk sensor kontrolu
               {
                  puts("left sensor");
                  angle_1=angle;            //ilk açýyý al
               }
               
               if((input(PIN_B5)==0)&&(input(PIN_B4)==1))    //Eger 2.sensor aktif ise buradan 2. aciyi alacak. 
               {                                          //eger sensör rangeleri birbirini kapsar ise sagi buraya koyabilirz
                  puts("right sensor");
                  angle_2=angle;
                  diff=angle_2-angle_1;                 
                  left=0;               //býr sonraki vurus icin degiþkeni sýfýrla
                  go=1;                //ýsýmýzýn býttýgýný programa solucek degýsken
               }     
                 
            }
   }
 
}

#int_ext
void ext_isr()
{


   if(input(PIN_B1)==1)
      angle=angle+.1;
             
   else
      angle=angle-.1;
        

}







void main ()
{
   float battery;
   float angle_control;
   int battery_status;
   angle_control=angle;
   
   set_tris_A(00000001);
  // set_tris_B(00110011);
  
   setup_adc(ADC_CLOCK_INTERNAL);  // ADC clock frequency is equal internal clock frequency
   setup_adc_ports(AN0);    // AN0 is Analog.

   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   enable_interrupts(INT_RB);
   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);
   ext_int_edge(H_TO_L);

      while(TRUE)
         {
         
         angle=0;
         set_adc_channel(0);
         delay_us(10);
         battery = read_adc();
            
         if(battery>=250)
         battery_status=10;
         else if(battery>=225)
         battery_status=9;
         else if(battery>=200)
         battery_status=8;
         else if(battery>=175)
         battery_status=7;
         else if(battery>=150)
         battery_status=6;
         else if(battery>=125)
         battery_status=5;
         else if(battery>=100)
         battery_status=4;
         else if(battery>=75)
         battery_status=3;
         else if(battery>=50)
         battery_status=2;
         else if(battery>=25)
         battery_status=1;
         else if(battery>=0)
         battery_status=0;
         puts("battery_status");
         printf("%d\r",battery_status);
    
           
           output_C(0x00);
           
         
               while((right==0)&&(left==0));                 
             
              
               while(go==0)
                  {
                  while(angle_control==angle);
                  
                  angle_control=angle;
                  //printf("%f",angle_control);
                                         
                  }
               
               //disable_interrupts(INT_RB);
              puts("angle difference");
              printf("%f\r\r",diff);
              right=0;
              left=0;
              go=0;
                             
         }
         
}
 
Last edited:

Does anyone has any idea about this?
 

If you are ready to work with LabVIEW and NI-Card, then I can suggest you to buy LabVIEW 2010 and NI-6221 (PCI or USB) card and insert into a PC which having serial port, this two things are more than enough for you to solve this problem. If you are ready to buy NI-LabVIEW and PCI/USB - 6221 card, then I will share the code to you.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…