Usart communication. text to OLED from TERMINAL

Go To Last Post
3 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...