在线时间27 小时
UID3138077
注册时间2015-6-17
NXP金币0
TA的每日心情 | 开心 2018-11-4 09:17 |
---|
签到天数: 2 天 连续签到: 1 天 [LV.1]初来乍到
注册会员

- 积分
- 156
- 最后登录
- 2019-7-5
|
本帖最后由 yitai121 于 2015-11-27 15:09 编辑
1、开发环境
开发环境使用的是mbed离线开发包,Smeshstudio。这个是一个基于java开发环境eclipse的编译器也就是Mbed的离线版,同时SMeshStudio还支持arduino,contiki的开发,是一个多平台的编译器smeshlink官网地址http://mbed.smeshlink.com/。
2、支持的开发板
Mbed官网给出的支持的开发板并没有KL02z,但支持FRDM-KL05z,我对比了两块芯片,不同之处在于KL02z比KL05z少了DAC接口和Touch System Interface,其他引脚定义基本一样,因此本次就基于FRDM—kl05z建立了工程。
支持的开发板
3、PWM输出控制伺服舵机
今天用到的是Kl02Z,有四个PWM输出的通道,分别是TPM0_CH0、TPM0_CH1、TPM1_CH0、TPM1_CH1,对应的引脚分别是PTA6、PTA5、PTA0、PTB13。例程给出的是使用TPM0_CH0输出PWM驱动舵机在全角度扫描转动。
pwm通道
kl02z PWM通道
4、用到的舵机驱动专用库函数SERVO.h,这是smeshlink自带的,直接添加就好了。代码贴出来:
- /* mbed R/C Servo Library
- *
- * Copyright (c) 2007-2010 sford, cstyles
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
- #include "Servo.h"
- #include "mbed.h"
- static float clamp(float value, float min, float max) {
- if(value < min) {
- return min;
- } else if(value > max) {
- return max;
- } else {
- return value;
- }
- }
- Servo::Servo(PinName pin) : _pwm(pin) {
- calibrate();
- write(0.5);
- }
- void Servo::write(float percent) {
- float offset = _range * 2.0 * (percent - 0.5);
- _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
- _p = clamp(percent, 0.0, 1.0);
- }
- void Servo::position(float degrees) {
- float offset = _range * (degrees / _degrees);
- _pwm.pulsewidth(0.0015 + clamp(offset, -_range, _range));
- }
- void Servo::calibrate(float range, float degrees) {
- _range = range;
- _degrees = degrees;
- }
- float Servo::read() {
- return _p;
- }
- Servo& Servo::operator= (float percent) {
- write(percent);
- return *this;
- }
- Servo& Servo::operator= (Servo& rhs) {
- write(rhs.read());
- return *this;
- }
- Servo::operator float() {
- return read();
- }
复制代码 6、最终代码如下:
- // Do not remove the include below
- #include "kl02z_servo.h"
- // Continuously sweep the servo through it's full range
- #include "mbed.h"
- #include "Servo.h"
- Servo myservo(PTA6);
- int main()
- {
- myservo.calibrate(0.002,180);
- while(1)
- {
- for(int i=-90; i<90; i++)
- {
- myservo.position(i);
- wait(0.01);
- }
- wait(2);
- for(int i=-90; i>0; i--)
- {
- myservo.position(i);
- wait(0.01);
- }
- wait(2);
- }
- }
复制代码
|
评分
-
查看全部评分
|