Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Problem in macro value generation

Status
Not open for further replies.

LBdgWgt

Member level 5
Joined
Mar 6, 2006
Messages
83
Helped
5
Reputation
10
Reaction score
0
Trophy points
1,286
Activity points
2,066
Hi all,

i am using C30 compiler for dsPIC30 (i think it basically gcc right?)
i have a problem as i tried to make automatic macro value generation for UART baud rate register.

Code:
#define XTAL                7500000U
#define PLL_MULTIPLIER      8U
#define CLOCK_POSTSCALER    1U
#define F_OSC               (XTAL * PLL_MULTIPLIER)/CLOCK_POSTSCALER
#define FCY                 F_OSC/4U
#define UART_BAUD           9600U
#define UART1_BRG           (FCY/(16U * UART_BAUD) - 1U)

it should be that the result i want:

Code:
UART1BRG = (UINT16) ((7.5M*8/4)/(16*9600)) - 1 = 97 (approximately)

but what as i generate the code, the baud register is always filled with the value of 0x298 (664 decimal)?

thanks for any hints!
 

found the solution:

Code:
#define XTAL                7500000UL
#define PLL_MULTIPLIER      16UL
#define CLOCK_POSTSCALER    1UL
#define F_OSC               ((XTAL * PLL_MULTIPLIER)/CLOCK_POSTSCALER)
#define FCY                 (F_OSC/4UL)
#define UART_BAUD           9600UL
#define UART1_BRG           (UINT16) ((FCY/(16UL * UART_BAUD)) - 1UL)

seems we have to use the brackets and UL for big numbers such as 7500000 in my case

regards,
 

Yes, the C30 compiler is a port of the Gcc compiler.
and a very nice compiler it is too!
Macros have been depreciated and thier use is discouraged, except for simple text sustitutions.
if we expand your macro UART1_BRG we get:
Code:
(UINT16) (((((7500000UL * 16UL)/1UL)/4UL)/(16UL * 9600UL)) - 1UL)

Quite easy to miss a braket and end up with the wrong value?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top