PDA

View Full Version : Serial port settings for given crystal



Scampy
- 14th December 2013, 22:49
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



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.



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

Demon
- 14th December 2013, 23:31
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

Scampy
- 15th December 2013, 12:17
Robert, many thanks for the link.

I've entered the parameters for 9600 baud, with a osc of 20 and get the resulting 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 ?

HenrikOlsson
- 15th December 2013, 13:14
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.

Scampy
- 15th December 2013, 13:34
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

HenrikOlsson
- 15th December 2013, 13:45
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.

Scampy
- 15th December 2013, 14:25
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........

Demon
- 15th December 2013, 17:04
Good, I'm going to take credit for solving this. lol

(just woke up)

Robert
:)

Scampy
- 15th December 2013, 17:34
(just woke up)

Robert
:)

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

Demon
- 15th December 2013, 22:11
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
:)