Advertisement
nutterar

Input USART data into array

Nov 14th, 2012
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. // This code will read in the USART data and store the result
  2. // in an array.
  3.  
  4.  const short n = 20;
  5.  char rxchar, i = 0, flag = 0; // Variable for storing the data from UART and array counter
  6.  unsigned char rxarray[n];   // array to store the received charaters
  7.  
  8. void interrupt () {
  9.   if (PIR1.RCIF) {          // test the interrupt for uart rx
  10.     rxchar = Usart_Read();  //
  11.     rxarray[i] = rxchar;
  12.     i++;
  13.     // ******************************************************
  14.     // Only select one of the following statements
  15.     if (rxchar == 36) {  // select this if looking for a terminating character
  16.     //if (i == n) (         // select this if looking for a number of characters
  17.       flag = 1;
  18.       } // end if (rxchar == "$")
  19.     } // end  if (PIR1.RCIF)
  20.   } // end interrupt
  21.  
  22.  
  23. void main () {
  24. unsigned short j;
  25.  
  26.  INTCON.GIE = 1;
  27.  INTCON.PEIE = 1;
  28.  PIE1.RCIE = 1; //enable interrupt.
  29.  Usart_Init(9600);
  30.  
  31.  while(1) {   // Begin endless loop
  32.   // This section is where you tell the program what to do with the data once it
  33.   // has all arrived
  34.   if (flag ==1) {
  35.     j = 0;
  36.     while(j < i) {
  37.      Usart_Write(rxarray[j]);
  38.      j++;
  39.      } // end while(rxarray[i])
  40.     i = 0;
  41.     flag = 0;
  42.   } // end if (flag)
  43.   //************************
  44.   // You can place other things to do in your program here
  45.   // i.e. ADC_readings, turn on outputs, etc.
  46.   // ***********************
  47.  
  48.  } // end while
  49. } // end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement