Key controlled step motor running demo1. Introduction of this demoThis demo is to show a simple example of how to use Embedded Pi to control a step motor byArduino motor shield. The motor is controlled by two keys. One key controls speed and anothercontrols the direction. You can learn how to use CoIDE and Embedded Pi to build your project.2. About Hardware2.1 Necessary hardwaresTo complete this demo, you need following hardwares:-1. An Embedded Pi board-2. An Arduino motor shield and a 42BYGH serial step motor.
-3. 9V2A power CoLinkEx(optional) an LED,two buttons-4. Some wires, USB cable, a pointer2.2 Hardware connectionFirstly, connect the step motor to Arduino motor shield, shown as below:You should pay attention to the connection of the four wires. They should be right connected or themotor will not work properly.Then connect the LED and two buttons to the motor shield. Shown as below:
The two button connect to the IN2 and IN3 port, and the LED connects to the OUT~5 port.Theposition of the three ports are shown below.
Then plug the motor shield into Embedded Pi board. Connect the USB cable to supply power forEmbedded Pi and connect the external 9V power to supply power for motor shield and motor.If you have CoLinkEx debugging adapter or other debugging tools, you can connect it to thedebugging port on Embedded Pi. Or if you have no debugging tools, don’t worry, you can downloadprogram to Embedded Pi through UART1 port.Make sure that the three jumpers on the Embedded Pi are connected rightly. JP1 should beconnect to 5V side (3.3V side may also be ok in this demo) to enable all the input and output port.. JP3should remain disconnected to disable output of Raspberry Pi port. JP4 should be connected to enableSTM32 input and output. For more details, please refer to Embedded Pi Schematic Diagram. Theconnections are shown as below.
3. About software3.1 Software requirements and installingThis demo needs CoIDE integrated development environment and GCC tool chain. Thesesoftwares can be freely downloaded from CooCox’s site http://www.coocox.org/ . After installed thesesoftwares, open CoIDE to configure the tool chain path:Browse the explorer to the install directory of GCC, select “bin” directory and press OK. As soon asyou have configured this, you can build your project.If the debugging tools are used, you should install their drivers first. For example, if you use anCoLinkEx as the debugger, you should install its driver. The CoLink/CoLinkEx driver is also availableto download freely from CooCox’s site.If you have already installed these softwares, you can jump over this step.3.2 Start project3.2.1 Create a projectThere are two entrances of creating a new project. The one is click “project” menu on the menubar and select “New Project” to create a new project. The other is directly clicking “Create NewProject” on the welcome page shown as below:
1. In the pop up window type the project name and select project path.If you don’t want to use the default path, deselect the right tick and browse to your specified directory.
2. Select the mode of creating a new project.Here are two modes, the one is chip mode which means create a new project based on your targetchip; the other is board mode which means create a new project based on your target board. Here weselect chip mode then press next.3. Select chipEmbedded Pi use a STM32F103RBT6 as its controller. So we find STM32F103RB in the chiplist. The right side shows the description of the chip. Or you may feel it’s troublesome to find thechip, you can directly type in the key word of the chip name and the filter will list theresult, this is convenient.
Then click “Finish” to complete the creating.3.2.2 Add components and exampleThe source codes we used in this demo are all included in CoIDE’s example source codes. Useronly need to add them to the project by check them. CoIDE makes MCU’s development more easily.While you should pay attention that its strongly recommended to update all the components inyou repository through internet before you use this demo. Since the components are update frequently,some bugs are fixed and some new components are added, it may cause confliction if new demoprogram using old components.
Click hereto updateAfter the project was found, you can select the CoX peripheral library or manufacturerlibrary(like stm32 manufacturer library) as you like but don’t use both of then in one project. CooCoxprovide a simple and easy to use CoX peripheral library, this library has a universal interface whichsupport many kinds of Cortex M serial MCUs. User developed programs based on CoX peripherallibrary can be transplant to other Cortex M serial MCUs easily with little modify.CoX also provided many module and devices drivers like LCDs, sensors, arduino modules and soon. This can be freely used in your project and can simplify your developing. Many of them are alsobased on CoX library. You can find the drivers in the “Drivers” tab in repository page.Now we add Arduino motor shield driver to the project. We switch to the “Drivers” tab page,select the “Shield” category, find and select “Shield arduino motor shield”, then the driver will beadded to the project. CoIDE can automatically add the related library but maybe not complete and youneed to add by yourself when you build the project and it warns you file not found.
Like this driver, the Arduino motor shield driver will use ADC and timer, but they are not addedto our project, so we add then manually. Except those two files, we also need theEmbedded_PI.Config files which define the pins of Embedded Pi, so we add it:
We can see from the Components window that motor shield driver has been added to the projectand it has an example. We click the example, in the new pop out window we click “add” to add theexample to our project. When it notice you, just press Yes. Now the project is found.3.2.3 Building, downloading and debuggingWe can directly click Build button or press F7 to build the project. There maybe some warningsoutput during building, that does not matter. At last the project build successfully and executable ordebugging files are generated.
configuration is needed. You should select the file path of JLinkGDBServerCL.exe (normally in theinstall directory of JLink driver).If you don’t have a debugger, you can also download program to the chip through serialcommunication port of the computer. For this purpose you need a specified software like Flash LoaderDemonstrator (you can get it and learn how to use it from http://www.st.com). You can follow thesesteps: first connect the power to Embedded Pi and connect UART port (the TX1, RX1 on EmbeddedPi ) to the UART of the computer, notice the TX line and RX line should be crossed connected. Thenpress and hold the BOOT0 when connect or download program press the RESET button. You can trymore times if connection fails.
3.3 Example analysisAfter the program enter main() function, the program will directly enter into StepMotorExample()function and will not return. In this function, the following procedure will be executed sequently:system clock initialization, alternative-function I/O clock enable, LED and Keys GPIO clock enable,direction configuration, motor I/O initialization. Then let the LED blinks for 5 times after thatprogram enter a while(1) loop. In the loop, step motor’s running direction will be set according to thetimes the key2 pressed, and run 20 steps according to the direction. The LED will trun on or offaccording to the direction. The key input use interrupt input mode. If key1 pressed the direction willchange. If key2 pressed the speed will changed.It should be payed attention that only after the motor run 20 steps does the program execute thedirection change and speed change. So if the speed of motor is too slow, the response to the key willcause delay. This problem can be solved by using a timer interrupt to control the output to motor. Inthis demo, we only give out a simple solution of step motor controlling, while in the following demos ,we will use interrupt method and this controls perfectly.4. Phenomenon of demoAfter power on the board or reset MCU, the red LED will blink 5 times, then the step motor startto run with its highest speed. Then every time key1 pressed, the motor will change its direction, andthe LED will turn on or off synchronously with the direction. Every time key2 pressed the motor’sspeed will slow down. After pressed key2 for 10 times, the speed return to max speed and cycles. Hereis a picture shows the result.
5. Following introductionYou can see the video of this demo on http://coocox.org/epi.html. There are also otherdemos, documents, videos on the site. You can post your experiences during developing an applicationon CooCox forum http://www.coocox.org/Forum/index.php to share with others. Or you can give outyour questions, the technical supporters and the net friends will help you actively. Also, you can sendmail to CooCox firstname.lastname@example.org to give your advice or look for help.