//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;
}
}