This motor can be obtained very cheaply. It is a stepper motor but has gearing contained within it and so it takes about 4000 steps per revolution (half stepping). I think it is used for car door wing mirrors and so is not expected to go too fast, for that application its is ideal and will give smooth control.
The motor is normally supplied with a PCB and an ULN2003 driver IC as shown above. For the library code here PORTB 0 to 3 are used. Making the port pins consecutive reduces the complexity of the code.
There is an excellent description of how this motor works here. It is driven using half step mode, hence 8 steps. Full step mode would only require 4 steps.
The code is made simpler because the port pins are consecutive and so there is no need to reference each pin.
The main function is called step() which reads a value from the table and applies it to the port pins. The table is either read forward or backward depending on the direction.
The following two lines of code are worth a mention:
We can take advantage of the PIC32 clear and set registers and also of rookie so that this code will work both on the MX1 and the MX3.
From rookie PORTB is actually not the address of the port but the address of the TRIS register. PORTB+LAT_OFF+CLR is in fact the address of LATBCLR, you can verify this by typing:
print hex$(PORTB+LAT_OFF+CLR), this will return bf886134 which looking at the library can be verified that this is in fact LATBCLR
The above is value is for the MX1, if using MX3 the value will be different.
Getting back to the two lines of code, we first clear bits 0-3 of PORTB and then we set bits 0-3 of PORTB using the value from the stepper table. Only the '1's will of course change. This technique can be sued for other situations where just certain bits of a port require modification without effecting the other bits on the same port.
There is also a delay introduced, this is necessary as the steps to the motor would be sent to fast for it to respond. The delay can be modified as required.
|Click on link to see code. Use save link as to copy to local folder. To use with BV_COM, highlight link or use copy link location and then paste into the text transfer box|
The final function go() has two parameters, number of steps and direction. Lots of steps are needed as it takes about 4000 to do just one turn.