在MPLAB中用MPLAB进行伺服电机编码 [英] Servo motor coding on MPLAB in C

查看:119
本文介绍了在MPLAB中用MPLAB进行伺服电机编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助!



我正在尝试使用PWM编码伺服电机SM-S4315M是伺服电机的名称。我根据输入使用电机将车轮定位到几个不同的位置但是我似乎无法将电机放到任何位置!



这是我到目前为止的代码,但它似乎没有工作:



  #include   <   16F648A  < span class =code-keyword>>  

#fuses NOWDT,NOPROTECT,BROWNOUT,NOPUT,NOLVP,INTRC,NOCPD,NOMCLR

#use delay(clock = 4000000

void main(< span class =code-keyword> void ){
output_low(PIN_B3); // 将CCP1输出设为低
setup_ccp1(CCP_PWM); // 将CCP1配置为PWM
setup_timer_2(T2_DIV_BY_4, 149 1 ); // 1666 kHz
set_pwm1_duty( 37 < /跨度>); // 引脚B3上的占空比为25%(37/149)= 25%
delay_ms ( 3000 );
}



如果您对伺服电机编码有任何经验,请使用PIC 16F648A以及我使用的应用程序伺服电机用于控制机器人的转向,跟随一条线。

解决方案

来自Microchip数据表:

< blockquote class =FQ>

Quote:

由于CCP1引脚与PORTB数据锁存器复用,因此TRISB< 3>必须清零该位以使CCP1引脚成为输出。

因此必须先调用 set_tris_b(0xF7)将端口B3配置为输出。然后可以使用 output_low()函数将引脚上的电平设置为低电平。



参见第9.3.3节。在数据表中有关配置PWM操作的步骤。



在将任何硬件连接到控制器之前,使用示波器测试输出可能很有用。

Help!

I am trying to code a servo motor using PWM SM-S4315M is the name of the servo motor. I am using the motor to position a wheel into several different places depending on an input however I can''t seem to get the motor into any position!

Here is the code that I have so far but it doesn''t seem to be working:

#include <16F648A>

#fuses NOWDT, NOPROTECT, BROWNOUT, NOPUT, NOLVP,INTRC,NOCPD,NOMCLR

#use delay(clock = 4000000)

void main(void) {
    output_low(PIN_B3);   // Set CCP1 output low
    setup_ccp1(CCP_PWM);  // Configure CCP1 as a PWM
    setup_timer_2(T2_DIV_BY_4, 149, 1);  // 1666 kHz
    set_pwm1_duty(37);                  // 25% duty cycle on pin B3 (37/149)=25%
    delay_ms(3000);
}


Please lend a hand if you have any experience with coding servo motors, by the way I am using a PIC 16F648A and the application that I am using the servo motor for is to control the steering of a robot which is to follow a line.

解决方案

From the Microchip datasheet:

Quote:

Since the CCP1 pin is multiplexed with the PORTB data latch, the TRISB<3> bit must be cleared to make the CCP1 pin an output.

So you must call set_tris_b(0xF7) first to configure port B3 as output. The output_low() function can be then used to set the level on the pin to low.

See also section 9.3.3. in the datasheet about the steps to configure PWM operation.

Before connecting any hardware to your controller, it may be useful to test the output using an oscilloscope.


这篇关于在MPLAB中用MPLAB进行伺服电机编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆