0
\$\begingroup\$

I am trying to write the values of a counter on the serial interface, just to test the interface. The problem is that only every second value (2,4,6,8,...) is received by the desktop. I use port PORTA to verify the values of the counter. A trace of the port shows that the odd values of the counter are present on the port only for a much shorter time, as if it does not have to wait until the value has been written through the relatively slow serial interface (9600 baud). It seems that every second value is never written into the serial port.

If I uncomment the second call of UART1PutChar(a); (last statement in the code below) it all works fine. I receive all the values as expected (1,2,3,4,...), but I cannot expain this behaviour.

void UART1Init(int bautrate)
{
   U1BRG = bautrate;
   U1MODE   =   0x8000;
   U1STA    =   0x8400;
   IFS0bits.U1RXIF = 0;
}

void UART1PutChar(char Ch)
{
   while(U1STAbits.UTXBF == 1);
   U1TXREG = Ch;
}

int16_t main(void)
{

    UART1Init(51);

    TRISA = TRISA & 0xFF00; 
    PORTA = 0;
    unsigned char a = 0;
    int i;
    while(1)
    {
        a = a+1;
        PORTA = a;
        UART1PutChar(a);
        //UART1PutChar(a);
    }
}

Setup:

  • PIC24f16KL402
  • Compiler C30
  • MPLAB X 1.60 with PICKIT 3
\$\endgroup\$
  • \$\begingroup\$ Your post is very hard to understand. Could you please explain what you're trying to do, what's happening and what's not and what you tried to resolve the problem (and what happened then). And all that in good English, please :-) \$\endgroup\$ – user17592 Jan 8 '13 at 21:31
2
\$\begingroup\$

Try checking that the transmit shift register is empty also - add while(TRMT == 0) in your UART1PutChar routine. This should ensure the last transmission is complete before you put the next value in.

\$\endgroup\$
  • \$\begingroup\$ Your tip helped to fix the problem of missing values. The different timings seem come from the fact that the the serial port is buffered and that calling UART1PutChar() does not take the same time each time. For some reasons this timing variations have a repeating every two bytes (fast, slow, fast, slow ...). \$\endgroup\$ – mrks Jan 9 '13 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.