Im using SetBaudrate(br2400) but getting 9600?

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Im using SetBaudrate(br2400) but getting 9600?

Post by TonyR » Mon Mar 28, 2011 5:08 pm

Im using SetBaudrate(br2400) but getting 9600?

PIC18F87J50 with 48MHz external clock. Do I need to set something else? Thanks.

Code: Select all

Device = 18F87J50                   
Clock = 48
 
Include "usart.bas" 
 
Dim LED As PORTE.1 
     
SetBaudrate(br2400)
  
 While true

   High(LED)
   DelayMS(100)      
    '
    ' Send parameters to PC
    '
    Write("*")                     ' synch word for comms watchdog
    Write("337",13)             ' rail volts
    Write("009",13)             'Avge amps on coil
    Write("907.278",13)       ' Avge volts on coil
    Write("555.555",13)       ' Switching freq
    Write("025",13)             ' Ambient
    Write("093",13)             ' IGBT temp
    Write("132",13)             ' coil temp
    Write("149",13)             ' target temp
    Write("#")                     ' end of packet
   
   Low(LED) 
   DelayMS(100)
        
Wend

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Mon Mar 28, 2011 7:41 pm

Keep in mind that the "clock = " statement doesn't actually setup anything hardware-wise... it's purely so the compiler can know what speed you're running at so it can adjust software delays, timers, etc.

You have to setup the CONFIG and OSCONx/OSCTUNE registers yourself, however, to actually get the chip running at that freq.

For the usart, you'll probably want to enable the 16-bit BRG (which is off by default).
Try
Code:

#option USART_BRGH = true
#option USART_BRG16 = true
include "usart.bas"

to get both the high-speed divisor and 16-bit BRG enabled

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Post by TonyR » Tue Mar 29, 2011 1:21 am

Thanks for that advice Jerry. Because I'm in a hurry with this project I pasted in the #option statements and hoped that would fix the problem without requiring any thinking on my part! But it didn't fix.

Do I need to set the CONFIG register and OSCON register as well as setup BRG?

This will require thinking and reading?

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Mar 29, 2011 9:12 am

Tony,

If you're using an external 48MHz clock, you'd be better off changing to "Config FOSC = EC". I don't think you need to change the other options/registers to run w/an external clock... it looks like they default ok. You'd only need to set them up if you had to enable the PLL and dividers.

But, it appears you've been bitten by the 16-bit baudrate generator problem I've pointed out a few times http://www.sfcompiler.co.uk/forum/viewtopic.php?t=944.

At 48MHz and 2400 baud, you have no choice but to use the 16-bit BRG, so leave those two options in and change SetBaudrate() in usart.bas

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Post by TonyR » Wed Mar 30, 2011 5:12 am

Hi Jerry,
I pasted in

#option USART_BRGH = true
#option USART_BRG16 = true
Include "usart-2.bas"

and changed setBaudrate to

Public Sub SetBaudrate(pSPBRG As Word = br19200)
SPBRG = pSPBRG.byte0
SPBRGH = pSPBRG.byte1

but at compile time SPBRGH is "Identifier not declared"

Looking in 18F87J50 file I find there is SPBRGH1, SPBRGH2, SPBRG1, SPBRG2 and SPBRG but not SPBRGH

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Post by TonyR » Wed Mar 30, 2011 5:33 am

Its fixed, thank you for the valuable clues Jerry!

I changed setBaudrate to

Public Sub SetBaudrate(pSPBRG As Word = br19200)
SPBRG1 = pSPBRG.byte0
SPBRGH1 = pSPBRG.byte1


For 18F87J50 at least

Post Reply