Answer the question
In order to leave comments, you need to log in
Arduino Uno, UART speed issue?
Hello!
Programming an arduino in C. I used initialization and forwarding according to the atmega328 manual. When viewing in the terminal on the settings specified in the code, incorrect data is displayed, but if you set not 9600, but 19200 in the terminal, then the characters are displayed normally. If you use the standard Arduino IDE examples, then everything works correctly.
Firmware
#include <avr/io.h>
#define FOSC 16000000 // Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
void USART_Init( unsigned int ubrr)
{
/*Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/*Enable receiver and transmitter */
UCSR0B = 1<<TXEN0;
/* Set frame format: 8data, 1stop bit */
UCSR0C = 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 main( void )
{
USART_Init(MYUBRR);
while(1){
USART_Transmit('b');
}
}
#Контроллер, установленный на плате. Может быть другим, например atmega328
DEVICE = atmega328p
#Тактовая частота 16 МГц
CLOCK = 16000000
#Команда запуска avrdude. Ее нужно скопировать из Arduino IDE.
AVRDUDE = C:\Users\User\Desktop\arduino-1.0.4\hardware/tools/avr/bin/avrdude -CC:\Users\User\Desktop\arduino-1.0.4\hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega328p -carduino -P\\.\COM3 -b115200 -D
OBJECTS = main.o
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE)
all: main.hex
.c.o:
$(COMPILE) -c $< -o [email protected]
.S.o:
$(COMPILE) -x assembler-with-cpp -c $< -o [email protected]
.c.s:
$(COMPILE) -S $< -o [email protected]
flash: all
$(AVRDUDE) -U flash:w:main.hex:i
clean:
rm -f main.hex main.elf $(OBJECTS)
main.elf: $(OBJECTS)
$(COMPILE) -o main.elf $(OBJECTS)
main.hex: main.elf
rm -f main.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size --format=avr --mcu=$(DEVICE) main.elf
Answer the question
In order to leave comments, you need to log in
The problem turned out to be in the initial initialization, there is garbage in the memory of the UCSR0A-C registers, but I assumed that there were zeros.
Fixed the code and everything worked
#include <avr/io.h>
#define FOSC 16000000 // Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1
void USART_Init( unsigned int ubrr)
{
/*Set baud rate */
UCSR0A = 0;
UCSR0B = 0;
UCSR0C = 0;
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/*Enable receiver and transmitter */
UCSR0B = 1<<TXEN0;
/* Set frame format: 8data, 1stop bit */
UCSR0C = 3<<UCSZ00;
}
void USART_Transmit( char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) )
;
/* Put data into buffer, sends the data */
UDR0 = data;
}
void main( void )
{
USART_Init(MYUBRR);
while(1){
USART_Transmit('a');
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question