EmbDev.net

Forum: µC & Digital Electronics AVR ATmega8, cannot transmit multiple characters via UART, data getting lost


von Alex K. (Company: ---) (akap)


Rate this post
useful
not useful
Hi,

I think I have a very simple problem, but as a beginner I cannot find a 
solution.

I am writing a code for a MCU until that will transmit data via UART 
(RS232). [ATmega8A]

Currently, I am testing my code in Proteus:
1
#include <avr/io.h>
1
#include <avr/interrupt.h>
1
#include <util/delay.h>
1
#include <stdlib.h>
1
#include <compat/twi.h>
1
#define FOSC 16000000     //Clock Speed
1
#define BAUD 9600         //Baud rate set to 9600
1
#define MYBRR FOSC/16/BAUD-1
1
void USART_Init (void)
1
{
1
UBRRH = (MYBRR >> 8);        //Code for setting
1
UBRRL = MYBRR;               //the baud rate
1
UCSRB = (1 << RXEN) | (1 << TXEN); //Enable receiver and transmitter
1
UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
1
//Frame format setting: 8 Data, 2 Stop bit
1
}
1
/* USART transmit function */
1
void USART_Transmit(unsigned char data)
1
{
1
  while(!(UCSRA & (1 << UDRE)));//Wait until transmit buffer is empty
1
     UDR = data;
1
}
1
/* USART receive function */
1
unsigned char USART_Receive(void)
1
{
1
     while(!(UCSRA & (1 << RXC))); //Wait until data is received
1
     return UDR;   //Get the data from buffer and return it
1
}
1
 int main(void)
2
[c] {
3
[c] unsigned char c;
4
5
[c]while(1)
1
{
1
    a = USART_Receive();
1
    c = 10;
1
    USART_Transmit(c);
1
    _delay(10000);
1
}
1
}

n Proteus, the virtual terminal displays 0. However, I am expecting to 
see 10. This is just one example, in general only the last 
digit/character is getting displayed on the virtual terminal.
Same happens if I want to transmit say AA, only A will be displayed.
I cannot explain this. Proteus bug or logic error?

Thank you for any advise.

von c-hater (Guest)


Rate this post
useful
not useful
Alex Kapphahn wrote:

> Currently, I am testing my code in Proteus:

What, the f**k is Proteus? For what is it good for?


> I am writing a code for a MCU until that will transmit data via UART
> (RS232). [ATmega8A]

>
1
#define MYBRR FOSC/16/BAUD-1

Stupid, but may work. You should clarify your intention using braces.

>
1
#define MYBRR (FOSC/16)/BAUD-1

Much better is it with implicite rounding:

>
1
#define MYBRR (FOSC/16+BAUD/2)/BAUD-1


>
1
UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);
>
1
//Frame format setting: 8 Data, 2 Stop bit

Your code partially doesn't match the provided comment. Matches for 
number of data bits, but doesn't match for number of stop bits.


>
1
/* USART receive function */
>
1
unsigned char USART_Receive(void)
>
1
{
>
1
     while(!(UCSRA & (1 << RXC))); //Wait until data is received
>
>
1
     return UDR;   //Get the data from buffer and return it
>
1
}

Poor implementation. It doesn't care the possebility of transmission 
errors.


> n Proteus, the virtual terminal displays 0. However, I am expecting to
> see 10.

You shouldn't. If you send a char with value 10, you will receive a char 
with value 10 (in the case there is no transmission error and sender and 
receiver uses same frame format).

You can expect to get a visible "10" in the only case, the receiving 
terminal replaces received, but unprintable characters with decimal 
strings, describing their values.

> Proteus bug or logic error?

Probably your error. Either you made a mistake in your code or you don't 
understand what "Proteus" does. Whatever "Proteus" is...

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.