I am using ATmega8 and I am trying to send string over USART (in printf
style) which include a variable .I am using Atmel Studio 6.2
as IDE for AVR programming. Here is my code:-
#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
void USARTInit(uint16_t ubrr_value) // initialize USART
{
UBRRL = ubrr_value;
UBRRH = (ubrr_value>>8);
UCSRC|=(1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0);
UCSRB=(1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
}
void USARTWriteChar(char data) // send character using USART
{
while(!(UCSRA & (1<<UDRE)));
UDR=data;
}
void send_string(char s[]) // send string using USART
{
int i =0;
while (s[i] != 0x00)
{
USARTWriteChar(s[i]);
i++;
}
USARTWriteChar('\n');
}
int main(void)
{
USARTInit(51);
char val='A';
while(1)
{
send_string("Value = %c",val);
}
}
Now when I compile my code I got this error:-
too many arguments to function 'send_string`
So, clearly it is not accepting %c
as it does in C programming. Is there a way in embedded C
to pass a variable in a string?
too many arguments to function
send_string
The message is quite clear there, your send_string()
accepts only a pointer to char
as input argument, but while calling, you're trying to pass two input arguments "Value = %c",val
causing the mis-match.
You cannot use format specifiers here the way you have shown.
In general, the way to go is to use a temporary buffer, use snprintf()
to generate the input string and then, pass the buffer to the send_string()
call.
That said, since you're only interested in passing the value of c
to send_string()
, you can reduce the function to take only a char
, like
void send_string(char s) { ...
and then, pass the constant (pre-defined) string Value =
and then, the input argument go get the same effect.