I am trying to connect a Bluetooth module through serial communication. But the error which is " error: expected ';', ',' or ')' before numeric constant" occurred on the line where I defined the baud rate. can not find where the error is. Help would be appreciated, thanks!
#define BAUD_RATE 9600
#define BAUD_PreS (((F_CPU / (BAUD_RATE * 16UL))) - 1)
void USART_init(long BAUD_RATE){
UCSRB |= (1 << RXEN ) | (1 << TXEN ) ;
UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1) ;
UBRRH = (BAUD_PreS >> 8);
UBRRL = BAUD_PreS;
}
CodePudding user response:
The precompiler will replace BAUD_RATE
with 9600
, so this:
#define BAUD_RATE 9600
void USART_init(long BAUD_RATE){
becomes
#define BAUD_RATE 9600
void USART_init(long 9600){
// ^^^^^^^^^
So, you need to give the variable in USART_init
a different name - but, since it's not even used in the function, just remove it:
void USART_init(){
CodePudding user response:
Get rid of the line
#define BAUD_RATE 9600
It looks like you want BAUD_PreS
to expand into something that depends on the argument to USART_init()
. So you shouldn't define that as a macro, it needs to get the value that was passed as the argument to the function.
Usually this type of macro is written as a function-style macro.
#define BAUD_PreS(r) (((F_CPU / ((r) * 16UL))) - 1)
Then you would use it as:
UBRRH = (BAUD_PreS(BAUD_RATE) >> 8);
UBRRL = BAUD_PreS(BAUD_RATE);