#include <mega16.h>
#define ISC01 1
#define ISC11 3
#define INT1 7
#define INT0 6
#define COM1A1 7
#define COM1B1 5
#define WGM13 4
#define WGM12 3
#define CS10 0
void main(void){
DDRB=0xFF; //PORTB la output PORT
DDRD=0x30; //Cac chan OC1B va OC1A la output PIN
PORTD=0xff; //Dung dien tro keo noi cho cac chan
MCUCR|=(1<<ISC11)|(1<<ISC01); //ngat canh xuong
// MCUCR=0x0a;
GICR |=((1<<INT1)|(1<<INT0)); //cho phep 2 ngat hoat dong
TCCR1A=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
TCCR1B=(1<<WGM13)|(1<<WGM12)|(1<<CS10);
OCR1AH=11;
OCR1AL=181; //Duty cycle servo1=1000us=1.5ms (0 degree)
OCR1BH=7;
OCR1BL=104; //Duty cycle servo2=1500us=1 ms (-90 degree)
ICR1H=156; //Time period = 20000us=20ms
ICR1L=32;
#asm ("sei") //set bit I cho phep ngat toan cuc
while (1){ //vong lap vo han
//khong lam gi
}
}
//trinh phuc vu ngat 0
interrupt [EXT_INT0] void external_int0 (void)
{
if ((OCR1AH==7)&&(OCR1AL==104)) {
OCR1AH=11;
OCR1AL=181;
} //thay doi goc xoay servo1 den 90 do
else {
OCR1AH=7;
OCR1AL=104;
} // thay doi goc xoay servo1 den 0 do
}
//trinh phuc vu ngat 1
interrupt [EXT_INT1] void external_int1 (void)
{
if ((OCR1BH==7)&&(OCR1BL==104)){
OCR1BH=11;
OCR1BL=181;
} //thay doi goc xoay servo1 den 90 do
else{
OCR1BH=7;
OCR1BL=104;
} // thay doi goc xoay servo1 den 0 do
}