Advertisement
Utshaw

Servo-lock-unlock-AtMega32

Jul 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. #ifndef F_CPU
  2. #define F_CPU 8000000UL // 1 MHz clock speed
  3. #endif
  4.  
  5.  
  6. #include <avr/io.h>
  7. #include <util/delay.h>
  8.  
  9. unsigned char TCCR1AVal ;
  10. unsigned char TCCR1BVal ;
  11. unsigned char ICR1Val;
  12.  
  13.  
  14. void moveClockWise(){
  15. OCR1A = ICR1 - 1400; // CLockwise control
  16. }
  17.  
  18.  
  19. void moveAntiClockWise(){
  20. OCR1A = ICR1 - 4400; // Anti-clockwise control
  21. }
  22.  
  23. void _resetServo(){
  24. TCCR1A = TCCR1AVal;
  25. TCCR1B = TCCR1BVal;
  26. ICR1 = ICR1Val;
  27. }
  28.  
  29. void _setServo(){
  30. TCCR1A |= 1 << WGM11 | 1 << COM1A1 | 1 << COM1A0;
  31. TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS10;
  32. ICR1 = 19999;
  33. }
  34.  
  35.  
  36. void debugClockWise(){
  37. _setServo();
  38. moveClockWise();
  39. _delay_ms(250);
  40. _resetServo();
  41. }
  42.  
  43.  
  44. void debugAntiClockWise(){
  45. _setServo();
  46. moveAntiClockWise();
  47. _delay_ms(250);
  48. _resetServo();
  49. }
  50.  
  51. int main(void)
  52. {
  53.  
  54.  
  55.  
  56.  
  57.  
  58. DDRA &= ~(1<<PA0); // Configure PA0 as input
  59. DDRA |= (1<<PA1); // Configure PA1 as output
  60. DDRA &= ~(1<<PA2); // Configure PA2 as input
  61. DDRA |= (1<<PA3); // Configure PA3 as output
  62.  
  63.  
  64. DDRD = 0XFF;
  65.  
  66. TCCR1AVal = TCCR1A;
  67. TCCR1BVal = TCCR1B;
  68. ICR1Val = ICR1;
  69.  
  70.  
  71. int flag = 1; // flag-0 : "Searching-For-Lock-Signal" State ; flag-1 : "Searching-For-Unlock-Signal" state
  72.  
  73. while(1){
  74. if((PINA & (1<<PA0)) && !flag){ // "Searching-For-Lock-Signal" State ; Check if PA0 is giving HIGH
  75. PORTA |= (1<<PA1); // Give HIGH to PA1
  76. debugClockWise(); // Locked done
  77.  
  78. _delay_ms(400);
  79. flag = 1; // Now check for upcoming unlock signal
  80. PORTA &= ~(1<<PA1);
  81. }else if((PINA & (1<<PA2)) && flag){ //"Searching-For-Unlock-Signal" state
  82. PORTA |= (1<<PA3);
  83. debugAntiClockWise(); // UnLocked done
  84.  
  85. _delay_ms(400);
  86. flag = 0; // Now check for upcoming lock signal
  87. PORTA &= ~(1<<PA3);
  88. }
  89. }
  90.  
  91.  
  92.  
  93.  
  94. //debugAntiClockWise();
  95. //debugClockWise();
  96.  
  97.  
  98.  
  99.  
  100. // while(1){
  101. // if(PINA & 0x00){
  102. // _setServo();
  103. // moveClockWise();
  104. // }else{
  105. // _setServo();
  106. // moveAntiClockWise();
  107. // }
  108. // _delay_ms(250);
  109. // _resetServo();
  110. // _delay_ms(20000);
  111.  
  112.  
  113. // }
  114.  
  115.  
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement