Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC16f877 programming code for motor driver circuit

Not open for further replies.


Newbie level 3
Sep 15, 2007
Reaction score
Trophy points
Activity points
pic16f877 digital to analog

I am making a motor driver circuit using PIC16f877 microchip, dual full bridge driver L298 and L6210(diode chip)...I have been able to form a circuit but dont know how to programme my PIC chip...what i want to do is that i want to move the motor in both dirrections, depending on the input....can any one please help in giving me the PIC code to make my motor run in both dirrections using the components i have mentioned above...i will be extremely gratefull...thnx.

programming pic16f877

Well PIC is quite a broad functionality chip...and its quite easy...
to program it you can even use BASIC to learn about programming in basic search the net regarding PIC BASIC PRO and you'll get the compiler...
the simple forward reverse program for a PIC in basic is as below...when key is pressed at pin 1 of PortA then it will activate the Pin 1 of PortB on which the forward pin for motor is connected and when pin 2 of PortA then it will activate then Pin 2 of PortB on which the reverse pin for motor is connected

TRISA = %00000000
TRISB = %00000000
if PORTA.0 = 1 then
PORTB.0 = 1
PORTB.1 = 0
if PORTA.1 = 1 then
PORTB.1 = 1
PORTB.0 = 0
goto loop

well jamishid thnx a lot bro...really appreciate ur help...believe me the simple code u provided did help me a lot....
I have another small problem on the same thing...I want to control the speed of the motor and also want to stop it after it malkes certain number of cycles...Can u provide me with the sode of how to control the speed and the number of cycles if you have time? thnx alot once again man.

well as you said you're using a DC Motor so...for DC Motors there quite a number of defficiencies...
1. Speed is never constant as it depends on the current which you're providing...
2. Number of rotations is also never constant as it also depends on the current...
Note: DC motors draw a huge ammount of current during that causes the above 2 reasons to occur...

now as you need to control the speed of dc motor, its not impossible but not even easy...well to control the speed of DC motor you can use a variable resistor to control the input current, but as you need to do this automatically so you need to use a MOS as it can act as a variable resistor depending on the current being provided at the gate, so for this purpose you will also need a Digital to analog convertor which will convert the digital signals from the microcontroller to variable bit pattern which will be provided to the gate of the MOS which will then act as a variable resistor and in accordance to the channel being developed between the drain and source the current will pass, this will crete a variation of speed.
and as for the number of turns is concerened so thats completely impossible to take control of the rotations as the rotations of the dc motor vary from time to time depening of whats the load and what the current being provieded...

the best soulution for motor controlling is to use a STEPPER MOTOR instead of a DC Motor as stepper motors are completely under your control for each and every pulse and by the variation of pulse you can measure the turns and the number of ratations along with the speed.

warning: stepper motors can not sustain huge loads so if yo're using the motor to drive a heavy load then the best solution is to use DC Motor but if its for a small application or demonstration then Stepper Motor is the best solution

OK Using PIC and L293 driver.. u can build a ciruit.
If u able to develop a circuit for that ... just send ur circuit....

other wise u get it from through net. so decide on whch ckt ur implementing the code...

so that i can help u...

Not open for further replies.

Part and Inventory Search

Welcome to