How To Control speed and direction of two motors

The motor direction is controlled by buttons connected to ports A4 and A5. Pushing the button changes the direction rotation of the corresponding motor. Releasing a button returns the motor direction to its original state. Both motors run from a single PWM controlled by the pot. Moving the pot tip towards its upper end on the schematic leads to increasing the rotation speed.

To control the motor speed we use PWM as in the previous experiments. The PIC reads the voltage coming out of the pot, which is in the range 0..5V, and redirects it to a built-in ADC with a 10-bit resolution. In this case, however, its two lower bits are ignored. The range of ADC values (0..255) is send directly to the PWM duty cycle ratio control register. This way we get an 8-bit PWM resolution. The PWM period is set to 244Hz.






The main part of the program is an endless loop. At each iteration of this loop we read the ADC value and the buttons stati. The input ports A4 and A5 have weak pull-ups enabled. This results in the logical 1 value at these inputs if the buttons are not pushed. Pushing a button modifies this value to 0 and this is used to modify the direction of the corresponding motor. The direction is modified by complementing the corresponding pair of outputs RC3, RC4 or RC1, RC2. The complementing changes the voltage signs on the motor pins.

Note that in this implementation the motor speed is the same, since they are controlled by a single PWM. It is important to know that PWM runs in its own thread and releases the CPU for other operations. There is just one PWM thread in PIC16F684. Due to a difference of motor parameters, the control is not very reliable at very low voltages, as one or both motors might not rotate at all as they need a certain voltage to start. Furthermore, by the same reason the rotation speed at very low voltages might be different. This difference disappears as the pot tip moves about 1/4 of its range counted from the grounded end.

Download Source Code (PIC Microcontroller)
0 Comments
Disqus
Fb Comments
Comments :

0 comments:

Post a Comment