MCU: Atmega2560 IDE: Microchip Studio v7.0.2594 Toolchain: WinAVR (also tried with Native)
I am trying communicate with my MCU over UART. I have written two very basic functions over UART to transmit bytes and strings and observing the output on Serial terminal. The project compiles successfully but the string function doesn't work. Someone please help!!
UART_TxString(MOD_UART1,"\nHello"); //This causes MCU to transmit all 0s over the UART port
UART_TxByte(MOD_UART1,'\n'); //This works fine
UART_TxByte(MOD_UART1,'H'); //This works fine
UART_TxByte(MOD_UART1,'e'); //This works fine
UART_TxByte(MOD_UART1,'y'); //This works fine
//Function definitions
void UART_TxByte(sys_module module, uint8_t byte)
{
switch(module)
{
case MOD_UART0:
{
while (( UCSR0A & (1<<UDRE0)) == 0) ; // Do nothing until UDR is ready
UDR0 = byte;
break;
}
case MOD_UART1:
{
while (( UCSR1A & (1<<UDRE1)) == 0) ; // Do nothing until UDR is ready
UDR1 = byte;
break;
}
default:
break;
}
}
void UART_TxString(sys_module module, char* string)
{
while(*string)
{
UART_TxByte(module, *string);
string++;
}
}
I have worked with Atmega series many times, but this has never happened before. Passing strings to functions (UART/LCD/Wireless Modules) have always worked.
I have also tried to make a global array and put the string contents into it and then pass it to the string function, still it caused same issue.
Update: I figured out a work around for now as given below,
void UART_TxString(sys_module module, char string[])
{
uint16_t i=0;
for(i=0 ; string[i]!=0 ; i++)
{
UART_TxByte(module, string[i]);
}
}
However, with above workaround, I need to load string into the array using sprintf. But if I do this:
char string[] = "hello\r"
UART_TxString(MOD_UART1, string); // It doesn't work...
Still not able to find out why while(*string)
did not work.
UART_TxString()vs the map file should give you the sram vs ram information.char *xhas the same semantic likechar []as a function argument btw. fwiw you would rather useconst char *x. Maybe you ran out of RAM/stack. WinAVR is v4.3 or older, maybe it's a bug. Cross check the generated assembly.uint8_tforUART_TxBytefunction but put in acharat theUART_TxStringfunction. Normaly there should be no problem because they are both of the same size (8 bits). This it the only problem i see with the code.