Usart communication. text to OLED from TERMINAL

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

Hey. I´m a student at a technical university, and we have been giving an assignment to write data from the PC terminal window in the ATMEL STUDIO program out on an OLED display. 

So far i've made the code that is linked below. The function "void putsUSART1(char *ptr)" was given by out teacher, and it is by my understanding the part that should take the get the data from the TERMINAL. I'm just not understanding it completely and was hoping that someone in here could be of assistance. 

The part of the code that is sending the string to the display is just to check that the display was able to receive data. 

 

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

#include <stdio.h>
#include <avr/io.h>
#define F_CPU 16000000UL
#define BAUD 19200
#define MYUBRR F_CPU/8/BAUD-1
#include <util/delay.h>
#include "I2C.h"
#include "ssd1306.h"

void USART_init();
void USART_Transmit(char tx);
char USART_Receive();
void putsUSART1();

void setup()
{
    // Initialze SSD1306 OLED display
    reset_display();              // Clear screen
    setXY(0,0);              // Set cursor position, start of line 0
    sendStr("ACROBOTIC");
    setXY(1,0);              // Set cursor position, start of line 1
    sendStr("industries");   //write a text message - a string
    setXY(2,0);              // Set cursor position, start of line 2
    sendStr("Pasadena,");    //write a text message - a string
    setXY(2,10);             // Set cursor position, line 2 10th character
    sendStr("C222A");            //write a text message - a string
    setXY(3,10);             // Set cursor position, line 2 10th character
    sendStr("TEST");
    
}

int main(void)
{
        I2C_Init();
        InitializeDisplay();
        print_fonts(); _delay_ms(500);
        clear_display();
        setup(), _delay_ms(10);
    
    
        
    USART_init(MYUBRR);
    /* Replace with your application code */
    while (1) 
    {
            
    USART_Transmit(USART_Receive());
    _delay_ms(1000);
    
    
    //send text
    //send tal
    char str[20]={0};
    //send bogstav
    char gog[20]={65};
    //send tal
    sprintf(str,"%u",0b00001111);
    setXY(5,5);
    sendStr(str);
    //send bogstav
    setXY(6,6);
    sendStr(gog);
    

    
    
    }
}

void USART_init(unsigned int ubrr)
{
    //full duplex
    UCSR0A=(1<<U2X0);
    //eneable 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;
}

void putsUSART1(char *ptr)
{
    //kalder transmitfunktionen for hver byte i ptr
    while(*ptr)
    {USART_Transmit(*ptr);
    ptr++;}
}
 

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

Mortenara wrote:
and it is by my understanding the part that should take the get the data from the TERMINAL
That does not sound right. A function with "puts" (put string) in the name usually means "take this string and send it OUT" - it's not an input function.

 

To get things from the PC and into the AVR it's far more likely you will be using USART_Receive()

 

BTW putsUSART1() is a misnomer, it does not use USART1, it uses USART0. I'd try to avoid confusion like this if I were you.

 

Also don't do things like this:

        print_fonts(); _delay_ms(500);

New lines cost nothing in C. They make the code easier to read and follow. Normally the ';' at the end of a line would always be followed by '\n'...

        print_fonts();
        _delay_ms(500);

Even worse is:

      setup(), _delay_ms(10);

Technically the comma here will be accepted by the C compiler but just don't do this. make it:

      setup();
      _delay_ms(10);

 

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

Mortenara wrote:
The function "void putsUSART1(char *ptr)" was given by out teacher

Then you really should be talking to your teacher about it!

 

Mortenara wrote:
it is by my understanding the part that should take the get the data from the TERMINAL

No.

 

The "put" in the name tells you that it will put a string to the UART

 

You can also see this from the fact that it calls  USART_Transmit() - which, as the name suggests, is going to transmit not receive!

 

Please see Tip #1 for how to properly post source code.

 

 

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can you please share the code that you ended up with? I assume this project is complete. I'm trying to work on the same thing as part of my master's project.

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

Go on - a Master's student should be able to knock this up in moments!

 

All you need is

  1. code to receive text on a UART
  2. code to write  text to the OLED
  3. a bit of glue to tie them together

 

which bit are you stuck with?

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: Wed. Nov 20, 2019 - 06:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
a Master's student should be able to knock this up in moments!

IT depends on the kind of protection in place.....wink

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

Last Edited: Wed. Nov 20, 2019 - 07:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My c file for the UART endded up like this. Got everything to work perfectly.

/*
 * UART.c
 *
 * Created: 10/24/2019 12:27:01 PM
 *  Author: Morten
 */ 

#include <stdio.h>
#include <avr/io.h>

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

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

//modtag en karakter
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++;}    
}

//receiver en string
void USART_Modtag_String(char *ptr)
{
    char cx;
    char i=0;
    char BS=0x08;//back space
    char space=0x20;//space

    while((cx=USART_Receive())!=(('\n'))) //0x0D er carrige return, som retunerer tilbage til starten af stringen - while((cx=USART_Receive())!=(0x0D && 0x0a))
    //newline fortæller hvor man er ved enden af en string og starten af en ny en. oftes laves denne med enter tasten
    {
    *ptr=cx;
    //USART_transmit(cx)
    if(cx==BS)
    {
    USART_Transmit(space);
    USART_Transmit(BS); ptr--;
    }
    else {ptr++;
    i++;}
    }*ptr=0;
}

 

Last Edited: Wed. Nov 20, 2019 - 09:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Again,  Please see Tip #1 for how to properly post source code.

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...