Вы находитесь на странице: 1из 13

To control a DC Motor with your PC-Computer, you need to know how to control serial port and how to control

a dc motor with microcontroller. In other words, PC will control a microcontroller through serial port and the microcontroller will control the dc motor. Its simple and no need for complex codes because we will use ready libraries brought by MikroC for us. In this tutorial we will use:

1. MikroC Pro 2. Proteus Isis Professional 3. Virtual Serial Port Software


This tutorial is divided into two parts:

1. Connections and schematics 2. Programming and codes

Note:
For controlling the serial port we will use UART Terminal Tool in MikroC Pro Virtual Serial Port Software is a program used to add virtual Serial Ports (example: COM 1, COM 2 etc) for computers that dont have serial ports.

Connections and schematics:


Open Proteus and then add these components: Pic16F877A 2N2222 NPN Transistor Motor Green LED RED LED COMPIM

Figure 1

In this schematic, we connected the 1st pin in PORT B (RB0) to Green LED in series with the base of the 2N2222 Transistor. The collector of the transistor is connected to power terminal while the emitter is connected to one side of a dc motor. The other side of the motor is connected to the ground terminal. The other LED is connected to the 2nd pin of PORTB ( RB1 ). The Green LED is used for indication. It indicates that the motor is working. While Red LED is used to indicate that the motor is not working. Now, right click on the COMPIM Component and click Edit Properties

Figure 2

A window will open

Figure 3
Change Physical Port, Physical Baud Rate and Virtual Baud Rate as the following figure:

Figure 4

Do the following steps if you dont own a serial port in your computer (Virtual serial port is used only for simulations, if you want to do this tutorial in real, you have to buy USB to Serial Converter Cable) Open Virtual Serial Port Software:

Figure 5
Then click Add pair, to add two ports: COM1 and COM2

Notice the added ports on left side


Figure 6
When you finish this step, your computer has 2 virtual serial ports COM1 and COM2.Consequenly you can simulate our tutorial.

Programming and Codes:


Open MikroC and start new project with the following configuration:

Device Name: PIC16F877A Device Clock: 4.000000 MHz

Then write the following code:

char i; // Declare a character variable called i void main() { uart1_init(9600); // Initialize the uart which is responsible for serial communication delay_ms(100); // Delay - For better performance trisb=0b11000000; // Make first and Second pins of PORT B (RB0 & RB1) as OUTPUT portb=0; // Make all pins of PORTB LOW portb.b1=1; // RB1 is HIGH , thus turns on the Red LED - Means, the motor not working while(1) // Loop forever { if ( uart1_data_ready()==1) // If data is received by the Serial Port do the following { i=UART1_Read(); // Read the received data if ( i == 'A' ) { portb.b0=1; portb.b1=0;} // If the received data is "A" , make RB0 HIGH and RB1 LOW , the motor will work and the Green LED will light while Red LED will turn off if ( i == 'B' ) { portb.b0=0; portb.b1=1;} // if the received data is "B" , make RB0 LOW and RB1 High i.e stop the motor and light the Red LED }}} Then build the program

Our tutorial is finished, now we have to check if our program works well as well as our connections and serial port configuration. Open MikroC, then click tools, then click USART Terminal A window will open

Figure 7

Change the configuration in COM Port Settings box as follows and then click connect in commands box

Figure 8

Then open the proteus file of our project and click play button as the following :

Figure 9
Notice that the Red LED is turned on indicating that the motor is not working

Now open MikroC and then in USART Terminal, write the letter A in the send panel like the following figure:

Figure 10

Now go to the proteus project , you will notice that the motor start turning and the Green LED will turn on to indicate that the motor is working while the Red LED is off.

Figure 11

Again, open USART Terminal and send the letter B.You will notice that the motor stops and the Red LED is on indicating that the motor is not working while the Green LED is off.

Figure 12

Вам также может понравиться