Easy Embedded Programming: Spin the Motor

Publié par

The final video wraps up the series and shows off your accomplishments. In the end, the speed of the motor will be controlled by the distance between your hand and the proximity sensor.

In the previous video, we learned how to drive a pin at various frequencies with or without a PWM, how to generate interrupt to perform an action every x microseconds thanks to an interrupt and a timer, and how to communicate with external components by programming an I2C and an SPI bus. Combining all this knowledge, we can now create a project where the speed of the motor will depend on the distance between our hand and the proximity sensor. Again, for the microcontroller we use the PSOC4200 BLE from Cypress, the proximity sensor is the VL6180 from STMicroelectronics and the stepper motor driver is the L6474 from STMicroelectronics. The device with the I2C was a proximity sensor and the device with the SPI bus was a stepper motor driver. 

Voir les produits connexes

VL6180XV0NR/1

STMicroelectronics Capteur spécialisé Afficher

Voir les produits connexes

L6474HTR

STMicroelectronics Contrôle de mouvement du moteur Afficher
 
The project has the following blocks: a 32 kilo Hertz clock connected to a timer with a period of eight thousand counts. This timer triggers an interrupt every 250 ms where we will check the value of the proximity sensor. 
 
The 32kHz clock feeds a PWM to generate a square signal. A period of 320 counts combined with the 32kHz clock drives the motor at a speed of 100 step/s. In order to have a 50% duty cycle, the compare value of half of the period value. To change the speed of the motor, we will overwrite these values in the program. The output line of the PWM is connected to the STEP pin of the L6474 and each rising edge on this pin moves the motor of one step.
 
The I2C block allows communicating with the proximity sensor VL6180 and the SPI bus communicates to the stepper motor driver L6474. The I2C is programmed as a master at 100kHz.
 
The I2C SCB component uses an internal interrupt that has a priority level of 3 by default. As in the program it is called while we are in the interrupt service routine triggered by the timer that has also a default priority value of 3, it is never called and there is no I2C communication. If we change the interrupt level of the I2C at 2, it is then called and the I2C communication is then performed correctly.
 
Arduino pinout constraints generate constraints to use the I2C and the SPI in SCB mode. Indeed it would need to be in the same SCB component. To overcome this issue, the SPI master in the UDB is used instead of the SPI of the SCB. APIs are very similar. In the UDB mode, pin connections are not automatic, an pin components must be added in the schematic.
The polarity of the SPI bus is programmed with CPHA = 1 and CPOL = 1, MSB first and data bits equal to 8. The MISO pin is the input pin and the MISO, CLOCK and Chip Select pins are configured as outputs in strong drive mode for all of them.  

two more pins are necessary : a reset pin and a direction pin.
Regarding the program, the SPI, I2C, Timer and PWM blocks must be started. We also calls methods programmed in the L6474.c file to reset and configure the L6474. The timer is programmed with an interrupt: in the interrupt, we start to read a value from the proximity sensor. Based on the distance, a new speed is calculated and the PWM period and compare values are overwritten. The feedback from the proximity sensor is between 0 (object close to sensor) and 255 (no object detected). The closer the object is, the faster the motor spin.
 
In the L6474.c file, we have a method to reset the motor driver by changing the level of the reset pin with some delays, a 2nd method to send an SPI command 
 
and a third one to configure the driver and call the method sending the SPI packets. 
 
The code was not optimized in order to make it very easy to read. Please refer to the L6474 reference manual to understand the configuration parameters.
Finally, in the VL6180.c file, we program the method for the proximity sensor.  Before we can use the proximity sensor, we need to configure it and confirm the device is ready. Please read the VL6180X specifications for more details.
 
 
We just follow the recommendations of an application note about the VL6180. We perform a range measurement in single shot mode. So in our program, the following methods:

- to start the measurement, 
- wait for the measurement to be ready and finally a method to read the value.
- Read the value of the proximity sensor
 
The final step is to build and program the board. The motor spins at low speed (100 step/s) and when a project get close to the proximity sensor, the motor can spin up to 1,000 step/s the distance, depending on the distance, the speed will vary between these two values.

Vidéos récentes

Désolé, le filtre sélectionné n’a renvoyé aucun résultat.

Nous avons mis à jour notre politique de confidentialité. Prenez un moment pour lire les changements. En cliquant sur "J'accepte", vous acceptez la clause de confidentialité d'Arrow Electronics ainsi que les conditions d'utilisation.

Notre site Internet place des cookies sur votre appareil pour améliorer votre expérience et pour améliorer notre site. Pour en savoir plus sur les cookies que nous utilisons et la façon de les désactiver, cliquez ici. Des cookies et des technologies de suivi peuvent être utilisés à des fins de marketing. En cliquant sur « Accepter », vous consentez au placement de cookies sur votre appareil et à notre utilisation de technologies de suivi. Cliquez sur « En savoir plus » pour de plus amples informations et instructions sur la façon de désactiver les cookies et les technologies de suivi. Même si l'acceptation des cookies et technologies de suivi est volontaire, leur désactivation peut entraîner un mauvais fonctionnement du site Internet et certaines publicités peuvent être moins pertinentes pour vous. Nous respectons votre confidentialité. Lisez notre politique de confidentialité ici.