samir82show

Master Code (preprocessor three mode)

Feb 9th, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 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. #undefine AUTO_MANUAL //define and compile to use automated + manual push button modes, undefine and compile to use GSM mode
  8. #define MAX 10
  9. #define MAXANGLE 1999
  10. #define MINANGLE 999
  11. #define MOTOR_PLUS 0x01
  12. #define MOTOR_MINUS 0x02
  13.  
  14. #ifndef AUTO_MANUAL
  15. struct cmd // struct to hold the splitted command
  16. {
  17. char str[MAX], num[MAX];
  18. unsigned int i;
  19. } cmd, *cmdptr;
  20. #endif
  21. volatile unsigned char mode; //variable mode switching between Auto and Manual mode
  22.  
  23. // External Interrupt 0 service routine for mode switching
  24. interrupt [EXT_INT0] void ext_int0_isr(void)
  25. {
  26. // Place your code here
  27. if(mode++ >= 1)
  28. mode = 0;
  29. printf("%d",mode);
  30. }
  31.  
  32. #ifndef RXB8
  33. #define RXB8 1
  34. #endif
  35.  
  36. #ifndef TXB8
  37. #define TXB8 0
  38. #endif
  39.  
  40. #ifndef UPE
  41. #define UPE 2
  42. #endif
  43.  
  44. #ifndef DOR
  45. #define DOR 3
  46. #endif
  47.  
  48. #ifndef FE
  49. #define FE 4
  50. #endif
  51.  
  52. #ifndef UDRE
  53. #define UDRE 5
  54. #endif
  55.  
  56. #ifndef RXC
  57. #define RXC 7
  58. #endif
  59.  
  60. #define FRAMING_ERROR (1<<FE)
  61. #define PARITY_ERROR (1<<UPE)
  62. #define DATA_OVERRUN (1<<DOR)
  63. #define DATA_REGISTER_EMPTY (1<<UDRE)
  64. #define RX_COMPLETE (1<<RXC)
  65.  
  66. // USART Receiver buffer
  67. #define RX_BUFFER_SIZE 8
  68. char rx_buffer[RX_BUFFER_SIZE];
  69.  
  70. #if RX_BUFFER_SIZE <= 256
  71. unsigned char rx_wr_index,rx_rd_index,rx_counter;
  72. #else
  73. unsigned int rx_wr_index,rx_rd_index,rx_counter;
  74. #endif
  75.  
  76. // This flag is set on USART Receiver buffer overflow
  77. bit rx_buffer_overflow;
  78.  
  79. // USART Receiver interrupt service routine
  80. interrupt [USART_RXC] void usart_rx_isr(void)
  81. {
  82. char status,data;
  83. status=UCSRA;
  84. data=UDR;
  85. if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
  86. {
  87. rx_buffer[rx_wr_index++]=data;
  88. #if RX_BUFFER_SIZE == 256
  89. // special case for receiver buffer size=256
  90. if (++rx_counter == 0)
  91. {
  92. #else
  93. if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
  94. if (++rx_counter == RX_BUFFER_SIZE)
  95. {
  96. rx_counter=0;
  97. #endif
  98. rx_buffer_overflow=1;
  99. }
  100. }
  101. }
  102.  
  103. #ifndef _DEBUG_TERMINAL_IO_
  104. // Get a character from the USART Receiver buffer
  105. #define _ALTERNATE_GETCHAR_
  106. #pragma used+
  107. char getchar(void)
  108. {
  109. char data;
  110. while (rx_counter==0);
  111. data=rx_buffer[rx_rd_index++];
  112. #if RX_BUFFER_SIZE != 256
  113. if (rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
  114. #endif
  115. #asm("cli")
  116. --rx_counter;
  117. #asm("sei")
  118. return data;
  119. }
  120. #pragma used-
  121. #endif
  122.  
  123. // Timer 0 output compare interrupt service routine
  124. #ifdef AUTO_MANUAL
  125. interrupt [TIM0_COMP] void timer0_comp_isr(void)
  126. {
  127. //these are the states of the automatic mode they are driven by counter 0
  128. static unsigned int OCR1Avar = 1499;
  129. static unsigned char state = 1;
  130. static unsigned char count = 0;
  131. count++;
  132. if(!mode)
  133. {
  134. if(count >= 10)
  135. {
  136. switch(state)
  137. {
  138. case 1:
  139. if(OCR1A++ < MAXANGLE);
  140. else
  141. state = 2;
  142. break;
  143. case 2:
  144. if(OCR1B++ < MAXANGLE);
  145. else
  146. state = 3;
  147. break;
  148. case 3:
  149. if(OCR1Avar++ <= MAXANGLE)
  150. spi(MOTOR_PLUS);
  151. else
  152. state = 4;
  153. break;
  154. case 4:
  155. if(OCR1B-- > MINANGLE);
  156. else
  157. state = 5;
  158. break;
  159. case 5:
  160. if(OCR1A-- > MINANGLE);
  161. else
  162. state = 6;
  163. break;
  164. case 6:
  165. if(OCR1B++ < MAXANGLE);
  166. else
  167. state = 7;
  168. break;
  169. case 7:
  170. if(OCR1Avar-- >= MINANGLE)
  171. spi(MOTOR_MINUS);
  172. else
  173. state = 8;
  174. break;
  175. case 8:
  176. if(OCR1B-- > MINANGLE);
  177. else
  178. state = 1;
  179. break;
  180. }
  181. count = 0;
  182. }
  183. }
  184. }
  185. #endif
  186. #ifndef AUTO_MANUAL
  187. void command_split(struct cmd* command) //this function is to process command into two values
  188. {
  189. unsigned char i = 0;
  190. int c;
  191. while((command->str[0] = getchar()) != '$');
  192. while((c = getchar()) != ':' && i < MAX)
  193. command->str[i++] = c;
  194. command->str[i] = '\0';
  195. i = 0;
  196. while((c = getchar()) != '#' && i < MAX)
  197. command->num[i++] = c;
  198. command->num[i] = '\0';
  199. command->i = atoi(command->num);
  200. }
  201. #endif
  202.  
  203.  
  204. void init() //initialization function
  205. {
  206. PORTA = 0x00;
  207. DDRA = 0xFF;
  208. PORTB = 0x00;
  209. DDRB = 0xB0;
  210. PORTC = 0xFE;
  211. DDRC = 0x01;
  212. PORTD = 0x04;
  213. DDRD = 0x30;
  214.  
  215. TCCR0=0x09;
  216. TCNT0=0x00;
  217. OCR0=0x0F;
  218. TCCR1A=0xA2;
  219. TCCR1B=0x19;
  220. TCNT1H=0x00;
  221. TCNT1L=0x00;
  222. ICR1H=0x4E;
  223. ICR1L=0x1F;
  224. OCR1A = 1499;
  225. OCR1B = 1499;
  226.  
  227. #ifdef AUTO_MANUAL
  228. TIMSK = 0x02;
  229. #else
  230. TIMSK = 0x00;
  231.  
  232. UCSRA=0x00;
  233. UCSRB=0xD8;
  234. UCSRC=0x06;
  235. UBRRH=0x00;
  236. UBRRL=0x0C;
  237. #endif
  238.  
  239. ACSR=0x80;
  240. SFIOR=0x00;
  241.  
  242. GICR|=0x40;
  243. MCUCR=0x02;
  244. MCUCSR=0x00;
  245. GIFR=0x40;
  246.  
  247. SPCR=0x50;
  248. SPSR=0x00;
  249. }
  250.  
  251. void main(void)
  252. {
  253. #ifndef AUTO_MANUAL
  254. unsigned char minimized_angle; //this variable is to hold the value sent to the remote motor in the slave micro
  255. cmdptr = &cmd; //command struct object assignment
  256. #endif
  257. #ifdef AUTO_MANUAL
  258. unsigned int Press_Conf = 0;
  259. #endif
  260. // Global enable interrupts
  261. #asm("sei")
  262. init();
  263. while (1)
  264. {
  265. // Place your code here
  266. #ifdef AUTO_MANUAL
  267. if(mode == 1)
  268. {
  269. Press_Conf++;
  270. if(Press_Conf >= 200)
  271. {
  272. if(PINC.7 == 0)
  273. {
  274. if(OCR1A < MAXANGLE)
  275. OCR1A++;
  276. }
  277. else if(PINC.6 == 0)
  278. {
  279. if(OCR1A > MINANGLE)
  280. OCR1A--;
  281. }
  282. if(PINC.5 == 0)
  283. {
  284. if(OCR1B < MAXANGLE)
  285. OCR1B++;
  286. }
  287. else if(PINC.4 == 0)
  288. {
  289. if(OCR1B > MINANGLE)
  290. OCR1B--;
  291. }
  292. if(PINC.3 == 0)
  293. {
  294. spi(0x01);
  295. }
  296. else if(PINC.2 == 0)
  297. {
  298. spi(0x02);
  299. }
  300. Press_Conf = 0;
  301. }
  302. }//end of mode 1
  303. #endif
  304. #ifndef AUTO_MANUAL //start of GSM mode
  305. cmdptr = &cmd;
  306. command_split(cmdptr);
  307. if(!strcmpf(cmd.str,"vertical"))
  308. {
  309. OCR1A = cmd.i;
  310. }
  311. else if(!strcmpf(cmd.str,"horzental"))
  312. {
  313. OCR1B = cmd.i;
  314. }
  315. else if(!strcmpf(cmd.str,"grab"))
  316. {
  317. minimized_angle = cmd.i/10; //dividing the value of degree by 10 to get char size number to be able to send it thru SPI
  318. spi(minimized_angle); //sending the minimized angle value through SPI
  319. } //end of GSM mode
  320. #endif
  321. }
  322. }
Advertisement
Add Comment
Please, Sign In to add comment