How to Simulate UART in SIMULIDE for ATMEGA328 in a ArduinoUNO

by nikkischulz6 in Circuits > Microcontrollers

391 Views, 0 Favorites, 0 Comments

How to Simulate UART in SIMULIDE for ATMEGA328 in a ArduinoUNO

Bildschirmfoto vom 2023-02-07 16-42-27.png

I wanted to simulate AVR UART

I thought I could use SIMULIDE

I only saw an example of ARDUINO

I thought... could I compile my AVR example to hex and then upload it on the ArduinoUNO?

Yes it works and I can se me my program running ...


Supplies

SIMULIDE

AVR Studio in VM (not needed just for further testing)

I use ubuntu

Write UART Code

Its just a simple example..

when the controller receives something else then 'a' it should send "beste" out of tx.

for example you send 'b' then it responds with "beste"

if you send 'a' it will not repond..

Goal for this code was to understand UART better and have a simplified Use case in my simulation.

Because when I send 'a' to rx of the AVR it should respond with "beste"

store it under "main.c" so the makefile can find it


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

#define F_CPU 4000000UL              // clock frequency of AVR in Hz
#define BAUD 9600                    // desired baud rate
#define MYUBRR F_CPU / 16 / BAUD - 1 // calculate baud rate register value

// @brief function to initialize UART. Also Check the Datasheet C Code example
//@ par ubrr uart baud rate register
void USART_init(unsigned int ubrr) {

  // 1.) Set the baud rate
  // Baudrate Registers: UBRRnH:UBRRnL
  // Baudrate : fosc / 16*(UBRR+1)
  // shift and convert to get the right Bits
  // high bits
  UBRR0H = (unsigned char)(ubrr >> 8);
  // low bits
  UBRR0L = (unsigned char)ubrr;

  // 2.)  Enable UART transmitter and receiver
  //  Configuration and Status Register
  UCSR0B = (1 << RXEN0) | (1 << TXEN0);
  // 3.) set frame format: 8 data bits, 1 stop bit
  UCSR0C = (1 << USBS0) | (3 << UCSZ00);
}

//@brief function to transmit data via UART
void USART_transmit(unsigned char data) {
  // 1.) Wait for empty transmit buffer
  // UDRE0 =  1 = UDR is empty
  // = 0 = UDR is still full
  while (!(UCSR0A & (1 << UDRE0)))
    ;
  // 2.) Put data into buffer and send
  UDR0 = data;
}

//@brief function to receive data via UART
unsigned char USART_receive(void) {
  // Wait for data to be received
  // Check RXC0
  // RXC0 = 1 = Data fully received
  // RXC0 = 0 = notfully received

  while (!(UCSR0A & (1 << RXC0)))
    ;
  // get and return received data from buffer
  return UDR0;
}
///

int main(void) {
  char data;

  USART_init(MYUBRR);
  while (1) {

    data = USART_receive();
    if(data == 'a'){
    }else{ USART_transmit('b');
    USART_transmit('e');
    USART_transmit('s');
    USART_transmit('t');
    USART_transmit('e');}
   
  }
  return 0;
}



Convert It to Hex File

use this makefile:

store a file called "makefile" with the following content.

then run the console and write make

it should convert the main.c to a main.hex


PORT=/dev/ttyUSB1

MCU=atmega328p

CFLAGS=-g -Wall -mcall-prologues -mmcu=$(MCU) -Os

LDFLAGS=-Wl,-gc-sections -Wl,-relax

CC=avr-gcc

TARGET=main

OBJECT_FILES=main.o


all: $(TARGET).hex


clean:

   rm -f *.o *.hex *.obj *.hex


%.hex: %.obj

   avr-objcopy -R .eeprom -O ihex $< $@


%.obj: $(OBJECT_FILES)

   $(CC) $(CFLAGS) $(OBJECT_FILES) $(LDFLAGS) -o $@


program: $(TARGET).hex

   avrdude -p $(MCU) -c stk500 -P $(PORT) -U flash:w:$(TARGET).hex

Load the Main.hex Into the Arduino of SIMULIDE

Bildschirmfoto vom 2023-02-07 16-49-56.png
Bildschirmfoto vom 2023-02-07 16-51-35.png
Bildschirmfoto vom 2023-02-07 16-53-02.png

open SimulIDE and goto examples -> arduino->arduino_serial_echo

then double click on the *simu -file so the simulation opens.

now you have to make a right click on the arduino to program it with your main.hex:

just browse to the path you have stored it and click it..

then it will work

you can then write something into the left WHITE box of the serial monitor

you will see on the right BLUE site that it was sent to the microcontroller.

the controller should answer on the left side .

if you type in 'a' it will respond nothing

if you type in something else it will respond "beste"