你可以自己写一个的。这种舵机比较特殊,但是只要满足一下PWM就可以实现了:
1. 周期T=20ms(即50HZ),占空比500us-2500us。
2. 500us舵机逆时针转90度,2500us舵机顺时针转90读。 (其中0度是1500us)
3. 即: TL+TH=20ms 改变TH的值,舵机角度随之改变。参数参照以上1.2点。
TL TH
_____ ____
_________| |_________|| _________|
4. 电路原理很简单,接好电源,地,信号线。
信号线接到单片机或者其他能输出PWM的控制脚。
试试这个吧,我用过的#include<hidef.h>
//#include <math.h>
// #include <mc9s12xs128.h>
#include "derivative.h"
//#pragma LINK_INFO DERIVATIVE"mc9s12dg128b"
/*****定义变量******/
unsigned int a,c,d
unsigned int AD_wValue,B //AD转换结果
int b,i,x,e,j
void LEDShow(int x)
/*****总线初始化*****/
void PLL_Init(void) {
REFDV=1
SYNR=2
while(!(CRGFLG&0x08))
CLKSEL=0x08
}
/******AD初始化******/
/*void AD_Init() {
//ATD0CTL1=0x00
ATD0CTL2=0xC0//启动AD,快速清零,无等待模式,禁止外部触发,中断禁止
ATD0CTL3=0x40//转换序列长度8
ATD0CTL4=0x85
ATD0CTL5=0xA0//右对齐无符号,单通道0采样
ATD0DIEN=0x00//禁止数字量输入 */
/**读取AD转换结果**/
/*void AD_GetValue()
{
while(!ATD0STAT0_SCF) //等待转换结束
AD_wValue=ATD0DR0L//读取结果寄存器的值
} */
/*****pwm初始化********/
void PWM_Init(void)
{
PWME_PWME1=0x00 // Disable PWM (PP1) 禁止
PWMCTL_CON01=1 //0,1级联,寄存器为通道1的
PWMCAE_CAE1=0//通道7左对齐
PWMCNT01=0 // 计时器清零
PWMPOL_PPOL1=1 // 先置高电平,DYT反
PWMPRCLK=0 //COLCKA不分频
PWMSCLA=4 //COLCK SA进行16分频
PWMDTY01=1500// 舵机静止占空比为7.5%=1500/20000
PWMPER01=20000 //周期20MS
PWMCLK_PCLK1=1// CLOCK SA 作时钟源
PWME_PWME1=1
}
/*舵机控制*/
void PWMDuo_Dutycycle(int d)
{
PWMDTY01=d
PWME_PWME1=1// Enable PWM 使能
}
void delay(long m){
while(m--)
}
/**************显示输出*******************/
const int Dtable[10] =
{0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}
const int CStable[4] =
{0xF7,0xFB,0xFD,0xFE}
void LEDShow1(int i,int c)
{
PTT&= CStable[i]
PORTB = Dtable[c]
delay(100)
PTT|=0xff
}
void LEDShow(int x){
DDRB=0xFF
DDRT=0xFF
b = x/10 //x=126 b=12
e=x-b*10// e=6
x=b // x=12
for(i=0i<=3i++) {
LEDShow1(i,e)
b = x/10//b=1 //b=0 b=0
e=x-b*10 // e=2 e=1 e=0
x=b // x=1 x=0 x=0
}
}
/******主函数*********/
void main(void) {
/*put your own code here*/
//EnableInterrupts
DisableInterrupts
PLL_Init()
ATD0CTL1=0x00
ATD0CTL2=0xC0//启动AD,快速清零,无等待模式,禁止外部触发,中断禁止
ATD0CTL3=0x80//转换序列长度8
ATD0CTL4=0x83
ATD0CTL5=0xA0//右对齐无符号,单通道0采样
ATD0DIEN=0x00//禁止数字量输入
PWM_Init()
for()
{
while(!ATD0STAT0_SCF) //等待转换结束
AD_wValue=ATD0DR0//读取结果寄存器的值
B=AD_wValue
a=1150+(1000/250)*B
if ( a>2000 )
{
a=2000
}
else if(a<1000)
{
a=1000
}
else{
a=a
}
PWMDuo_Dutycycle(a)
LEDShow(a)
// _FEED_COP()/* feeds the dog */
}/* loop forever */
/* please make sure that you never leave main */
}
#include<REG52.H>#define OUT P0
#define CON P2
#define ms0_5Con 461
#define ms2_5Con 2304
typedef unsigned char unit8
typedef unsigned short unit_16
sbit servo0=OUT^0
sbit servo1=OUT^1
sbit servo2=OUT^2
sbit servo3=OUT^3
sbit servo4=OUT^4
sbit servo5=OUT^5
sbit servo6=OUT^6
sbit servo7=OUT^7
sbit KEY1=CON^0
sbit KEY2=CON^1
unit_16 pwm[8]={1382,1382,1382,1382,1382,1382,1382,1382}//??90?,(???1382.4,???1382)
void Inter_Form()
{
TMOD|=0x01
TH0 =0xf7
TL0 =0x00
TR0 =1
ET0 =1
EA =1
}
void delay(unit8 x)
{
unit8 i=0
while(x--)
{
for(i=0i<125i++)
}
}
/*------------------------?????--------------------------*/
/*unit_16 Transform(uchar val)
{
//0?=0.5ms, 45?=1ms, 90?=1.5ms, 135?=2ms, 180?=2.5ms
//2.5 ms??? F700, (12n/11059200=2.5/1000, n=2304, X=65536-2304=63232 >F700)
//return (unit_16)(((float)(2/180)*X+0.5)/1000*11059200/12)
unit_16 a = (val+46)*10
if(a<ms0_5Con)
a=ms0_5Con
if(a>ms2_5Con)
a=ms2_5Con
return a
} */
/*-------------------------???---------------------------*/
void Steering_UP(unit8 val)
{
if(pwm[val]>ms2_5Con)
pwm[val]=ms2_5Con
pwm[val]=pwm[val]+10
}
void Steering_Down(unit8 val)
{
if(pwm[val]<ms0_5Con)
pwm[val]=ms0_5Con
pwm[val]=pwm[val]-10
}
void main(void)
{
Inter_Form()
while(1)
{
if(!KEY1)
{
delay(2)
if(!KEY1)
Steering_UP(0)
}
else if(!KEY2)
{
delay(2)
if(!KEY2)
Steering_Down(0)
}
}
}
/*------------------------??????--------------------------*/
void SteeringGear() interrupt 1
{
static unit8 pwm_flag=0
switch(pwm_flag)
{
case 1: servo0=1TH0=-pwm[0]>>8TL0=-pwm[0]break
case 2: servo0=0TH0=-(ms2_5Con-pwm[0])>>8TL0=-(ms2_5Con-pwm[0])break
case 3: servo1=1TH0=-pwm[1]>>8TL0=-pwm[1]break
case 4: servo1=0TH0=-(ms2_5Con-pwm[1])>>8TL0=-(ms2_5Con-pwm[1])break
case 5: servo2=1TH0=-pwm[2]>>8TL0=-pwm[2]break
case 6: servo2=0TH0=-(ms2_5Con-pwm[2])>>8TL0=-(ms2_5Con-pwm[2])break
case 7: servo3=1TH0=-pwm[3]>>8TL0=-pwm[3]break
case 8: servo3=0TH0=-(ms2_5Con-pwm[3])>>8TL0=-(ms2_5Con-pwm[3])break
case 9: servo4=1TH0=-pwm[4]>>8TL0=-pwm[4]break
case 10: servo4=0TH0=-(ms2_5Con-pwm[4])>>8TL0=-(ms2_5Con-pwm[4])break
case 11: servo5=1TH0=-pwm[5]>>8TL0=-pwm[5]break
case 12: servo5=0TH0=-(ms2_5Con-pwm[5])>>8TL0=-(ms2_5Con-pwm[5])break
case 13: servo6=1TH0=-pwm[6]>>8TL0=-pwm[6]break
case 14: servo6=0TH0=-(ms2_5Con-pwm[6])>>8TL0=-(ms2_5Con-pwm[6])break
case 15: servo7=1TH0=-pwm[7]>>8TL0=-pwm[7]break
case 16: servo7=0TH0=-(ms2_5Con-pwm[7])>>8TL0=-(ms2_5Con-pwm[7])break
default:TH0=0xffTL0=0x80pwm_flag=0
}
pwm_flag++
}
/*---------------------------------------------------------------------------------
如有问题可再咨询
-----------------------------------------------------------------------------------*/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)