#include포트열고닫기 (Servo Motor, LED, Dc Motor)#include #include #define F_CPU 16000000UL #include #include /* ADC */ unsigned int adc_check(unsigned char value) { ADMUX = value; ADCSRA |= 0x40; while ((ADCSRA & 0x10) == 0); ADCSRA |= 0x10; return ADCW; } void RC_Motor(int angle) { int i; if(angle < -90)angle=-90;// angle : -90도 ~ +90도 if(angle > 90)angle= 90; i=(angle)*18 + 3000; OCR1C=i; //PB7 } void Init_USART1(void) { UBRR1H = 0; UBRR1L = 103; UCSR1A = 0x00; UCSR1B = 0x98; UCSR1C = 0x06; DDRB = 0xFF; } void Init_Servo(void) { TCCR1A=0x0A; // FAST PWM , TOP:ICR1, OCR1C업데이트:TOP TCCR1B=0x1A; // 16MHz -> 8분주 = 0.5usec(클럭1개 소요시간) ICR1=47999; OCR1C=1380; } void TX(unsigned char data) { while(!(UCSR1A & (1 << UDRE1))); UDR1 = data; } unsigned char RX(void) { while(!(UCSR1A & (1 << RXC1))); return UDR1; } int main(void) { int in,out; int t; Init_USART1(); ADCSRA=0x85; Init_Servo(); TIMSK=0x00; sei(); TX('a'); while(1) { _delay_ms(1); in=adc_check(1); _delay_ms(1); out=adc_check(3); if(in > 100 || out > 100) { if(in < 100 && out > 100) TX('A'); else if(in > 100 && out > 100) TX('X'); if(in > 100 && out < 100) TX('B'); else if(in > 100 && out > 100) TX('x'); } if(in < 100 && out < 100) TX('C'); _delay_ms(100); adc_check(7); t = ADCW; _delay_ms(1); /* TX(t / 1000 + '0');//1234/1000 =1.234 + '0' = 1 TX(t % 1000 / 100 + '0');//1234 % 1000 = 234, 234/100 = 2.34 TX(t % 100 / 10 + '0');//1234 % 100 = 34, 34/10 = 3.4 TX(t % 10 +'0');//1234 %10=4 TX('\n'); */ if(t > 350 ) { if(OCR1C == 4620) { RC_Motor(-90); TX('R'); } } /* m = OCR1C; TX(m / 1000 + '0');//1234/1000 =1.234 + '0' = 1 TX(m % 1000 / 100 + '0');//1234 % 1000 = 234, 234/100 = 2.34 TX(m % 100 / 10 + '0');//1234 % 100 = 34, 34/10 = 3.4 TX(m % 10 +'0');//1234 %10=4 TX('\n'); */ } return 0; } ISR(USART1_RX_vect) { unsigned char i; i = RX(); if(i == 'a') //PB1. LED { PORTB ^= 0x02; } else if(i == 'b') //PB5 { PORTB ^= 0x20; } else if(i == 'm') //PB3 { PORTB ^= 0x08; } else if(i == 's') { if(OCR1C==1380) { RC_Motor(0); } else if(OCR1C==3000) { RC_Motor(-90); } } i=0; }
최근댓글