samir82show

testing serial behaviour

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