1
votes

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
}

and the test code in main:

 int main(void)
 {
 unsigned char c;

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

In 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. I cannot explain this. Proteus bug or logic error?

Thank you for any advise.

1
I don't see UART initialization. And I should noticed that UCSRA, UDR and other constants values in several AVR/ATMega controllers might be differ.xio4
I initialized the UART, just did not inlcude it here. Just included it in the code above.AKAP
Try c = 'A'; rather than sending a linefeed (10).chux - Reinstate Monica
That will work. But I also want to transmit something like FF. Here, again only F will be transmitted.AKAP

1 Answers

1
votes

UART data is sent only in ascii format...you have to convert the integer data to ascii format..use itoa() or do this one

int main(void)
 {
 unsigned char c;
 unsigned char b;

while(1)
{
    a = USART_Receive();
    c = 10;
    b=c;
    b=c/10;
    USART_Transmit(b+48);
    b=0;
    b=c%10;
    USART_Transmit(b+48);
    _delay(10000);
}
}