Advertisement
Guest User

RX problems

a guest
Sep 19th, 2010
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.85 KB | None | 0 0
  1. /** D A T A   B U F F E R S   A N D   F L A G S **********************/
  2.  
  3. unsigned char TXBuffer[128];
  4. unsigned char TXCurrentPos = 0x00;
  5. unsigned char TXEndPos = 0x00;
  6. unsigned char TXBufferSize = 0x00;
  7.  
  8. //if curpos == endpos, then the buffer is empty
  9. //after sending a byte, increment the curpos by 1
  10. //after adding a byte to the buffer, increment the endpos
  11.  
  12.  
  13. unsigned char ByteReceived = 0x00;
  14.     // Set in the interrupt so it can be passed to main
  15.     // After main reads it, main should clear it
  16.    
  17. unsigned char countdown = 0;
  18.  
  19. unsigned int time = 0x0000;
  20. unsigned char Switch_Count = 0;
  21.  
  22. unsigned char waitingForACK = 0;
  23. unsigned char currentlySendingHandshake = 1; //If 1 then we're sending handshake
  24.                                        //else we're sending data
  25.  
  26.    
  27.  
  28.  
  29.  
  30.  
  31. /** D E C L A R A T I O N S ******************************************/
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. //creates a two dimensional array used to store the pins[pinA][data1][data2]
  39. //and their AD values.                                  [pinB][data1][data2]
  40. unsigned char DATA_ARRAY[USED_AD_PINS][3];
  41.  
  42. //size counts the size of the array we will be sending to the SD card
  43. //each time we do. This will be used in the handshake to tell the SD
  44. // module the number of bytes to expect. If we make any changes to the
  45. // packet scheme this will need to be changed.
  46. unsigned char size = USED_AD_PINS * 3;
  47.  
  48.  
  49.  
  50.  
  51. /** I N T E R R U P T S **********************************************/
  52.  
  53. #pragma interrupt InterruptVectorHigh
  54. void InterruptVectorHigh(void){
  55.    
  56.     if(PIR1bits.RCIF == 1){
  57.    
  58.             TRISD = 0x00;
  59.     LATDbits.LATD3 = ~LATDbits.LATD3;
  60.    
  61.         ByteReceived = RCREG;
  62.        
  63.         if(waitingForACK == 1){ //if we're waiting for ACK..
  64.             if(ByteReceived == 0x06){ //and we get ACK
  65.                 currentlySendingHandshake = ~currentlySendingHandshake;
  66.                
  67.                 if(currentlySendingHandshake == 1){
  68.                     countdown = 13;
  69.                 }
  70.    
  71.                 else{
  72.                     countdown = 3;
  73.                 }
  74.                
  75.                 if(TXBufferSize > 0){
  76.                
  77.                     PIE1bits.TXIE = 1; //Finally reenable TX INT.
  78.                
  79.                 } //if there is data to send
  80.                
  81.                 waitingForACK = 0; //we're no longer waiting for ack
  82.             }
  83.         }
  84.    
  85.     }
  86.    
  87.     else if (PIR1bits.TXIF == 1){
  88.        
  89.             TRISD = 0x00;
  90.     LATDbits.LATD2 = ~LATDbits.LATD2;
  91.        
  92.        
  93.         if(TXBufferSize > 0){         // if there is data in the buffer
  94.             TXBufferSize--;      // update the amount of data in buffer
  95.             TXREG = TXBuffer[TXEndPos];           // send next byte
  96.             TXCurrentPos++;               // update to the new position
  97.             countdown--;
  98.         }
  99.        
  100.         if(TXBufferSize == 0){
  101.             PIE1bits.TXIE = 0; // If we run out of data in the buffer
  102.                                // pause TX
  103.         }
  104.        
  105.         if(countdown == 0){
  106.             waitingForACK = 1; //we're now waiting for ack
  107.             PIE1bits.TXIE = 0; //stop sending data until we get ack
  108.         }
  109.        
  110.        
  111.        
  112.        
  113.    
  114.     }
  115.  
  116.  
  117. }
  118.  
  119. #pragma interruptlow InterruptVectorLow
  120. void InterruptVectorLow(void){
  121.  
  122.  
  123.  
  124. }
  125.  
  126.  
  127.  
  128.  
  129. /** M A I N ***********************************************************/
  130.  
  131.  
  132.  
  133.  
  134.  
  135. void main (void){
  136.  
  137.  
  138.     uartINIT();
  139.     rxEnable();
  140.     globalInterruptINIT();
  141.     TXInterruptINIT();
  142.     RXInterruptINIT();
  143.     Delay10KTCYx(50); //delay a while to give the SD card some time
  144.     TXREG = 0x55; //initialize SD card
  145.     while(ByteReceived != 0x06){ // wait for adknowledgement from SD
  146.        
  147.     }
  148.    
  149.    
  150.  
  151.             addToBuffer(0x74);
  152.             addToBuffer(0x88);
  153.             addToBuffer('A');
  154.             addToBuffer(0x2E);
  155.             addToBuffer(0x63);
  156.             addToBuffer(0x73);
  157.             addToBuffer(0x76);
  158.             addToBuffer(0x00);
  159.             addToBuffer(0x00);
  160.             addToBuffer(0x00);
  161.             addToBuffer(0x00);
  162.             addToBuffer(0x03);
  163.             addToBuffer(0x30);
  164.             addToBuffer(0x31);
  165.             addToBuffer(0x32);
  166.            
  167.             Delay1KTCYx(500);
  168.            
  169.             PIE1bits.TXIE = 1; //enable tx interrupt
  170.  
  171. }
  172.  
  173.  
  174.  
  175. void addToBuffer(char data){
  176.  
  177.     TXBuffer[TXEndPos] = data;
  178.     TXEndPos++;
  179.     TXBufferSize++;
  180.     //if(waitingForACK == 0){
  181.         //PIE1bits.TXIE = 1; //Enable the TX interrupt
  182.     //}              //If we're not waiting for ack
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement