Advertisement
olelek

mega8servotest.c

Aug 19th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. /*
  2. * mega8servotest.c
  3. *
  4. * Created: 2013-05-10 19:04:22
  5. *  Author: Olek
  6. */
  7.  
  8.  
  9. #include <avr/io.h>
  10. #include <util/delay.h>
  11.  
  12. int main(void)
  13. {
  14.     DDRD=0x00;
  15.  
  16.     PORTD=0x02;
  17.     //Set PORTB1 pin as output
  18.  
  19.     DDRB=0xFF;
  20.  
  21.     //TOP=ICR1;
  22.  
  23.     //Output compare OC1A 8 bit non inverted PWM
  24.  
  25.     //Clear OC1A on Compare Match, set OC1A at TOP
  26.  
  27.     //Fast PWM
  28.  
  29.     //ICR1=20000 defines 50Hz PWM
  30.  
  31.     ICR1=20000;
  32.  
  33.     TCCR1A|=(0<<COM1A0)|(1<<COM1A1)|(0<<COM1B0)|(0<<COM1B1)|
  34.  
  35.     (0<<FOC1A)|(0<<FOC1B)|(1<<WGM11)|(0<<WGM10);
  36.  
  37.     TCCR1B|=(0<<ICNC1)|(0<<ICES1)|(1<<WGM13)|(1<<WGM12)|
  38.  
  39.     (0<<CS12)|(1<<CS11)|(0<<CS10);
  40.  
  41.     //start timer with prescaler 8
  42.     OCR1A = 1500;
  43.     while(1)
  44.     {
  45.  
  46.  
  47.             if(bit_is_clear(PIND, 4)){
  48.  
  49.                 //Lewo
  50.  
  51.                 //OCR1A=515;
  52.  
  53.             }
  54.  
  55.             if(bit_is_clear(PIND, 5)) {
  56.  
  57.                 //środek
  58.  
  59.                 //OCR1A=1560;
  60.  
  61.             }
  62.             if(bit_is_clear(PIND, 6)) {
  63.  
  64.                 //Prawo
  65.  
  66.                 //OCR1A=2560;
  67.  
  68.             }
  69.             if(bit_is_clear(PIND, 7)) {
  70.  
  71.                 //wyłącz wyjśća PWM, odlącz serwo
  72.                 //TCCR1A ^= (1<<COM1A0);
  73.                 //TCCR1A ^= (1<<COM1A1);
  74.                 //PORTB ^= _BV(1);
  75.  
  76.             }
  77.             OCR1A=515;
  78.             _delay_ms(500);
  79.             OCR1A=1560;
  80.             _delay_ms(500);
  81.             OCR1A=2560;
  82.             _delay_ms(500);
  83.             OCR1A=1560;
  84.             _delay_ms(1000);
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement