pic Uart receive and transmit

Discussion in 'The Projects Forum' started by sovannarith, Feb 11, 2015.

  1. sovannarith

    Thread Starter Member

    Feb 11, 2010
    18
    0
    Dear forum member,
    Picture.jpg
    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.
    Could you please help my problem.

    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 (uart1_data_ready())
    {
    dataRx=uart1_read();
    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:
    '************************************************
    Private Sub cmdDataReceive_Click()
    Dim DataReceive$
    DataReceive = MSComm1.Input
    txtDataReceive.Text = DataReceive
    End Sub

    Private Sub cmdDataSend_Click()
    Dim DataSend$
    DataSend = txtDataSend.Text
    MSComm1.Output = DataSend
    End Sub

    Private Sub Form_Load()
    MSComm1.PortOpen = True
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
    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.
     
  2. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,500
    2,366
  3. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    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)
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,415
    3,354
    Use a terminal emulator program to debug your comm setup.
     
  5. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,500
    2,366
    See post #2 ;)
    Max.
     
  6. sovannarith

    Thread Starter Member

    Feb 11, 2010
    18
    0
    Yes I careful about this connection but it doesn't work.
    I really don't know why?
     
  7. sovannarith

    Thread Starter Member

    Feb 11, 2010
    18
    0
    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.
     
  8. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    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.
     
    sovannarith likes this.
  9. BobTPH

    Active Member

    Jun 5, 2013
    782
    114
    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
     
  10. sovannarith

    Thread Starter Member

    Feb 11, 2010
    18
    0
    Thank you all for your comment.
    now I can find the problem it causes from wire connection. I have to connect pin 5 of RS232 to ground level. now it works fine.
    Thank all again.
     
Loading...