You need to log in to create posts and topics.
AVR Atmega128 sprintf sscanf not working
Ulkizz@ulkizz
4 Posts
#1 · January 16, 2025, 7:31 am
Quote from Ulkizz on January 16, 2025, 7:31 amAs states above, function sprintf (and scanf) works incorrect. Functions seems to work except for copying to destination buffer. The latter only an assumption.
Hex compiled in Microchip Studio 7 with avr-gcc.exe (AVR_8_bit_GNU_Toolchain_3.6.2_1778) 5.4.0
Target device ATMega128A
SimulIDE-1.1.0-SR1. Also true for RC1
The functions works correct on real device.
Sample code:
#define F_CPU 7372800L #include <avr/io.h> #include <stdio.h> #include "util/delay.h" void USART_INIT(unsigned int chNo, unsigned int Baud) { unsigned int ubrr = F_CPU/16/Baud-1; UBRR0H = (unsigned char)(ubrr>>8); UBRR0L = (unsigned char)ubrr; UCSR0B = (1<<RXEN0)|(1<<TXEN0); UCSR0C = (1<<USBS0)|(3<<UCSZ00); } void USART_Transmit( unsigned char data ) { /* Wait for empty transmit buffer */ while ( !( UCSR0A & (1<<UDRE0)) ) ; /* Put data into buffer, sends the data */ UDR0 = data; } void USART_Transmit( unsigned char data[] ) { int i = 0; while(data[i]!='\0') { USART_Transmit(data[i]); i++; } } void USART_Transmit( const char data[] ) { USART_Transmit((unsigned char*)data); } int main(void) { USART_INIT(0,19200); char buffer[10]; /* Replace with your application code */ while (1) { _delay_ms(1000); sprintf(buffer, "\r\nTest %d", 10); USART_Transmit(buffer); USART_Transmit('.'); } }
As states above, function sprintf (and scanf) works incorrect. Functions seems to work except for copying to destination buffer. The latter only an assumption.
Hex compiled in Microchip Studio 7 with avr-gcc.exe (AVR_8_bit_GNU_Toolchain_3.6.2_1778) 5.4.0
Target device ATMega128A
SimulIDE-1.1.0-SR1. Also true for RC1
The functions works correct on real device.
Sample code:
#define F_CPU 7372800L
#include <avr/io.h>
#include <stdio.h>
#include "util/delay.h"
void USART_INIT(unsigned int chNo, unsigned int Baud)
{
unsigned int ubrr = F_CPU/16/Baud-1;
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}
void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}
void USART_Transmit( unsigned char data[] )
{
int i = 0;
while(data[i]!='\0')
{
USART_Transmit(data[i]);
i++;
}
}
void USART_Transmit( const char data[] )
{
USART_Transmit((unsigned char*)data);
}
int main(void)
{
USART_INIT(0,19200);
char buffer[10];
/* Replace with your application code */
while (1)
{
_delay_ms(1000);
sprintf(buffer, "\r\nTest %d", 10);
USART_Transmit(buffer);
USART_Transmit('.');
}
}
Click for thumbs down.0Click for thumbs up.0
Last edited on January 16, 2025, 8:13 am by Ulkizz
arcachofo@arcachofo
665 Posts