samir82show

Master servo control(three modes)

Feb 5th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. #include <mega32a.h>
  2. #include <delay.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <spi.h>
  7. #define MAX 10
  8. #define MAXANGLE 1999
  9. #define MINANGLE 999
  10. #define MOTOR_PLUS 0x01
  11. #define MOTOR_MINUS 0x02
  12. struct cmd //command struct
  13. {
  14. char str[MAX], num[MAX];
  15. int i;
  16. } cmd, *cmdptr;
  17. volatile unsigned char mode;
  18. interrupt [EXT_INT0] void ext_int0_isr(void)
  19. {
  20. // Place your code here
  21. if(mode++ >= 2)
  22. mode = 0;
  23. printf("%d",mode);
  24. }
  25.  
  26. interrupt [TIM0_COMP] void timer0_comp_isr(void)
  27. {
  28. //these are the states of the automatic mode they are drove by counter 0
  29. static unsigned int OCR1Avar = 1499;
  30. static unsigned char state = 1;
  31. static unsigned char count = 0;
  32. count++;
  33. if(!mode)
  34. {
  35.  
  36. if(count >= 10)
  37. {
  38. switch(state)
  39. {
  40. case 1:
  41. if(OCR1A++ < MAXANGLE);
  42. else
  43. state = 2;
  44. break;
  45. case 2:
  46. if(OCR1B++ < MAXANGLE);
  47. else
  48. state = 3;
  49. break;
  50. case 3:
  51. if(OCR1Avar++ <= MAXANGLE)
  52. spi(MOTOR_PLUS);
  53. else
  54. state = 4;
  55. break;
  56. case 4:
  57. if(OCR1B-- > MINANGLE);
  58. else
  59. state = 5;
  60. break;
  61. case 5:
  62. if(OCR1A-- > MINANGLE);
  63. else
  64. state = 6;
  65. break;
  66. case 6:
  67. if(OCR1B++ < MAXANGLE);
  68. else
  69. state = 7;
  70. break;
  71. case 7:
  72. if(OCR1Avar-- >= MINANGLE)
  73. spi(MOTOR_MINUS);
  74. else
  75. state = 8;
  76. break;
  77. case 8:
  78. if(OCR1B-- > MINANGLE);
  79. else
  80. state = 1;
  81. break;
  82. }
  83. count = 0;
  84. }
  85. }
  86. }
  87.  
  88. void init()
  89. {
  90. PORTA = 0x00;
  91. DDRA = 0xFF;
  92. PORTB = 0x00;
  93. DDRB = 0xB0;
  94. PORTC = 0xFE;
  95. DDRC = 0x01;
  96. PORTD = 0x04;
  97. DDRD = 0x30;
  98.  
  99. TCCR0=0x09;
  100. TCNT0=0x00;
  101. OCR0=0x0F;
  102. TCCR1A=0xA2;
  103. TCCR1B=0x19;
  104. TCNT1H=0x00;
  105. TCNT1L=0x00;
  106. ICR1H=0x4E;
  107. ICR1L=0x1F;
  108. OCR1A = 1499;
  109. OCR1B = 1499;
  110.  
  111. TIMSK=0x02;
  112.  
  113. UCSRA=0x02;
  114. UCSRB=0b10011000;
  115. UCSRC=0x06;
  116. UBRRH=0x00;
  117. UBRRL=0x0C;
  118.  
  119. ACSR=0x80;
  120. SFIOR=0x00;
  121.  
  122. GICR|=0x40;
  123. MCUCR=0x02;
  124. MCUCSR=0x00;
  125. GIFR=0x40;
  126.  
  127. SPCR=0x50;
  128. SPSR=0x00;
  129. }
  130. void command_split(struct cmd* command)
  131. {
  132. unsigned char i = 0;
  133. int c;
  134. while((c = getchar()) != EOF && c != ':' && i < MAX)
  135. command->str[i++] = c;
  136. command->str[i] = '\0';
  137. i = 0;
  138. while((c = getchar()) != EOF && i < MAX)
  139. command->num[i++] = c;
  140. command->num[i] = '\0';
  141. command->i = atoi(command->num);
  142. }
  143.  
  144. void main(void)
  145. {
  146. unsigned int Press_Conf = 0;
  147. cmdptr = &cmd; //command struct object assignment
  148. // Global enable interrupts
  149. #asm("sei")
  150. init();
  151. while (1)
  152. {
  153. // Place your code here
  154.  
  155. if(mode == 1)
  156. {
  157. Press_Conf++;
  158. if(Press_Conf >= 200)
  159. {
  160. if(PINC.7 == 0)
  161. {
  162. if(OCR1A < MAXANGLE)
  163. OCR1A++;
  164. }
  165. else if(PINC.6 == 0)
  166. {
  167. if(OCR1A > MINANGLE)
  168. OCR1A--;
  169. }
  170. if(PINC.5 == 0)
  171. {
  172. if(OCR1B < MAXANGLE)
  173. OCR1B++;
  174. }
  175. else if(PINC.4 == 0)
  176. {
  177. if(OCR1B > MINANGLE)
  178. OCR1B--;
  179. }
  180. if(PINC.3 == 0)
  181. {
  182. spi(0x01);
  183. }
  184. else if(PINC.2 == 0)
  185. {
  186. spi(0x02);
  187. }
  188. Press_Conf = 0;
  189. }
  190. }//end of mode 1
  191. else if(mode == 2)
  192. {
  193. cmdptr = &cmd;
  194. command_split(cmdptr);
  195. if(!strcmpf(cmd.str,"nothing"))
  196. putchar('N');;
  197. else if(!strcmpf(cmd.str,"vertical"))
  198. puts(cmd.str);
  199. else if(strcmpf(cmd.str,"horzental"))
  200. puts(cmd.str);
  201. else if(!strcmpf(cmd.str,"grab"))
  202. puts(cmd.str);
  203. } //end of mode 2
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment