2015-01-09 6 views
0

dsPic33을 사용하여 11 바이트 문자열을 수신하여 배열에 배치했지만 완전히 수신하지 못했습니다. 내가 보내는 문자열은 "$ 123456789 #"이며 사진에서 받아야합니다. 아래 코드를 사용해 보았습니다. 어떤 도움을 주시면 감사하겠습니다.dsPic 11 바이트 usart 문자열 수신

char inBytes[11]; 
int i; 
unsigned char temp; 

while (U1STAbits.URXDA != 0) 
{ 
    temp = U1RXREG; 
    if (temp == '$') 
    { 
     inBytes[0] = temp; 
     for(i = 1; i < 10; i++) 
     { 
     if (U1STAbits.URXDA != 0) 
     inChar = U1RXREG; 
     inBytes[i] = inChar; 
     } 
    } 

답변

0

jolati는 최종 값이 너무 낮아 11 바이트를 얻을 수 없다는 점에 좋은 점이 있었지만 다른 바이트가 읽히기 전에 다른 바이트가 사용 가능할 때까지 기다려야한다고 덧붙여 야합니다.

예에서;

void AsyncRX() 
{ 
    //Note that the static variables keeps their value between invocations of the 
    // function. i is set to 0 only on the first run of this function, it keeps 
    // its value on every other run after that. 
    static int i = 0; 
    static char inBytes[11]; 

    //Nothing more to do until there is at least 1 byte available 
    if(!U1STAbits.URXDA) 
     return; 

    //Save the byte and test that our message starts with $ 
    inBytes[i] = U1RXREG; 
    if(inBytes[0] != '$') 
     return; 

    //Test the counter to see if we have a full 11 bytes 
    i++; 
    if(i < 11) 
     return; 

    //Do something with your 11 bytes 
    //... 
    //... 

    //Reset the counter for the next message 
    i = 0; 
} 
: 당신 캔트 사용 인터럽트, 당신은 항상 같은 비 차단 폴링을 사용할 수 있는지

char inBytes[11]; 
int i; 
unsigned char temp; 

while (!U1STAbits.URXDA); //Wait until at least one byte is available 

temp = U1RXREG; 
if (temp == '$') 
{ 
    inBytes[0] = temp; 

    for(i = 1; i < 11; i++) //Loop over range i = 1 to 10 inclusively 
    { 
     while (!U1STAbits.URXDA); //Wait until at least one byte is available 
     inBytes[i] = U1RXREG; 
    } 
} 

이상적으로는, 그것은이 들어오는대로 데이터를 처리 할 수 ​​있도록 인터럽트 비 차단 방식으로이 작업을 수행하지만 것

인터럽트 예제의 경우 폴링 된 버전을 잡고 ISR에 던지기 만하면됩니다. 다음은 그 예입니다. 내가 사용하고있는 dsp33이 무엇인지 알지 못하고 잠시 동안 하이 엔드 코어에서 인터럽트를 프로그래밍하지 않았으므로 한 두 번 변경해야 할 수도 있습니다. 또한 해당 레지스터가 기본적으로 활성화되어 있지 않으므로 인터럽트를 활성화해야합니다.

void __attribute__ ((interrupt, no_auto_psv)) _U1RXInterrupt(void) 
{ 
    //Note that the static variables keeps their value between invocations of the 
    // function. i is set to 0 only on the first run of this function, it keeps 
    // its value on every other run after that. 
    static int i = 0; 
    static char inBytes[11]; 

    //Reset the interrupt flag 
    IFS0bits.U1RXIF = 0; 

    //Use up all bytes in the buffer (the interrupt can be set to only fire 
    // once the buffer has multiple bytes in it). 
    while(U1STAbits.URXDA) 
    { 
     //Save the byte and test that our message starts with $ 
     inBytes[i] = U1RXREG; 
     if(inBytes[0] != '$') 
      continue; 

     //Test the counter to see if we have a full 11 bytes 
     i++; 
     if(i < 11) 
      continue; 

     //Do something with your 11 bytes 
     //... 
     //... 

     //Reset the counter for the next message 
     i = 0; 
    } 
} 
+0

감사합니다. 인터럽트와 함께 어떻게 작동하는지보고 싶습니다. 예제를 가르쳐 주시겠습니까? –

+0

업데이트 된 답변보기 초기 구성을 다루지는 않겠지 만 (어떤 dsp33을 사용하는지 알지 못하고 매우 오랜 시간에 codered dsp ISR을 발견하지 못했습니다.) –

1

for(i = 1; i < 10; i++)은 색인 1에서 데이터 저장을 시작하고 9에서 멈추고 단지 9 바이트입니다. < 10<= 10 또는 < 11으로 변경하십시오.