USART without library-How To send to termianl value of register

Hello,
I have prepared code to send data via USART on ArduinoMega2560 without external library. I don’t want to use BuiltIIn Serial.print().
My Code is:

#define FOSC 16000000 // Clock Speed = 16MHz
#define BAUD 19200
#define USART_TX_BUFFER_SIZE 128
#define MYUBRR FOSC/16/BAUD-1
#include <Arduino.h>
void USART_init (unsigned int ubrr);
void USART_Transmit(char data); 

void setup() {

    USART_init(MYUBRR);
    DDRB |= (1<<7);//sets PB7 as GPIO output
   _delay_ms(500);
}

//=============================================================================
void loop() {
     for ( int i=0; i<255;++i)
     { 
     USART_Transmit(i); 
     _delay_ms(500);
     PORTB ^= (1<<7);
     long a=10;
     char buf[10];
     ltoa(a,buf,BIN);
     USART_Transmit(10);
     USART_Transmit(buf);
     _delay_ms(500);
     PORTB ^= (1<<7);
     }
}
//=============================================================================
void USART_init (unsigned int ubrr)
{
 UBRR0H = (unsigned char)(ubrr>>8);/*Set baud rate (Basically just setting the constant Value determined by MYUBRR in the UBRR0H&L Register */
    UBRR0L = (unsigned char)ubrr;
    UCSR0B = (1<<TXEN0) ;//| (1<<TXCIE0);     /* Enable receiver, transmitter and transmit buffer interrupt */
    UCSR0C = 0B00000000;
    UCSR0C = (!(1<<USBS0))|(1<<UCSZ01)|(1<<UCSZ00);
}
void USART_Transmit(char data) 
{ 
   while (!( UCSR0A & (1<<UDRE0))); //Bit 5 – UDREn: USART Data Register Empty
   UDR0 = data;  
}

1. I have Warning:
src\main.cpp: In function ‘void loop()’:
src\main.cpp:28:24: warning: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
USART_Transmit(buf);
^
src\main.cpp:9:6: note: initializing argument 1 of ‘void USART_Transmit(char)’
void USART_Transmit(char data);

WHY?

2. On Terminal there is only proper “char”, but I can not display Binary Value or Register Value, f.e. for now I try to dsiplay DEC 10 in BIn. In future, I want to display Register Value, f.e. for USCR0C-> 00011000.
111

How to send value to terminal?

The compiler exactly tells you why he’s giving you a warning. Read it more carefully.

 src\main.cpp:28:24: warning: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
USART_Transmit(buf);

your buf is a char buf[10];, so an array of 10 chars (and also a pointer). Your void USART_Transmit(char data) needs a single char data argument – not a buffer. That’s where your error comes from.

Either write a new function like

void USART_TransmitString(const char* str) {
    for(unsigned i=0; i < strlen(str); i++) {
       USART_Transmit(str[i]);
   }
}

and use it or do that loop in your main program loop.

2 Likes

As you can see, I got stuck In Bascom few years ago… Now I try to start one more time with AVR, but on Arduino Board and C…

Addendum on something I’ve overlooked: You

You’re also attempting to transmit all the numbers from 0 to 254 inclusive via UART – that will generate some non-ASCII-readable garbage output for most numbers. If you mean to transmit e.g. 100 as the string "100" you need an integer-to-string conversion, aka itoa(), similiar to the ltoa you are already using.