로그인 | 회원가입 | 주문 조회
참고자료 서보모터 제어(1도 단위 제어) ATMEGA128 (ICCAVR V7 사용)
글쓴이 administrator 등록일 2012-02-23 조회 7,753

//ICC-AVR application builder : 2/23/2012 9:51:47 AM
// Target : M128
// Crystal: 16.000Mhz

#include <iom128v.h>
#include <macros.h>

#define  SERVO_DDR        DDRF
#define  SERVO(m)  m?    (PORTF=0x01):(PORTF=0x00)   //PORTF의 D0 핀을 서보 모터로 사용.

volatile int count=0;
volatile int POS = 60; // POS :   60  -   240

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x00;
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
 PORTE = 0x00;
 DDRE  = 0x00;
 PORTF = 0x00;
 DDRF  = 0x80;
 PORTG = 0x00;
 DDRG  = 0x00;
}


//TIMER0 initialize - prescale:32
// WGM: Normal
// desired value: 100KHz
// actual value: 100.000KHz (0.0%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT0 = 0xFB; //set count
 OCR0  = 0x05;
 TCCR0 = 0x03; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
 TCNT0 = 0xfb; //reload counter value
 count++;
 if(count > 2000){
  count = 0;
 }
 
 if(count > POS){  
     SERVO(1);
 }else{
     SERVO(0);
 }
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 XDIV  = 0x00; //xtal divider
 XMCRA = 0x00; //external memory
 port_init();
 timer0_init();

 MCUCR = 0x00;
 EICRA = 0x00; //extended ext ints
 EICRB = 0x00; //extended ext ints
 EIMSK = 0x00;
 TIMSK = 0x01; //timer interrupt sources
 ETIMSK = 0x00; //extended timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

//
void main(void)
{
 int angle=0;
 init_devices();
 //insert your functional code here...
 SERVO_DDR = 0xff;

 while(1){
     angle = 0;
     POS = 60+angle; 
 }
}

 

다음글 RC 서보모터 제어(0.1도 단위 제어) ATMEGA128 (ICCAVR V7 사용)
이전글 WINAVR gcc 컴파일러 다운로드 링크입니다.