0

To get right output I had to set U2X bit to one and when I set it to zero and change value of UBRR Register the output makes no sense. I checked that I'm using right values from datasheet table but it doesn't give right output at U2X=0.

my .h file

#ifndef USRAT_TEST_H_
#define USRAT_TEST_H_

#include <avr/io.h>
void USRAT_INIT(uint16_t bd);
char USRAT_RECIEVE_CHR(void);
void USRAT_SEND_CHR(char c);
#endif /* USRAT_TEST_H_*/

my .c file

#include <avr/io.h>
#define F_CPU 2000000UL
#include <util/delay.h>
#include "USRAT_TEST.h"
void USRAT_INIT(uint16_t bd)
{
    UCSRA = (1<<U2X);//when removing this line and edit F_CPU/8 to F_CPU/16 things go wrong
    UCSRB|=(1<<RXEN)|(1<<TXEN);// enable send and receive
    UCSRC|=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);//8-bit width 
    UBRRL=(F_CPU/8/bd-1);
    UBRRH=(F_CPU/8/bd-1)>>8;
    }
char USRAT_RECIEVE_CHR()
{
    while((UCSRA&(1<<RXC))==0)
    {

    }
    return UDR;
}
void USRAT_SEND_CHR(char c)
{
while((UCSRA&(1<<UDRE))==0)
{

}
 UDR=c;


}

main.c file

    #include <avr/io.h>
#define F_CPU 2000000UL
#include <util/delay.h>
#include "USRAT_TEST.h"
#include <stdio.h>
char data;
int main(void)
{
        USRAT_INIT(9600);
    /* Replace with your application code */
    while (1) 
    {
        data = USRAT_RECIEVE_CHR(); 
        USRAT_SEND_CHR(data); 
                    }
}

It's a simple simulation I added 2 virtual terminals to my AVR ....when setting U2X to 0 and writing values from datasheet when I write "a" or any character to one of them I get in the other one wrong charterer.

I took values of UBBR right from data sheet you can try without formula F_CPU/8/bd-1 and put them from table directly.

Can you tell me what is U2X effect on same BAUD RATE ???? OR When to set U2X to one or Zero??????

enter image description here

Hussien Mostafa
  • 159
  • 2
  • 18
  • 5
    Sorry, my crystal orb is inactive now. Can you provide the information about what the frequency of CPU, what UBRR you set, which baud rate you want, what output do you expect and, output do you have and why you think it is not working? I can say U2X works exactly as it described in the datasheet. – AterLux Feb 20 '18 at 11:53
  • 2
    Post the code that results in incorrect result. Describe the result you expected, and the result you obtained. Then your question may be answerable - Occam's Razor suggests that the error is yours and not the silcon, but you have not shown us the error - you have simply said _"I have done everything right and it does not work"_ which is of course nonsense. You need to show us what you are doing wrong rather then assert that you are doing nothing wrong. – Clifford Feb 20 '18 at 13:05
  • 1
    what does your scope show as the baud rate produced for the settings you are using (and of course post your code as requested). – old_timer Feb 20 '18 at 15:07
  • I'm sorry for not posting code . I have edited the post and I wanted to put a photo of my simulation put the file was deleted , I'll make it again and put it ASAP. Thanks vary much .I hope you can help me.... – Hussien Mostafa Feb 20 '18 at 18:34
  • I put values from datasheet put it doesn't work in normal mode @AterLux i get wrong characters "a">>"k" , "~"or any thing else.......and can you tell me what is U2X effect on same BAUD RATE ? – Hussien Mostafa Feb 20 '18 at 18:39
  • OR When to set U2X to one or Zero?? – Hussien Mostafa Feb 20 '18 at 18:42
  • I posted code a day ago any ideas....? – Hussien Mostafa Feb 22 '18 at 00:42

1 Answers1

1

Look again on examples in datasheet and see how UBRR relates to baud rate and clock. You will see something like #define MYUBRR FOSC/16/BAUD-1. Your code calculates UBRR dividing clock by 8 instead of 16, so to make UART working right you have to double the clock.

andy
  • 757
  • 5
  • 13