1

Hi i'm new to this and i need help. It's suppose to just show the 'S' in the realterm instead it gives 'null'. What would be the problem? could it be the register? or the code itself?

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

void UART_Init(unsigned int ubrr)
{
    UBRRH=(unsigned int)(ubrr>>8);
    UBRRL=(unsigned int)ubrr;
    UCSRA=0x00;
    UCSRB=(1<<TXEN)|(1<<RXEN);
    UCSRC=(0<<USBS)|(1<<UCSZ0)|(1<<UCSZ1);
}

void UART_Tx(unsigned char chr)
{
    while (bit_is_clear(UCSRA,UDRE)){}
    UDR=chr;
}

int main(void)
{
    UART_Init(95);
    DDRD|=0B11111111;
    PORTD|=0B11111111;

    while(1){
        _delay_ms(10);
        UART_Tx('S');
    }
}

System is running on xtal with 14745600 Hz. Speed on host is 9600 baud. all settings should be 8N1.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2211932
  • 25
  • 1
  • 5

2 Answers2

2

You need to set the URSEL when writing to the UCSRC register.

Change

UCSRC=(0<<USBS)|(1<<UCSZ0)|(1<<UCSZ1);

to

UCSRC=(1<<URSEL)|(0<<USBS)|(1<<UCSZ0)|(1<<UCSZ1);

From the data sheet:

The UBRRH Register shares the same I/O location as the UCSRC Register. Therefore some special consideration must be taken when accessing this I/O location. When doing a write access of this I/O location, the high bit of the value written, the USART Register Select (URSEL) bit, controlswhich one of the two registers that will be written. If URSEL is zero during a write operation, the UBRRH value will be updated. If URSEL is one, the UCSRC setting will be updated.

The rest of the code looks fine to me.

Rev
  • 5,827
  • 4
  • 27
  • 51
0

change UART_Tx('S'); using UART_Tx("S");