舵机程序
下面的演示程序中,舵机的输出轴在一定的角度范围内做周期性摆动。
舵机型号为Tower Pro Micro Servo 99 SG90,单片机型号为Atmega16。
对照右边的实物图,舵机的引出线依次为: GND(褐色),VCC(红色),CONTROL(橙黄)。VCC,GND为舵机的电源线(+5V),CONTROL(PWM波形)为舵机控制信号的输入线。
。
//ICC-AVR application builder : 2010-11-6 19:14:54
// Target : M16
// Crystal:
#include <>
#include <>
#define uchar unsigned char
#define uint unsigned int
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x10;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER1 initialize - prescale:8
// WGM: 15) PWM fast, TOP=OCRnA
// desired value: 50Hz
// actual value: (%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00; //setup
TCNT1L = 0x00;
OCR1AH = 0x09;
OCR1AL = 0xC3;
OCR1BH = 0x09;
OCR1BL = 0xC3;
TCCR1A = 0x03;
TCCR1B = 0x1A; //start Timer
}
#pragma interrupt_handler pa_isr:7
void pa_isr(void)
{
//compare occured TCNT1=OCR1A
TCNT1H = 0x00;
TCNT1L = 0x00;
PORTB|=(1<<PB4);
}
#pragma interrupt_handler pb_isr:8
void pb_isr(void)
{
//compare occured TCNT1=OCR1B
PORTB&=~(1<<PB4);
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
p
舵机程序 来自淘豆网m.daumloan.com转载请标明出处.