Conditional Defines?


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429

    Default Conditional Defines?

    Are conditional defines possible?

    for example:

    Code:
    IF PORTB.0 THEN
        DEFINE HSER_TXSTA 24h
        DEFINE HSER_SPBRG 8 
        SPBRGH=2
    ELSE   
        DEFINE HSER_TXSTA 20h
        DEFINE HSER_SPBRG 64
        SPBRGH=0
    ENDIF
    I only ask because I wasn't sure if defines were processed in the same way as PBP commands or if they were treated specially by the compiler.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  2. #2
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    Apparently you cant. I just tried it.

    Is there a way I can set the baud for HSERIN / HSEROUT conditionally based on an input pin?

    I tried setting the registers manually like this:

    RCSTA=$90
    TXSTA=$24
    SPBRG=8
    SPBRGH=2

    but then I get errors. I assume the HSERIN / HSEROUT commands require these to be set via the defines?
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    The USART does not need DEFINEd. You should be able to use it like you have tried.
    What are the errors?
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default

    Hi,
    No, DEFINE is a compiler or assembler directive and is not executed during runtime.

    What type of error did you get, which chip are you using and which baudrates are you trying to get. (I don't get any errors here.)

    /Henrik.

    EDIT: Oh well, Dave beat me to it - need to type and think faster....

  5. #5
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    Thanks for the replied. I am using an 18F2550 @ 48MHz. I want to choose baud rate based on 4 external jumpers.

    When i dont have the DEFINEs I get the following warning:

    Warning[202] \PBP\PBPPIC18.LIB 7605 : arguement out of range. least significant bits used.

    Here's my code:

    Code:
    Define OSC 48
    ADCON0=0                            
    ADCON1=$0F
    TRISB=$FF
    INTCON2.7=0 ' Turn on weak pull ups on port B
    
    baud VAR BYTE
    
    baud=PORTB&%00111100
    
    RCSTA = $90                     ' Enable serial port & continuous receive
    
    SELECT CASE baud
        CASE 60
            TXSTA = $20             ' Enable transmit, BRGH = 0
            SPBRG = 15              ' 300 Baud @ 48MHz, 0.0%
            SPBRGH = 39
        CASE 28  
            TXSTA = $20             ' Enable transmit, BRGH = 0
            SPBRG = 135             ' 600 Baud @ 48MHz, 0.0%
            SPBRGH = 19
        CASE 44
            TXSTA = $20             ' Enable transmit, BRGH = 0
            SPBRG = 195             ' 1200 Baud @ 48MHz, 0.0%
            SPBRGH = 9
        CASE 12
            TXSTA = $20             ' Enable transmit, BRGH = 0
            SPBRG = 225             ' 2400 Baud @ 48MHz, 0.0%
            SPBRGH = 4
        CASE 52
            TXSTA = $20             ' Enable transmit, BRGH = 0
            SPBRG = 112             ' 4800 Baud @ 48MHz, 0.0%
            SPBRGH = 2
        CASE 20
            TXSTA = $24             ' Enable transmit, BRGH = 1
            SPBRG = 225             ' 9600 Baud @ 48MHz, 0.0%
            SPBRGH = 4
        CASE 36
            TXSTA = $24             ' Enable transmit, BRGH = 1
            SPBRG = 112             ' 19200 Baud @ 48MHz, 0.0%
            SPBRGH = 2
        CASE 4
            TXSTA = $24             ' Enable transmit, BRGH = 1
            SPBRG = 56              ' 38400 Baud @ 48MHz, -0.16%
            SPBRGH = 1
        CASE 56
            TXSTA = $20             ' Enable transmit, BRGH = 0
            SPBRG = 51              ' 57600 Baud @ 48MHz, 0.16%
            SPBRGH = 0
        CASE 24
            TXSTA = $20             ' Enable transmit, BRGH = 0
            SPBRG = 25              ' 115200 Baud @ 48MHz, 0.16%
            SPBRGH = 0
        CASE ELSE
            TXSTA = $24             ' Enable transmit, BRGH = 1
            SPBRG = 225             ' 9600 Baud @ 48MHz, 0.0%
            SPBRGH = 4
    END SELECT
    BAUDCON.3 = 1                   ' Enable 16 bit baudrate generator
    
    HSEROUT ["Hello World!"]
    If I put in the DEFINES, or if I remove the HSEROUT line, the warning goes away.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    When there are no DEFINE's, and an hserout in your program, PBP defaults to 2400 baud in standard USART mode.
    But 2400 baud is too slow at 48Mhz in standard mode.

    When it tries to initialize the USART at the beginning of the program, the value for SPBRG is larger than 255 and gives the "out of range" warning.

    If you use DEFINE's that will work at 48mhz, then the warning will go away and you can manually set the EUSART registers in Enhanced mode whenever needed, like you are doing.

    DEFINE HSER_BAUD 19200

    Or, you can start with enhanced defines that specify SPBRG, then PBP doesn't try to calculate it.

    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    DEFINE HSER_SPBRG 225 ' 2400 Baud @ 48MHz, 0.0%
    SPBRGH = 4
    BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
    <br>
    Last edited by Darrel Taylor; - 20th June 2010 at 16:53. Reason: Enhanced defines
    DT

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default

    Hi,
    I copy/pasted your code into MCSP and compiled it with PBPW 2.60 without getting any errors. I'm not sure if the keyword DEFINE must be in upper case or not. Try changing your Define OSC 48 to DEFINE OSC 48 and see if that takes care of it.

    /Henrik.

    EDIT: Too late AGAIN....I give up ;-)

  8. #8
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    Thanks DT. I will follow your advice.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  9. #9
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I am confused again. Funny how I never ran into this before...
    The below only works at OSC 32 and below.
    Code:
    RCSTA = $90   ' Enable serial port & continuous receive
    TXSTA = $20   ' Enable transmit, BRGH = 0
    SPBRG = 225   ' 2400 Baud @ 0.0%
    SPBRGH = 4
    BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
    Why does it work above OSC 32 if it is DEFINED?

    But these work with the 202 Warning. 2400 BAUD will not work like this.
    Work meaning it displays on a terminal.
    Code:
    RUN: '9600 BAUD
       RCSTA=$90:TXSTA=$20:SPBRG=77
       RCSTA.4 = 0 : RCSTA.4 = 1
       HSEROUT ["HOWDY THERE",$d,$a]
       PAUSE 250
       GOTO RUN
    Code:
    RUN: '4800 BAUD
       RCSTA=$90:TXSTA=$20:SPBRG=155
       RCSTA.4 = 0 : RCSTA.4 = 1
       HSEROUT ["HOWDY THERE",$d,$a]
       PAUSE 250
       GOTO RUN
    Setup
    Code:
    '4550 HSER TEST'18F4550'06/20/10
    DEFINE OSC 48
    @ __CONFIG   _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
    @ __CONFIG   _CONFIG1H, _FOSC_HSPLL_HS_1H
    @ __CONFIG    _CONFIG2H, _WDT_OFF_2H & _WDTPS_512_2H
    @ __CONFIG    _CONFIG3H, _PBADEN_OFF_3H & _MCLRE_OFF_3H 
    @ __CONFIG    _CONFIG4L, _LVP_OFF_4L & _ICPRT_OFF_4L &_XINST_OFF_4L
    Once a NEWBE always a NEWBE
    Dave
    Always wear safety glasses while programming.

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Dave, it's the same thing as from post #6.

    If there is an HSERIN or HSEROUT anywhere in your program. PBP will initialize the USART at the beginning of the program, before jumping to user code.

    If you don't set the defines, PBP defaults to 2400 baud and standard USART mode (8-bit BRG).
    Anything above 32mhz, and 2400 baud is too slow for a standard USART. SPBRG ends up larger than 255, and throws an out of range warning.

    If you define HSER_SPBRG, then PBP does not try to calculate SPBRG ... hence no warning.
    If you define an HSER_BAUD that works at that OSC, it calculates a valid SPBRG and also doesn't give a warning.

    Once you've made it past the Initialization, you can manually set the EUSART registers for whatever baud rate you want.

    hth,
    DT

  11. #11
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Thanks again Darrel for your help.
    This is the part that I was not getting
    If there is an HSERIN or HSEROUT anywhere in your program. PBP will initialize the USART at the beginning of the program, before jumping to user code.
    For some reason I thought PBP went line by line when compiling so I did not understand why setting the registers before HSERIN did not do the same as DEFINE XX.

    I am beginning to get it.
    Dave
    Always wear safety glasses while programming.

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