Advertisement
kklevi

Untitled

Apr 21st, 2023 (edited)
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. // C++ code
  2. //
  3. /*
  4.   Sweep
  5.  
  6.   by BARRAGAN <http://barraganstudio.com>
  7.   This example code is in the public domain.
  8.  
  9.   modified 8 Nov 2013  by Scott Fitzgerald
  10.   http://www.arduino.cc/en/Tutorial/Sweep
  11. */
  12.  
  13. #include <Servo.h>
  14. uint8_t counter = 0;
  15. uint16_t counter16 = 0;
  16. uint8_t imp = 10;
  17.  
  18. int pos = 0;
  19.  
  20. Servo servo_9;
  21.  
  22. void setup()
  23. {
  24.   //
  25.   DDRB |= (1 << PB1);
  26.   tim2_ctc_oc_init();
  27. }
  28.  
  29. void loop()
  30. {
  31.  
  32.   //TCCR2A = (1<<WGM21) | (0<<WGM20);
  33.  
  34.  
  35.   //TCCR2B = (0<<WGM22) | (1<<CS22) | (0<<);
  36.  
  37. }
  38.  
  39. ISR(TIMER2_COMPA_vect)
  40. {
  41.   if(counter==199){
  42.    
  43.     //20ms period 0,1 - 200
  44.     //+90, 0, -90
  45.     //2 1,5     1   20  -    15    -   10
  46.     counter=0;
  47.     PORTB |= (1<<PB1);
  48.   }
  49.   else counter++;
  50.   if(counter16==9999){
  51.     counter16=0;
  52.     if(imp==20){
  53.       imp=10;
  54.     } else {
  55.       imp++;
  56.     }
  57.   } else {
  58.     counter16++;
  59.   }
  60.   if(counter==imp){
  61.     PORTB &= ~(1<<PB1);
  62.   }
  63.                                      
  64.                                      
  65. }
  66.  
  67. void tim2_ctc_oc_init(){
  68.      TCCR2A = (1<<WGM21) | (0<<WGM20);
  69.  
  70.  
  71.     TCCR2B = (0<<WGM22) | (1<<CS22) | (0<<CS21) | (0<<CS20);
  72.   OCR2A = 24;
  73.   TIMSK2 = (1<<OCIE2A);
  74.   sei();
  75.  
  76. }
  77.                                      
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement