Guest User

Untitled

a guest
Apr 20th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/interrupt.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "config.h"
  7. //#include "lcd.h"
  8.  
  9. //////////////////SERVICE////BOOTH/////////////////////////////
  10.  
  11.  
  12.  
  13. int head,tail;
  14. int maxsize;
  15. char q[100][20];
  16.  
  17. bool isEmpty()
  18. {
  19. if (head==tail) return true;
  20. return false;
  21. }
  22. bool isFull()
  23. {
  24. if ((head+1)%maxsize==tail) return true;
  25. return false;
  26. }
  27. bool push(char indata[])
  28. {
  29. if (isFull()) return true;
  30. strcpy(q[head],indata);
  31. head=(head+1)%maxsize;
  32. return true;
  33. }
  34. char* front()
  35. {
  36. if (isEmpty()) return NULL;
  37. return q[tail];
  38. }
  39.  
  40. bool pop()
  41. {
  42. if (isEmpty()) return false;
  43. tail=(tail+1)%maxsize;
  44. return true;
  45. }
  46. int size()
  47. {
  48. int ret=head-tail;
  49. if (ret<0) ret+=maxsize;
  50. return ret;
  51. }
  52.  
  53.  
  54.  
  55. typedef struct
  56. {
  57. uchar rtype; //receiver type
  58. uchar rid; //receiver id
  59. uchar stype; //sender type
  60. uchar sid; //sender id
  61. uchar command; //command type
  62. char data[5]; //main data
  63. char checksum[6]; //checksum
  64. uchar ebit; // Enzam bit
  65. }DATA;
  66.  
  67.  
  68. void printData(DATA data)
  69. {
  70. printf("rtype: %c\n\r",data.rtype);
  71. printf("rid: %c\n\r",data.rid);
  72. printf("stype: %c\n\r",data.stype);
  73. printf("sid: %c\n\r",data.sid);
  74. printf("command: %c\n\r",data.command);
  75. //printf("data: %s\n\r",data.data);
  76. }
  77. //01 23 4 56789 012345 6
  78. DATA perseData(char data[])
  79. {
  80. printf("got - %s\n\r",data);
  81. int i;
  82. DATA ret;
  83. ret.rtype=data[0];
  84. ret.rid=data[1];
  85. ret.stype=data[2];
  86. ret.sid=data[3];
  87. ret.command=data[4];
  88. for (i=0; i<5; i++) ret.data[i]=data[i+5];
  89. for (i=0; i<6; i++) ret.checksum[i]=data[i+10];
  90. ret.ebit=data[16];
  91. return ret;
  92. }
  93.  
  94. ////////////////////process//////////////////////
  95. void processData(DATA data)
  96. {
  97. //NOISE//
  98. if (data.rtype=='S' && data.rid==SELFID)
  99. {
  100. printf("YES i am that guy\n\r");
  101. printData(data);
  102. }
  103. else return ;
  104.  
  105. switch(data.command)
  106. {
  107. case STB_SERVICE_INIT:
  108. break;
  109. case STB_SERVICE_INIT_REPLY:
  110. //if data.data then show to lcd
  111. //else work.push(nextuser);
  112. //work.pop()
  113. if (!strcmp((char*)data.data,"-----"))
  114. {
  115. //LCDWriteString("Why ??");
  116. }
  117. else
  118. {
  119. char s[20];
  120. sprintf(s,"PCS%c%d------------",SELFID,STB_SERVICE_NEXTTOKEN);
  121. push(s);
  122. }
  123. pop();
  124. break;
  125. case STB_SERVICE_NEWUSER:
  126. break;
  127. case STB_SERVICE_NEWUSER_REPLY:
  128. break;
  129. case STB_SERVICE_NEXTTOKEN:
  130. break;
  131. case STB_SERVICE_NEXTTOKEN_REPLY:
  132. //if data.data then
  133. //{ show to lcd
  134. // work.pop()
  135. //}
  136. //else show --lcd
  137.  
  138. if (strcmp(data.data,"------"))
  139. {
  140. //LCDWriteString("Why ??");
  141. pop();
  142. }
  143. else
  144. {
  145. //char s[20];
  146. //sprintf(s,"PCS%c%d------------",SELFID,STB_SERVICE_NEXTTOKEN);
  147. // show -- lcd
  148. }
  149.  
  150. break;
  151. case STB_SERVICE_PING:
  152. //printf("%s\n\r",workq);
  153. printf("%s",front());
  154. break;
  155. case STB_SERVICE_PING_REPLY:
  156. break;
  157. }
  158.  
  159. }
  160.  
  161. //========================================================
  162. int SendCmd(char cmd,FILE *stream)
  163. {
  164. //Do nothing until UDR is ready for more data to be written to it
  165. while ((UCSRA & (1 << UDRE)) == 0x00) ;
  166.  
  167.  
  168. /* Copy 9th bit to TXB8 */
  169. UCSRB &= ~(1<<TXB8);
  170. if ( cmd & 0x0100 )
  171. UCSRB |= (1<<TXB8);
  172. /* Put data into buffer, sends the data */
  173. UDR = cmd; // Echo back the received byte back to the computer
  174.  
  175. return 0;
  176. }
  177.  
  178. int RecieveCmd(FILE *stream)
  179. {
  180. unsigned char ReceivedByte;
  181. while ((UCSRA & (1 << RXC)) == 0x00) {} // Do nothing until data have been recieved and is ready to be read from UDR
  182. ReceivedByte = UDR; // Fetch the recieved byte value into the variable "ByteReceived"
  183. return ReceivedByte;
  184. }
  185. //*******************************************/
  186.  
  187. ISR(USART_RXC_vect){ // Handler for RXD interrupt
  188. uchar in = UDR;
  189. char m[20];
  190. int i;
  191. //for (i=1; i<17; i++) scanf("%c",&m[i]);
  192. gets(m+1);
  193. m[0]=in;
  194. DATA buffer=perseData(m);
  195. processData(buffer);
  196. }
  197.  
  198. /*/////////////////BUTTON/////INT////////////////////
  199.  
  200. INT0
  201. work.push(nextuser);
  202.  
  203. ///////////////////////////////////////////////////*/
  204.  
  205.  
  206.  
  207. int main(){
  208.  
  209. DDRB = 0xFF;
  210. DDRC = 0xFF;
  211. DDRD &= ~1;
  212. DDRD |= 2;
  213.  
  214. //LCDInit(LS_NONE);
  215. //LCDClear();
  216. //GICR |= (1<<INT0);
  217. //MCUCR |= (1<<ISC00) | (1<<ISC01);
  218.  
  219. sei();
  220. //SERIAL PORT
  221. UCSRB |= (1 << RXEN) | (1 << TXEN) | (1<<RXCIE); // Turn on the transmission and reception circuitry
  222. UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1) ; // Use 8-bit character sizes
  223. UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register
  224. UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
  225.  
  226. stdout = fdevopen(SendCmd,NULL);
  227. stdin = fdevopen(NULL,RecieveCmd);
  228.  
  229. //printf("Welcome!\n\rThis is develop by Nayeem, Enzam, Shafiul ,Hira& Dipal.\n\rMachineStarted\n\r 0-INIT\n\r 1-SETvalue\n\r 2-STAT\n\r");
  230. //printf("Welcome! This transmitter will transmit nothing\n\rZZZZZ\n\r");
  231.  
  232.  
  233. head=tail=0;
  234. maxsize=100;
  235.  
  236. char s[20];
  237. sprintf(s,"PCS%c%d------------",SELFID,STB_SERVICE_INIT);
  238. push(s);
  239. while(1){
  240. //work.push(init);
  241. printf("i am aLIVE %c\n\r",SELFID);
  242. _delay_ms(7000);
  243. }
  244.  
  245. return 0;
  246. }
Add Comment
Please, Sign In to add comment