# pic Uart receive and transmit

#### sovannarith

Joined Feb 11, 2010
18
Dear forum member,

I have a problem with using uart of pic 16f887 to communicate with PC via VB 6.0.
I can successfully send command from VB to control pic but I try to get text back from pic it doesn't work. but instead read text, I can read back only what I sent out.

I have write a small code for testing using MikroC pro for pic as bellow:
//*******************************************
char dataRx;

void main() {
ANSEL = 0; // Configure AN pins as digital
ANSELH = 0;
trisd=0;
portd=0xff;
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100); // Wait for UART module to stabilize

while (1) { // Endless loop
{
if (dataRx=='A')
{
uart1_write_text("You type A");
uart1_write(13);
uart1_write(10);

}
else if (dataRx=='B')
{
uart1_write_text("You type B");
uart1_write(13);
uart1_write(10);
}
else
{
uart1_write_text("I don't understand");
uart1_write(13);
uart1_write(10);
}
portd = dataRx;
}
}
//***********************************************

And my VB code as bellow:
'************************************************
Dim DataReceive$DataReceive = MSComm1.Input txtDataReceive.Text = DataReceive End Sub Private Sub cmdDataSend_Click() Dim DataSend$
DataSend = txtDataSend.Text
MSComm1.Output = DataSend
End Sub

MSComm1.PortOpen = True
End Sub

MSComm1.PortOpen = False
End Sub
'*********************************************

I have also attached picture of circuit but instead use 10ufF I used 22pF. I don't know this can effect or not.

Thank for helping.

Joined Jul 18, 2013
26,695

#### Brownout

Joined Jan 10, 2012
2,390
I got the same things when I connected Tx and Rx wrong. Make sure you have Tx(computer) -> Rx(Pic) and Rx(computer) ->Tx(Pic)

#### MrChips

Joined Oct 2, 2009
28,151
Use a terminal emulator program to debug your comm setup.

Joined Jul 18, 2013
26,695
Use a terminal emulator program to debug your comm setup.
See post #2
Max.

#### sovannarith

Joined Feb 11, 2010
18
I got the same things when I connected Tx and Rx wrong. Make sure you have Tx(computer) -> Rx(Pic) and Rx(computer) ->Tx(Pic)
I really don't know why?

#### sovannarith

Joined Feb 11, 2010
18
See post #2
Max.
Yes I also try to use uart terminal from MikroC pro but I got the same result that is I can only send command to control PIC but when I try to read back data I can only get what I send out.

#### Attachments

• 150.4 KB Views: 21

#### John P

Joined Oct 14, 2008
1,995
One thing you can do is take the wire from the TX pin of the processor, and connect it to the RX pin. Then anything you send from the computer should be received back by the computer. This tests all the wiring and the level-conversion chip. If that test passes and the chip still isn't communicating when you put the wire back, the problem has to be in the PIC. Not powered, not programmed right, oscillator not functioning, reset always active, there's a lot of things it could be.

#### BobTPH

Joined Jun 5, 2013
6,314
John P,

It sounds like he already has it wired that way, since he is receiving on the PC what he sends on the PC. I think there is a wiring error.

Bob