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

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

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

#define  SERVO(m)  m?    (PORTF=0xff):(PORTF=0x00)

volatile unsigned int count=0;
volatile unsigned int POS = 600; // POS :   600  -   2400

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:1
// WGM: Normal
// desired value: 1MHz
// actual value:  1.000MHz (0.0%)
void timer0_init(void)
{
 TCCR0 = 0x00; //stop
 ASSR  = 0x00; //set async mode
 TCNT0 = 0xF0; //set count
 OCR0  = 0x10;
 TCCR0 = 0x01; //start timer
}

#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
 TCNT0 = 0xf0; //reload counter value
 count++;
 if(count > 20000){
  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_1=0;
 init_devices();
 //insert your functional code here...
 angle_0_1 = 0;
 while(1){
     POS = 600+angle_0_1; // angle : 0.1도 단위. 
 }
}

 

다음글 atmega2560 ADC예제 (WINAVR(gcc) + AVR Studio)
이전글 [ADC, ATMEGA128] AD컨버터 소스 (ICCAVR V7)