Receiving string UART

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Good evening all. I'm trying to teach myself some serial communication via UART using pc terminal in ATMEL STUDIO and an ATMEGA 2560. so far i've been able to transmit and receive. and also transmitting a string. But now i would like to expand to receiving a string sent from the pc terminal, but i can't seem to figure out how to. If anyone here would be able to guide me in the right direction, it would be much appreciated.

 

/*
 * USART.c
 *
 * Created: 10/9/2019 8:59:47 PM
 * Author : Morten
 */ 

 

#include <stdio.h>
#include <avr/io.h>
#define fosc 16000000
#include <util/delay.h>
#include <avr/interrupt.h>
#define BAUD 19200
#define MYUBRR 103
//((fosc/8)/BAUD)-1

 

void USART_init(unsigned int ubrr)
{
    //full duplex
    UCSR0A=(1<<U2X0);
    //enable receiver and transmitter
    UCSR0B|=(1<<RXEN0)|(1<<TXEN0);
    //set frame format: 8 data bit og 1 stop bit
    UCSR0C|=(1<<UCSZ10)|(1<<UCSZ11);
    //set BAUD rate UBRR 103 for 19200.
    UBRR0H=(unsigned char)(ubrr>>8);
    UBRR0L=(unsigned char)ubrr;
}

void USART_Transmit(char tx)
{
    //venter på tom transmitter buffer
    while(!(UCSR0A &(1<<UDRE0)));
    //putter data ind i buffer og sender data
    UDR0=tx;
    
}

char USART_Receive(void)
{
    //venter på at data bliver modtaget
    while(!(UCSR0A & (1<<RXC0)));
    //hent og retuner modtaget data fra bufferen
    return UDR0;
    
}

//transmitter en string
void USART_Send_String(char *ptr)
{
    //kalder transmitfunktionen for hver byte i ptr
    while(*ptr !=0x00)
    {USART_Transmit(*ptr);
    ptr++;}
    
}

 

 

 

char ptr[]="Hej med dig!"; //string der skal sendes

int main(void)
{
           
        
    USART_init(MYUBRR);
    //USART_init(103);
    
    
    while (1) 
    {
    USART_Send_String(ptr);

    _delay_ms(5000);
    
        
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Mortenara

 

Mortenara wrote:
But now i would like to expand to receiving a string sent from the pc terminal, but i can't seem to figure out how to

 

I use HTerm (though it's a tad old, but it does the job very good, but there are others around, too, like putty) - and with that terminalprogramme you can can transmit messages from pc -> processor and from processor -> pc - means you enter values via keyboard and the processor /UART) will receive them. And when the proc sends strings to HTerm you'll see them.

 

When receiving values from HTerm you got to decide what to do with those - compare that received values with  stored values, light a led, send another string to the terminal

 

best regards

 

Hero_123

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To receive a string you have to agree on some "terminator" character between sender and receiver. Usually this would be the character '\n' that is guaranteed to be sent by the Return key (it may also send '\r' but you cna't be so sure about that. So the terminal/sender might send "Hello world\n" and you keep receiving individual characters ( USART_Receive ()) and put each individual character into a growing character array until that gets '\n' then you put 0x00 into the character array to mark its end and return the whole thing to the user.

 

It might be best to have the caller pass a pointer to a character buffer that they already have or that they create but you also need to pass some maximum length as you can't keep adding characters forever so if the size is exceeded you must just return what you have got so far.

 

Having said all that you could simply use FDEV_SETUP_STREAM then use stdio functions for getting input like scanf(), getchar() and gets(). If you read about gets():

 

https://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#gaf577dcba9afe50a9d068d0b69ac85d2f

 

and in turn the fgets() it mentions:

 

https://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html#ga00d34a8bff0293d2d6f4563d248d8fb2

 

you will see these are pretty much as I just described anyway (but written for you and available as standard in C).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

clawson wrote:
'\n' that is guaranteed (sic) to be sent by the Return key

Well, not guaranteed - but it is the most likely.

 

It could be just CR; eg:

 

and some people send LF first, followed by CR (usually just because they haven't realised that the convention is CR first).

 

But the key point is:

you have to agree on some "terminator" between sender and receiver

 

https://learn.sparkfun.com/tutorials/terminal-basics/basic-terminology-

 

Internally, in the 'C' programming language, strings are terminated by a NUL character - but one would not normally transmit that.

 

https://www.tutorialspoint.com/cprogramming/c_strings.htm

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Oct 28, 2019 - 10:06 AM