How to send data serially from atmega328p to pc using serial communication?

648 Views Asked by At

i'm trying to send data continuously from atmega328 to pc using mx232 and i'm using tera term to display the data coming from com5. The problem here is that the sent data is always the same character regardless of which character i send from my atmega. the code is below and attached a photo of the output in tera term

#define F_CPU 8000000UL
#define BAUD_RATE 9600
#define RBC 51
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

void define_inputs_and_outputs(){
    DDRC |= (DDC5);
    DDRD |= (1<<DDD1);
}

void init_serial_transmit(){
    UCSR0B = (1<<TXEN0);
    UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
    //set baud rate
    UBRR0H = 0;
    UBRR0L = RBC;
    
}

void serialPutString(char* string, unsigned int size){
    for (int i=0; i<size; i++)
    {
        while(!(UCSR0A & (1<<UDRE0)));
        UDR0 = (int) string[i];
    }
    
}

void stop_serial(){
    UCSR0A = 0;
    UCSR0B = 0;
    UCSR0C = 0;
    UDR0 = 0;
}

void init_serial_receive(){
    return;
}

void blink(){
    PORTC |= (1<<PORTC5);
    _delay_ms(300);
    PORTC &= ~(1<<PORTC5);
    
}


int main(void)
{
    /* Replace with your application code */
    define_inputs_and_outputs();
    init_serial_transmit();
    //_delay_ms(10000);
    
    while (1){
        UDR0 = 8;
        serialPutString("moha",4);
        blink();
    }
}

enter image description here

0

There are 0 best solutions below