|
发表于 2009-11-25 14:21:10
|
显示全部楼层
//ICC-AVR application builder : 2009-11-19 下午 12:01:03
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>
static unsigned char count=0,servo=0;
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:8
// WGM: Normal
// desired value: 10KHz
// actual value: 10.000KHz (0.0%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x9C; //set count
OCR0 = 0x64; //set compare
TCCR0 = 0x02; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x9C; //reload counter value
count++;
if(count<=servo)
{
PORTA|=(1<<0);
}
else
{
PORTA&=~(1<<0);
}
if(count>=200)
{
count=0;
}
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
//insert your functional code here...
while(1)
{
servo=10;
}
}
这是一个舵机的,六个舵机在溢出中断力再加5个就行了,不过要注意时间够不够用?如果还不行,可以再说另一种方法,你先试试。 |
|