samir82show

Master Code v.1 (preprocessor three mode)

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