samir82show

master_receiver_SMS code

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