舵机驱动程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
舵机驱动程序
所用的舵机型号为Tower Pro Micro Servo 99 SG90,单片机型号为Atmega16。
舵机有三根引出线:VCC,GND,CONTROL。
VCC,GND为舵机的电源线,CONTROL为舵机控制信号的输入线。
控制信号为50Hz的PWM波形(矩形波),不同的脉宽(占空比)对应舵机不同的方位角。
改变脉宽即可改变舵机的方位角。
在没有过载且控制信号持续稳定的条件下,舵机的方位角严格保持不变.此控制信号适合所有类型的舵机.需要注意的是尽管也可以用延时函数来实现PWM波,但舵机运行可能不平稳,而且若PWM波的频率偏离50Hz过大,舵机会出现震颤甚至会无法启动舵机!
下面给出了一特定脉宽的舵机控制信号,PB7为控制信号的输出端口。
程序中采用T/C0定时器中断的方式来产生PWM波形,信号更准确.需要说明的是,因程序脉宽调整得原理,脉宽的可能取值是离散的,只能得到一些特定位置,但对于普通的执行机构,是能够满足要求的。
//ICC—AVR application builder : 2010—9-21 10:22:14
// Target : M16
// Crystal: 8。
0000Mhz
#include 〈iom16v。
h>
#include 〈macros.h>
//servo—drive
unsigned int count_T=0;
unsigned int angle=3;// set angle
//note: change the value of angle(0~5),then you can get different angle //position of the servo
//TIMER0 initialisation - prescale:64
// WGM: Normal
// desired value: 0.5mSec
// actual value: 0.499mSec (0.2%)
void timer0_init(void)
{
TCCR0 = 0x00;//stop
TCNT0 = 0xC2; //set count
OCR0 = 0x00; //set compare
TCCR0 = 0x03; //start timer
}
#pragma interrupt_handler timer0_comp_isr:20
void timer0_comp_isr(void)
{
//compare occured TCNT0=OCR0
if(count_T<angle) PORTB|=(1〈<PB7);//PB7 as output port to control servo else PORTB&=~(1〈〈PB7);
count_T++;
count_T%=40;
}
//call this routine to initialize all peripherals void init_devices0(void)
{
//stop errant interrupts until set up
CLI();//disable all interrupts
timer0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x02; //timer interrupt sources
SEI(); //re—enable interrupts
//all peripherals are now initialized
}
//port initial
void port_init( )
{
DDRA = 0x00;
DDRB = 0x10; //PB7 as output port to control servo DDRC = 0x00;
DDRD = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
}
void main()
{
port_init();
init_devices0( );
while(1);
}。