Serial port settings for given crystal


Closed Thread
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2009
    Posts
    583

    Default Serial port settings for given crystal

    Just need some guidance on setting up the defines for serial communications when using a particular crystal.

    I'm using a 20 mhz crystal.

    Here are my config settings

    Code:
    ASM 
      __CONFIG    _CONFIG1H, _OSC_HS_1H
      __CONFIG    _CONFIG2L, _PWRT_ON_2L  
      __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
      __CONFIG    _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H  
      __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM
    I've cut and pasted code from a previous project but that won't allow connection with the port.

    Code:
    DEFINE HSER_BAUD    115200        ' Hser baud rate 
    DEFINE HSER_RCSTA   90h           ' Hser receive status init 
    DEFINE HSER_TXSTA   20h           ' Hser transmit status init 
    DEFINE HSER_CLROERR 1             ' Hser clear overflow automatically 
    DEFINE HSER_SPBRG 25  ' 115200 Baud @ 40MHz, -0.22%
    SPBRGH = 0
    BAUDCON.3 = 1         ' Enable 16 bit baudrate generator
    ColumnWidth CON 10
    I've tried changing the baud rate to 9600, and different values for SPBRG as I'm not running the PIC at 40Mhz...

    Any help would be appreciated

    Malcolm
    Last edited by Scampy; - 14th December 2013 at 22:54.

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,625


    Did you find this post helpful? Yes | No

    Default Re: Serial port settings for given crystal

    Off the top of my head, you have 115200 Baud @ 40MHz with 20MHz crystal.

    Robert

    Edit: Have you tried Steve's baud rate calculator tool?

    http://www.picbasic.co.uk/forum/showthread.php?t=4994
    Last edited by Demon; - 14th December 2013 at 23:35. Reason: link

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Serial port settings for given crystal

    Robert, many thanks for the link.

    I've entered the parameters for 9600 baud, with a osc of 20 and get the resulting code

    Code:
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    DEFINE HSER_SPBRG 8   ' 9600 Baud @ SPBRGH = 2
    BAUDCON.3 = 1         ' Enable 16 bit baudrate generator
    RCSTA = $90   ' Enable serial port & continuous receive
    TXSTA = $24   ' Enable transmit, BRGH = 1
    SPBRG = 8     ' 9600 Baud @ -0.03%
    SPBRGH = 2
    However I still get an "unable to open comport" message when using the serial comm tool in Microcode studio..

    I'm using a USB to 5v TTL serial cable that is based on a prolific chipset, and to test this I loaded up an example code I had from a previous project, which relied on a different crystal (12 mhz) and then used the PPL multiplier to up the OSC to 48. This code simply echoed what is sent via the TX box and worked fine, so the physical communication between PC and PIC is fine. The only drawback of using the 12mhz xtal and the OSC48 is that the LCD, and temp sensor comms are screwed. I really need the serial coms to work with a OSC of 20 and a 20mhz xtal as the rest of the code (temp sensor, RTC etc ) all work fine at these settings.

    Any ideas ?

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,523


    Did you find this post helpful? Yes | No

    Default Re: Serial port settings for given crystal

    Hi,
    The fact that the PC can't open the COM-port has absolutely nothing to do with the code in PIC.

    The problem is with the USB dongle, its drivers, the USB port or the PC. Plug the dongle into your USB-port, open the device manager and check which COM-port number Windows has assigned to the dongle. Then make sure you select that COM-port in the Serial Communicator.

    Another possible cause for not being able to open the port is that it's already opened, perhaps by another program or possibly that some program didn't close it properly. Try restarting the computer.

    /Henrik.

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Serial port settings for given crystal

    Thanks for the suggestions, but as I said I can load one HEX file, place a 12mHz xtal in the development board and click on connect in the serial tool and it works fine. Disconnect the port, load up the other code, replace the 12mhz xtal with a 20 mhz with identical port setting in the serial tool (and those that match the settings in the code) click on connect and get the "unable to open com port" type message.

    Settings in device manager mimic those set in the code. No issue reported, driver provided by MS

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,523


    Did you find this post helpful? Yes | No

    Default Re: Serial port settings for given crystal

    That really doesn't sound right. In fact, you should be able to open the port (ie click connect in Serial Communicator) without having anything what so ever connected to the dongle. And you should still be able to open the port even if the baudrate in your PIC program is different from the one you setup in Serial Communicator - it won't work of course but you should still be able to open the port.

    But OK, if that is the case I'm afraid I don't have the faintest idea what's going on :-(

    /Henrik.

  7. #7
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Serial port settings for given crystal

    Moral of this story... don't rely on cheap crap from china...

    I have a sparkfun breakout board that's based around an FTDI chip - used this and was able to communicate 1st time........

  8. #8
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,625


    Did you find this post helpful? Yes | No

    Default Re: Serial port settings for given crystal

    Good, I'm going to take credit for solving this. lol

    (just woke up)

    Robert

  9. #9
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Serial port settings for given crystal

    Quote Originally Posted by Demon View Post
    (just woke up)

    Robert
    must of been a good night last night.... must be around midday in your part of the world

  10. #10
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,625


    Did you find this post helpful? Yes | No

    Default Re: Serial port settings for given crystal

    Company Christmas supper last night, wife supervises night shift at Tim Hortons (local coffee shop).

    Turned out to be fun, you can't beat filet mignon!

    Robert

Similar Threads

  1. serial port program in c
    By naveenupendran in forum Off Topic
    Replies: 0
    Last Post: - 28th February 2009, 11:47
  2. interfacing to the pc serial port
    By kelangfei in forum General
    Replies: 4
    Last Post: - 7th October 2007, 22:35
  3. PC serial port funny
    By nicjo in forum Serial
    Replies: 13
    Last Post: - 6th February 2007, 05:34
  4. 5v From Serial Port
    By TONIGALEA in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 29th November 2004, 12:49
  5. Replies: 1
    Last Post: - 30th June 2004, 08:07

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts