Easy Embedded Programming: Spin the Motor

게시자

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. 

관련 상품 참조

VL6180XV0NR/1

STMicroelectronics Specialized Sensors 보기

관련 상품 참조

L6474HTR

STMicroelectronics Motor Controller and Driver ICs 보기
 
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.

최신 동영상

죄송합니다. 선택한 필터와 일치하는 결과가 없습니다.

개인정보 보호정책이 업데이트되었습니다. 잠시 시간을 내어 변경사항을 검토하시기 바랍니다. 동의를 클릭하면 Arrow Electronics 개인정보 보호정책 및 이용 조건에 동의하는 것입니다.

당사의 웹사이트에서는 사용자의 경험 향상과 사이트 개선을 위해 사용자의 기기에 쿠키를 저장합니다. 당사에서 사용하는 쿠키 및 쿠키 비활성화 방법에 대해 자세히 알아보십시오. 쿠키와 추적 기술은 마케팅 목적으로 사용될 수 있습니다. '동의'를 클릭하면 기기에 쿠키를 배치하고 추적 기술을 사용하는 데 동의하는 것입니다. 쿠키 및 추적 기술을 해제하는 방법에 대한 자세한 내용과 지침을 알아보려면 아래의 '자세히 알아보기'를 클릭하십시오. 쿠키 및 추적 기술 수락은 사용자의 자발적 선택이지만, 웹사이트가 제대로 작동하지 않을 수 있으며 사용자와 관련이 적은 광고가 표시될 수 있습니다. Arrow는 사용자의 개인정보를 존중합니다. 여기에서 당사의 개인정보 보호정책을 읽을 수 있습니다.