1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

PIC18F and USART

Discussion in 'Microcontrollers' started by adrianvon, Oct 24, 2013.

  1. Little Ghostman

    Little Ghostman Well-Known Member Most Helpful Member

    Joined:
    Jan 7, 2011
    Messages:
    3,698
    Likes:
    426
    Location:
    SCOTLAND
    also make sure you have hyper terminal set to the same baud rate that you are using on the pic! i got caught out with that

    LG
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    906
    Location:
    Rochdale UK
    ONLINE
    What speed are you setting Hyperterminal?... Should be 9600..
     
  3. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,174
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi adrianvon,
    I assume you have the correct level shifting hardware between the PIC and the PC's serial port.?
    E
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2

    Could the problem be from the USB to RS232 convertor cable or the driver?

    Should BRGH be 0 or 1.

    The oscillator used is 20MHz and the baud rate is set to 9600. So that shouldn't be the problem.

    What is "level shifting hardware" please? Are you referring to the MAX232 IC ?? Sorry for asking such stupid questions but this is the first time trying to use UART.

    I don't know what possible could be wrong. This morning I changed the PIC, changed the MAX232 with ST232 and re build the circuit, but still giving problems :(
     
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,174
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    The 'RS232 serial' levels from a PIC are 0/+5V and are of the opposite 'sense' of the signals the PC serial input expects, ie: +/-9v [ typical]
    A Max232 IC is used between the PIC and the PC serial i/o
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    906
    Location:
    Rochdale UK
    ONLINE
    Code is now fine....

    What RS232 converter do you have... Some work with 0.1uf caps and some do not!!! I think you have a hardware issue now..
     
  8. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Yes I did connect a MAX232 IC between the PIC and the laptop. Today I also changed it with an ST232 just to make sure the problem wasn't from this IC.

    I am using a Manhattan USB to serial, like the one in the link below:

    http://www.manhattan-products.com/en-US/products/708-usb-to-serial-converter

    How should I know what capaciyor values to use please?
     
  9. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,174
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    The way I read the details of the link you posted, suggests that is already fully compatible as a 'RS232 serial' port, so it would expect standard RS232 signals, using a MAX 232 or equivalent.

    Do you have a diagram to post of the PIC/MAX232 to PC wiring.?
     
  10. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    I edited the attached diagram to show how the connections were made:
     

    Attached Files:

  11. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    906
    Location:
    Rochdale UK
    ONLINE
    Just try 10uf caps on the converter.... also I would suggest tying pin 8 & 10 of the MAX chip to ground...
     
  12. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    I am going to re-install the Hyper Terminal and it is asking for the following details. What shell I input please?
     

    Attached Files:

  13. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,174
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    If I am reading your diagram correctly, you have the 9 way 'D' connector wired the wrong way around!
    Pin 5 is Ground, pins 2 and 3 are Tx and RX

    It should be as this image.

    [​IMG]
     
  14. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2

    Sorry I did a mistake while editing the diagram. I re-uploaded the diagram.

    Regarding post #51, what shell I input please?
     
  15. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,174
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi adrian,
    I do not use hyper terminal, Ian is the best guy to ask.
    Noted your corrected drawing..:)
    E
     
  16. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Thanks for your help :)

    I was reading on how to set hyper terminal and it looks like it only works with Windows XP. Is that true? because I am using Windows 7 (64bit). Can I use PuTTY instead?
     
  17. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    906
    Location:
    Rochdale UK
    ONLINE
    You need to set Hyper terminal to use a direct COM connection... When you plug your USB/RS232 cable, you should get a enumerated port number... ie.. COM7 or something..

    Yes!! You can use putty... But the same applies.... Once you configure the virtual com, you can use any terminal software...
     
  18. adrianvon

    adrianvon Member

    Joined:
    Jan 22, 2011
    Messages:
    322
    Likes:
    2
    Ian Rogers, the coding below is the one you gave me:

    Code (text):

    if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set
        {
            Temp_Instruction = RCREG;

            while(!TXSTAbits.TRMT);
                TXREG =Temp_Instruction; // echo received data back to sender
            }
        }
     
    I was following the tutorial in the link below, where it states that PIR1.RCIF must be 1 to indicate that the data is received. Hence, it should be while PIR1.RCIF=0 so it keeps looping till data is received. The received data is then moved to a variable. From that variable it is then moved to TXREG (i.e. before the while(!TXSTAbits.TRMT); ).
    He then used while(PIR1.TXIF == 0); to wait for data to be transmitted.

    Although I did follow these steps (just in case) it is still not working.

    Are the RCREG and TXREG registers cleared automatically when data is received/transmitted ?? or do I have to clear then manually ?

    http://www.embeddedtutorials.com/usart-programming-in-pic18f458/
     
  19. tunedwolf

    tunedwolf Well-Known Member

    Joined:
    Aug 9, 2006
    Messages:
    1,080
    Likes:
    62
    Location:
    UK
    It might be that your cable is being installed as Com-5 or something, have you checked the device manager in windows? Make sure whatever port your cable has been allocated by windows, is the same as you have set in your terminal software :)
     
  20. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,141
    Likes:
    906
    Location:
    Rochdale UK
    ONLINE
    Yes... The RCIF is cleared when the register is read.... The TXIF isn't being used... The reason I don't use it is the TXIF is set BEFORE the TXREG is empty... So I use the TRMT flag, this is cleared when the TXREG is empty, which is what we need..

    I have tested the software as posted!!! If you are using C18 and used the OpenUSART(), WriteUSART() and ReadUSART() that they provide, you'll see that the software is okay..
     
  21. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Download PuTTY. It is the best terminal software I have seen. It is free. No need to install anything. Just run it.
    http://www.putty.org/

    Direct link for windows PuTTY: http://the.earth.li/~sgtatham/putty/latest/x86/putty.exe
     

Share This Page