EmbDev.net

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


Author: Alex Kapphahn (Company: ---) (akap)
Posted on:

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

[c]while(1) 
{ 
    a = USART_Receive(); 
    c = 10; 
    USART_Transmit(c); 
    _delay(10000); 
} 
} 

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.

Author: c-hater (Guest)
Posted on:

Rate this post
1 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]

>
#define MYBRR FOSC/16/BAUD-1 

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

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

Much better is it with implicite rounding:

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


>
UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); 
>
//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.


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

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...

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.