The objective of this tutorial is to control 5 servo motors interfaced with PIC16F877A by sending commands serially using Labview.. This whole thing will be simulated on proteus which is an excellent simulation tool.As far as controlling the motor goes , it is explained clearly in my tutorial on servo motor.Now next task is to prepare labview for serial communication and then create the link between labview and proteus .

Labview is graphic programming language and is really easy to use as everything is already done by folks at national instruments.

So for serial communication there is already a built in VI in labview and we will be using that only and also some general string and integer functions.For serial communication VISA driver has to be installed .Below is the image of labview front panel.

The front panel includes control for serial communication like selecting the COM port , parity bits and some other controls. There is a knob for selecting the motor and a slider to select value to be sent to motor and it varies from -9 to +9. There is a button to control the flow of data . But the whole magic happens in block diagram so here is the image of that.


In block diagram as you can see everything is enclosed in a while loop. Flowchart for this piece of code is that a motor switch selects the motor and accordingly a case is selected therefore in all there are 5 cases one for each motor. In each case same thing happens i.e input from slider is checked whether it is greater or equal to 0 if yes then ‘+’ is sent or else a ‘-‘ is sent(the other case selector). Now only string can be sent through serial communication so everything has to be converted into string which includes motor no. , the value to be sent and also the sign of value and string terminator”/”. So if you select motor 3 and value 7 using the slider then data that is sent is ‘3+7/’.Instead of sending the string together , I’ll send it character by character which makes the handling of string easier on controller side.So for this task I’ll use “String Subset vi” to take out the characters from the string and then send it. Now whenever the switch is pressed then data is sent to controller. There is a delay in serial communication case so as to smooth the things a little bit because controller is already busy with controlling motor and if you poke it by sending data continuously  it gets pissed and stops working properly.

Now after this next thing is to download ” virtual serial port driver” . Since there is no physical connection between labview and proteus , using the above mentioned tool a pair of virtual serial ports are created which both labview and proteus can read . In the software just click add pair and a pair will be created . I have used COM4 for labview and COM3 for proteus.

Now we come to proteus and below is the image showing the connections.

.There is PIC16F877A  connected to DB9 connector (COMPIN in proteus library). It has to be configured by editing its properties (Right click on the symbol), COM3 is selected and baud rate is set 9600. Now to burn the code ,by loading the hex file by right clicking on controller selecting the program file and also change the controller frequency to 2OMhz.  Now everything is configured and here is the code which is compiled in MikroC.

unsigned int count,countt,i,del,d1,d2,d3,d4,j;

unsigned char output[4];
 unsigned char delayus_variable;
void interrupt()          // Interrupt service routine
{
 if(TMR0IF_BIT==1)
 {
 count+=1;
 TMR0IF_BIT=0;
 }

 }
 void delayus(unsigned char  x)
 {
 delayus_variable=(unsigned char)x;

 asm  NOP
 asm NOP
 asm decfsz _delayus_variable,f
 asm goto $-3
 }
 void uart()

 {        if(UART1_Data_Ready()==1)
 {


 output[j]=UART1_Read();// storing each character in an array
 j+=1;
 if( output[3]==47)  // checking of string termination 47 ascii for "/"
 {
 j=0;
 output[3]=49;  // resetting value of string terminator

 countt=(unsigned int) output[2];// typecasting the received string to int
 countt=countt-48; // converting the ascii value

 switch(output[0])          // output[0] is motor no.
 {
 case 49:                   // case for motor 1,49 ascii for 1
 if(output[1]==43)   // checking the sign for value received,43 ascii for '+'
 del=del+(countt*10) ; // incrementing the Delay value for motor

 else
 del=del-(countt*10) ;
 break;
 case 50:              //motor2
 if(output[1]==43)

 d1=d1+(countt*10) ;

 else
 d1=d1-(countt*10) ;
 break;
 case 51:           //motor 3
 if(output[1]==43){
 d2=d2+(countt*10) ;
 }
 else
 d2=d2-(countt*10) ;
 break;
 case 52:         //motor 4
 if(output[1]==43)
 {
 d3=d3+(countt*10) ;
 }
 else
 d3=d3-(countt*10) ;
 break;

 case 53:       // motor 5
 if(output[1]==43){
 d4=d4+(countt*10) ;
 }
 else
 d4=d4-(countt*10) ;

 break;
 }
 }
 }
 }
 void Delay(unsigned int cnt)
{
 unsigned char        i;

 i = (unsigned char)(cnt>>8);
 while(i>=1)
 {
 i--;
 delayus(253);
 }
 delayus((unsigned char)(cnt & 0xFF));
}
void main()
{    j=0;
 TRISD=0;
 PORTD=0;
 del=d1=d2=d3=d4=1488;       // initial delay value

 count=0;
 countt=0;
 TRISC=0xC0;   //bits 7 and 6 have to be set to use the USART
 SPEN_BIT=1;   //SPEN has to be set (Serial Port ENable)
 CREN_BIT=1;   //set the Continuous Receive ENable bit
 PORTC=0;
 OPTION_REG=0x84;    //Used for prescaling
 GIE_BIT=1;          // turning on the global interrupt
 PEIE_BIT=1;         // turning on the peripheral interrupt
 UART1_Init(9600);   // initialising the UART
 TMR0IE_BIT=1;        // turning on timer interrupt
 TMR0=100;
 while(1)
 {
 uart();      // calling UART function
 switch(count )    //  PWM case for different motor
 {
 case 1:           // motor1
 RC1_BIT=1;

 Delay(del);
 RC1_BIT=0;

 TMR0=100;
 break;
 case 2:            //motor2
 RC2_BIT=1;

 Delay(d1);
RC2_BIT=0;
 TMR0=100;
 break;
 case 3:      //motor3

 RC3_BIT=1;

 Delay(d2);
 RC3_BIT=0;
 TMR0=100;
 break;
 case 4:        //motor4
 RC4_BIT=1;

 Delay(d3);

 RC4_BIT=0;
 TMR0=100;
 break;

 case  5 :    //motor5
 RC5_BIT=1;

 Delay(d4);

 RC5_BIT=0;
 countt=0;

 count=0;
 TMR0=100;
 break;
 }
 }
}

The above code is similar to code mentioned in servo controller tutorial with 2 changes. First is the UART function and second is the Delay function.In UART  function each character received is stored in an array  like “output[0] stores the motor no.” . When string terminator character is received then   the increment value “output[2]” is converted to int because the value received through is string type . Now the value is in ASCII format so if data sent is 2 then value we have is 50 ,ASCII for 2  and to get original value 48 is subtracted. Now a case structure is used to select different motor (value in output[0]) and in each case value of delay is updated and the second case structure is for sign of value(output[1]).

The reason I chose to use my own delay function is that Mikroc predefined delay function “delay_us” will only accept constant value and our delay value is variable.

The above mentioned code works fine for me and below is the download link for every file and if you have any doubt or any suggestion just leave a comment.

http://www.box.net/shared/rj9qzgf9co