0

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;
}
  • 1
    You can't use `BAUD_RATE` as a function argument, since it's a macro that expands into a number. You've written the equivalent of `void USART_init(long 9600)` – Barmar Sep 15 '22 at 20:28
  • With the defines shown this line `void USART_init(long BAUD_RATE)` becomes `void USART_init(long 9600)` after preprocessing. You should choose a different name for the argument or remove it completely since you are not using it. – Retired Ninja Sep 15 '22 at 20:28
  • `BAUD_RATE` shouldn't be a macro if you want to use it as the function parameter. – Barmar Sep 15 '22 at 20:30

2 Answers2

1

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(){
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
1

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);
Barmar
  • 741,623
  • 53
  • 500
  • 612