Skip to main content
Tweeted twitter.com/StackArduino/status/874655409008726016
Fixed spelling and grammar
Source Link
Greenonline
  • 3.2k
  • 7
  • 37
  • 49

Can I code default serial port in Arduino Mega (USART0, I assume) in pure C.? I mean to set up it and cofigureconfigure directly by ATmega's registers?

I'm asking beacausebecause I'm trying to do it and I have a problem. Below is code which I use and arduinoArduino doesn't transfer anything....

#include <avr/io.h>

#define F_CPU 16000000
#define BAUD_RATE 9600 
#define MY_UBRR (F_CPU+BAUD_RATE*8UL)/(16UL*BAUD_RATE)-1


void setup() {

    UART_Init(MY_UBRR);
}

void loop() {
    UART_Transmit('a');

 }

void UART_Init(unsigned char ubrr)
{
   UBRR0H = (unsigned char)(ubrr >> 8);
   UBRR0L = (unsigned char)ubrr;
   UCSR0B = (1<<RXEN0) | (1<<TXEN0);
   UCSR0C = (1<<USBS0) | (1<<UCSZ00);
}

 void UART_Transmit(unsigned char data)
 {
    while(!(UCSR0A & (1<<UDRE0)))
    UDR0 = data;
 }

That code I created following the datasheet for ATmega 2560. It has been compilatedcompiled and programmed but doesn't work at all....

Please give me any adviseadvice. It's important for me use just C code in that case. Thank you in advance

Can I code default serial port in Arduino Mega (USART0, I assume) in pure C. I mean to set up it and cofigure directly by ATmega's registers?

I'm asking beacause I'm trying to do it and I have a problem. Below is code which I use and arduino doesn't transfer anything....

#include <avr/io.h>

#define F_CPU 16000000
#define BAUD_RATE 9600 
#define MY_UBRR (F_CPU+BAUD_RATE*8UL)/(16UL*BAUD_RATE)-1


void setup() {

    UART_Init(MY_UBRR);
}

void loop() {
    UART_Transmit('a');

 }

void UART_Init(unsigned char ubrr)
{
   UBRR0H = (unsigned char)(ubrr >> 8);
   UBRR0L = (unsigned char)ubrr;
   UCSR0B = (1<<RXEN0) | (1<<TXEN0);
   UCSR0C = (1<<USBS0) | (1<<UCSZ00);
}

 void UART_Transmit(unsigned char data)
 {
    while(!(UCSR0A & (1<<UDRE0)))
    UDR0 = data;
 }

That code I created following the datasheet for ATmega 2560. It has been compilated and programmed but doesn't work at all....

Please give me any advise. It's important for me use just C code in that case. Thank you in advance

Can I code default serial port in Arduino Mega (USART0, I assume) in pure C? I mean to set up it and configure directly by ATmega's registers?

I'm asking because I'm trying to do it and I have a problem. Below is code which I use and Arduino doesn't transfer anything...

#include <avr/io.h>

#define F_CPU 16000000
#define BAUD_RATE 9600 
#define MY_UBRR (F_CPU+BAUD_RATE*8UL)/(16UL*BAUD_RATE)-1


void setup() {

    UART_Init(MY_UBRR);
}

void loop() {
    UART_Transmit('a');

 }

void UART_Init(unsigned char ubrr)
{
   UBRR0H = (unsigned char)(ubrr >> 8);
   UBRR0L = (unsigned char)ubrr;
   UCSR0B = (1<<RXEN0) | (1<<TXEN0);
   UCSR0C = (1<<USBS0) | (1<<UCSZ00);
}

 void UART_Transmit(unsigned char data)
 {
    while(!(UCSR0A & (1<<UDRE0)))
    UDR0 = data;
 }

That code I created following the datasheet for ATmega 2560. It has been compiled and programmed but doesn't work at all...

Please give me any advice. It's important for me use just C code in that case.

Source Link
M_K
  • 31
  • 1

Problem with using pure C code to program default UART (UART0) in Arduino Mega

Can I code default serial port in Arduino Mega (USART0, I assume) in pure C. I mean to set up it and cofigure directly by ATmega's registers?

I'm asking beacause I'm trying to do it and I have a problem. Below is code which I use and arduino doesn't transfer anything....

#include <avr/io.h>

#define F_CPU 16000000
#define BAUD_RATE 9600 
#define MY_UBRR (F_CPU+BAUD_RATE*8UL)/(16UL*BAUD_RATE)-1


void setup() {

    UART_Init(MY_UBRR);
}

void loop() {
    UART_Transmit('a');

 }

void UART_Init(unsigned char ubrr)
{
   UBRR0H = (unsigned char)(ubrr >> 8);
   UBRR0L = (unsigned char)ubrr;
   UCSR0B = (1<<RXEN0) | (1<<TXEN0);
   UCSR0C = (1<<USBS0) | (1<<UCSZ00);
}

 void UART_Transmit(unsigned char data)
 {
    while(!(UCSR0A & (1<<UDRE0)))
    UDR0 = data;
 }

That code I created following the datasheet for ATmega 2560. It has been compilated and programmed but doesn't work at all....

Please give me any advise. It's important for me use just C code in that case. Thank you in advance