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