How to correctly configure dsPIC to receive UART signal

picproteusuart

I want to use Proteus to simulate UART data transmission, I am going to open two virtual COM ports and connect them.

So I first use Virtual Serial Ports Emulator to open virtual COM ports:

enter image description here

Second, I use tera term to be COM1:

enter image description here

Third, my circuit in Proteus to be COM2:

enter image description here

And I set the COMPIM in the circuit as COM2, baud rate 9600.

enter image description here

The code of dsPIC chip in Proteus:

void main() {
     Unlock_IOLOCK();//unlock
     PPS_Mapping_NoLock(0, _INPUT, _U1RX);//Set UART1 Rx be pin RP0
     PPS_Mapping_NoLock(1, _OUTPUT, _U1TX);//Set UART1 Tx be pin RP1
     Lock_IOLOCK();//lock

     UART1_Init(9600);//Set Baud Rate
     Delay_ms(100);//Let UART initialize
     while(1)
     {
            unsigned receive;
            if (UART1_Data_Ready())
            {
              receive = UART1_Read(); 
              UART1_Write(receive); //The result should be sent back to COM1
            }
     }
}

Then I started Proteus simulation, then typed a character 'o' in tera term. As you can see, the virtual terminal on Proteus have received 'o', so the virtual COM ports work fine. But dsPIC seems to lose reading the 'o', so it cannot send it to COM1 back.

enter image description here

If I changed the code and restart the simulation

void main() {
     Unlock_IOLOCK();//unlock
     PPS_Mapping_NoLock(0, _INPUT, _U1RX);//Set UART1 Rx be pin RP0
     PPS_Mapping_NoLock(1, _OUTPUT, _U1TX);//Set UART1 Tx be pin RP1
     Lock_IOLOCK();//lock

     UART1_Init(9600);//Set Baud Rate
     Delay_ms(100);//Let UART initialize
     while(1)
     {
            unsigned receive;
            UART1_Write('a');
            if (UART1_Data_Ready())
            {
              receive = UART1_Read(); 
              UART1_Write(receive); //The result should be sent back to COM1
            }
     }
}

I can see 'a' sent by dsPIC in tera term:

enter image description here

So the UART_Write() seems to be OK, I don't know why dsPIC cannot read data from COM1. Maybe I configured something wrong.

Best Answer

I've triedd your code. The problem is that RB0 and RB1 pin is as well AN2 and AN3. Therefore you have to set them to digital. Add this line in the beginning of your code:

ADPCFG = 0xFFFF

and then the Rx pin can receive the data correctly.

BTW. I've tried PeterJ's comment but it seemed not to be the crucial part of this problem. Your original circuit design works fine once you set the analog pin to digital.