bladabuska

servo.c

Sep 3rd, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. /* -----------------------------------------------------------------------------
  2.  * File:            servo.c
  3.  * Project:         Servo Motor Example
  4.  * Author:          Leandro Schwarz
  5.  * Last modified:   28/08/2014
  6.  * -------------------------------------------------------------------------- */
  7.  
  8. // System definitions
  9. #define F_CPU   16000000UL
  10. #define MCU_8_BITS
  11.  
  12. // Header files
  13. #include "LS_defines.h"
  14. #include "LS_ATmega328.h"
  15.  
  16. // Constant definitions
  17. #define INC 10
  18. #define LEFT    0
  19. #define RIGHT   1
  20.  
  21. // Function declaration
  22.  
  23. // Global variables
  24.  
  25. // main function
  26. int main()
  27. {
  28.     uint16 value = 1000;
  29.     uint8 direction = LEFT;
  30.  
  31.     // Port configuration
  32.     setBit(DDRB, PB1);
  33.  
  34.     // Timer1 configuration
  35.     timer1ClockPrescaller8();
  36.     timer1PhaseFrequencyCorrectPWMICR1Mode();
  37.     timer1OC1ANonInvertedMode();
  38.     timer1SetInputCaptureValue(20000);
  39.  
  40.     while(1){
  41.         if(direction == LEFT){
  42.             value += INC;
  43.             if(value >= 2000){
  44.                 value = 2000;
  45.                 direction = RIGHT;
  46.             }
  47.         }else{
  48.             value -= INC;
  49.             if(value <= 1000){
  50.                 value = 1000;
  51.                 direction = LEFT;
  52.             }
  53.         }
  54.         timer1SetCompareAValue(value);
  55.         _delay_ms(30);
  56.     }
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment