samir82show

Master servo control(two modes)

Jan 28th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.49 KB | None | 0 0
  1. #include <mega32a.h>
  2. #include <spi.h>
  3. #define MAXANGLE 1999
  4. #define MINANGLE 999
  5. #define MOTOR_PLUS 0x01
  6. #define MOTOR_MINUS 0x02
  7. volatile unsigned char mode;
  8. // Timer 0 overflow interrupt service routine
  9. interrupt [TIM0_OVF] void timer0_ovf_isr(void)
  10. {
  11. // Place your code here
  12. static unsigned int OCR1Avar = 1499;
  13. static unsigned char state = 1;
  14. static unsigned char count = 0;
  15. count++;
  16. if(!mode)
  17. {
  18. if(count >= 10)
  19. {
  20. switch(state)
  21. {
  22. case 1: //forward Axial movement
  23. if(OCR1A++ < MAXANGLE);
  24. else
  25. state = 2;
  26. break;
  27. case 2: //forward Vertical movement
  28. if(OCR1B++ < MAXANGLE);
  29. else
  30. state = 3;
  31. break;
  32. case 3: // grabbing object
  33. if(OCR1Avar++ <= MAXANGLE)
  34. spi(MOTOR_PLUS);
  35. else
  36. state = 4;
  37. break;
  38. case 4: //reverse Vertical movement
  39. if(OCR1B-- > MINANGLE);
  40. else
  41. state = 5;
  42. break;
  43. case 5: //reverse Axial movement
  44. if(OCR1A-- > MINANGLE);
  45. else
  46. state = 6;
  47. break;
  48. case 6: //forward Vertical movement
  49. if(OCR1B++ < MAXANGLE);
  50. else
  51. state = 7;
  52. break;
  53. case 7: //release object
  54. if(OCR1Avar-- >= MINANGLE)
  55. spi(MOTOR_MINUS);
  56. else
  57. state = 8;
  58. break;
  59. case 8: //reverse Vertical movement
  60. if(OCR1B-- > MINANGLE);
  61. else
  62. state = 1;
  63. break;
  64. }
  65. count = 0;
  66. }
  67. }
  68. }
  69.  
  70. void main(void)
  71. {
  72. // Declare your local variables here
  73. unsigned char Press_Conf = 0;
  74.  
  75. // Input/Output Ports initialization
  76. // Port A initialization
  77. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  78. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  79. PORTA=0x00;
  80. DDRA=0x00;
  81.  
  82. // Port B initialization
  83. // Func7=Out Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In
  84. // State7=0 State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T
  85. PORTB=0x00;
  86. DDRB=0xB0;
  87.  
  88. // Port C initialization
  89. // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
  90. // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
  91. PORTC=0xFE;
  92. DDRC=0x01;
  93.  
  94. // Port D initialization
  95. // Func7=In Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In
  96. // State7=T State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T
  97. PORTD=0x00;
  98. DDRD=0x30;
  99.  
  100. // Timer/Counter 0 initialization
  101. // Clock source: System Clock
  102. // Clock value: 1000.000 kHz
  103. // Mode: CTC top=OCR0
  104. // OC0 output: Disconnected
  105. TCCR0=0x09;
  106. TCNT0=0x00;
  107. OCR0=0xFF;
  108.  
  109. // Timer/Counter 1 initialization
  110. // Clock source: System Clock
  111. // Clock value: 1000.000 kHz
  112. // Mode: Fast PWM top=ICR1
  113. // OC1A output: Non-Inv.
  114. // OC1B output: Non-Inv.
  115. // Noise Canceler: Off
  116. // Input Capture on Falling Edge
  117. // Timer1 Overflow Interrupt: Off
  118. // Input Capture Interrupt: Off
  119. // Compare A Match Interrupt: Off
  120. // Compare B Match Interrupt: Off
  121. TCCR1A=0b10100010;
  122. TCCR1B=0b00011001;
  123. TCNT1H=0x00;
  124. TCNT1L=0x00;
  125. ICR1H=0x4E;
  126. ICR1L=0x1F;
  127. //OCR1AH=0x05;
  128. //OCR1AL=0xDB;
  129. //OCR1BH=0x05;
  130. OCR1A = 1499;
  131. OCR1B = 1499;
  132.  
  133. // Timer/Counter 2 initialization
  134. // Clock source: System Clock
  135. // Clock value: Timer2 Stopped
  136. // Mode: Normal top=0xFF
  137. // OC2 output: Disconnected
  138. ASSR=0x00;
  139. TCCR2=0x00;
  140. TCNT2=0x00;
  141. OCR2=0x00;
  142.  
  143. // External Interrupt(s) initialization
  144. // INT0: Off
  145. // INT1: Off
  146. // INT2: Off
  147. MCUCR=0x00;
  148. MCUCSR=0x00;
  149.  
  150. // Timer(s)/Counter(s) Interrupt(s) initialization
  151. TIMSK=0x01;
  152.  
  153. // USART initialization
  154. // USART disabled
  155. UCSRB=0x00;
  156.  
  157. // Analog Comparator initialization
  158. // Analog Comparator: Off
  159. // Analog Comparator Input Capture by Timer/Counter 1: Off
  160. ACSR=0x80;
  161. SFIOR=0x00;
  162.  
  163. // ADC initialization
  164. // ADC disabled
  165. ADCSRA=0x00;
  166.  
  167. // SPI initialization
  168. // SPI Type: Master
  169. // SPI Clock Rate: 250.000 kHz
  170. // SPI Clock Phase: Cycle Start
  171. // SPI Clock Polarity: Low
  172. // SPI Data Order: MSB First
  173. SPCR=0x50;
  174. SPSR=0x00;
  175.  
  176. // TWI initialization
  177. // TWI disabled
  178. TWCR=0x00;
  179.  
  180. // Global enable interrupts
  181. #asm("sei")
  182.  
  183. while (1)
  184. {
  185. // Place your code here
  186. if(PINC.1 == 0)
  187. {
  188. Press_Conf++;
  189. if(Press_Conf >= 255)
  190. {
  191. mode = ~mode;
  192. PORTC ^= 1 << PINC0;
  193. Press_Conf = 0;
  194. }
  195. }
  196. if(mode)
  197. {
  198. if(PINC.7 == 0)
  199. {
  200. Press_Conf++;
  201. if(Press_Conf >= 255)
  202. {
  203. if(OCR1A < MAXANGLE)
  204. OCR1A++;
  205. Press_Conf = 0;
  206. }
  207. }
  208. else if(PINC.6 == 0)
  209. {
  210. Press_Conf++;
  211. if(Press_Conf >= 255)
  212. {
  213. if(OCR1A > MINANGLE)
  214. OCR1A--;
  215. Press_Conf = 0;
  216. }
  217. }
  218. if(PINC.5 == 0)
  219. {
  220. Press_Conf++;
  221. if(Press_Conf >= 255)
  222. {
  223. if(OCR1B < MAXANGLE)
  224. OCR1B++;
  225. }
  226. }
  227. else if(PINC.4 == 0)
  228. {
  229. Press_Conf++;
  230. if(Press_Conf >= 255)
  231. {
  232. if(OCR1B > MINANGLE)
  233. OCR1B--;
  234. Press_Conf = 0;
  235. }
  236. }
  237. if(PINC.3 == 0)
  238. {
  239. Press_Conf++;
  240. if(Press_Conf >= 255)
  241. {
  242. spi(MOTOR_PLUS);
  243. Press_Conf = 0;
  244. }
  245. }
  246. else if(PINC.2 == 0)
  247. {
  248. Press_Conf++;
  249. if(Press_Conf >= 255)
  250. {
  251. spi(MOTOR_MINUS);
  252. Press_Conf = 0;
  253. }
  254. }
  255. }
  256. }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment