-1

can anyone show some example code or link that help me to solve this problem. I want to receive and transmit a string using a terminal.

till now, I only got this -->

    void usart_init(void)
    {

        UBRRH = 0x00;
        UBRRL = 95;
        UCSRB =  (1 << RXEN) | (1 << TXEN) | **(1<<RXCIE)**; 
        UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0)|(1 << UCSZ1); 
    }

    ISR (USART_RXC_vect)
    {
    PORTD= 0b00000100;  //enable RTS
    char ReceivedByte ;
    ReceivedByte = UDR ; // Fetch the received byte value into the variable " ByteReceived "
    UDR = ReceivedByte ; // Echo back the received byte back to the computer
    PORTD= 0b00000000;  //disable RTS

    }
int main (void)
{
   usart_init();
   while(1)
    {
      sei();
    }
   return 0;
}

but this only can transmit and receive a character. How to modified this code to enable it to send and transmit a string.

Thank for the help :)

OooO
  • 13
  • 1
  • 8
  • UART is designed to transmit data character by character. You can have a wrapper function to send string wise but still it would send in chars internally. – Sagar Sakre Jun 20 '14 at 04:54

1 Answers1

0

This question is related to your other question. From what I see, you want to go the interrupt driven approach now.

Your should familiarize yourself with how strings are handled in C in general. In essence, you would write any received character into an array (which would be you receive buffer). So instead of

ReceivedByte = UDR

you would write something like this

_ReceiveBuffer[i++] = UDR

where _ReceiveBuffer could be a global array like this

char _ReceiveBuffer[100];

for example. You obviously have to check for buffer overflow and could use some pointer arithmetic to handle stuff, but this should get you started.

Some comments on your code:

ISR (USART_RXC_vect) {
    PORTD= 0b00000100;  //enable RTS
    char ReceivedByte ;
    ReceivedByte = UDR ; // Fetch the received byte value into the variable " ByteReceived "
    UDR = ReceivedByte ; // Echo back the received byte back to the computer
    PORTD= 0b00000000;  //disable RTS
}

You shouldn't echo back the data from within the ISR or do anything time consuming, you risk blocking your ISR. While this specific code would work, you are not waiting for the data to be transmitted, which isn't good practice.
And are you sure you even use hardware handshaking? In you previous post it doesn't look like it, so you can remove the two lines where the potential RTS signal is toggled.

while(1) {
    sei();
}

Continuously calling sei() makes no sense. sei() enables the global interrupt flag, its sufficient to do this once.

I suggest to read some C and AVR tutorials, since you lack some basic understanding.

Community
  • 1
  • 1
Rev
  • 5,330
  • 4
  • 21
  • 49