How to get data from one micro controller to another using UART

avrmicrocontrollerprogramminguart

My concept is I have two designed boards, One with AT90USB1287, other with ATmega32A both are working properly.

AT90USB1287 is talking to the PC via USB.
ATmega32A is talking to the PC via USART(RS232).

Now i have connected ATmega32A board to AT90USB1287 using UART communication(RS232 cable by disabling connection to PC) Because both boards has to work together with one PC. Easiest way is UART connection between those two.

If i want to send data from ATmega32A to PC means, I have to get data from ATmega32A to ATusb901287 and send it to PC via USB.

I am confusing how to get data from ATmega32A to AT90USB1287.
I have given connections like
1. connected RxD pin i.e., PD0 i.e., Pin 14 of ATMega32 to TxD pin i.e., PD3 i.e., Pin 28 of AT90USB1287
2. connected TxD pin i.e., PD1 i.e., Pin 15 of ATMega32 to RxD pin i.e., PD2 i.e., Pin 27 of AT90USB1287
3. USB pins of AT90USB1287 are connected to PC USB.

Updated

I have implemented UART functionality for both controllers.

The problem is i am not able to send the command that i got from PC to AT90USB1287 via USB. the command will be in USB receive buffer. I have to send the command to another micro controller using UART of AT90usb1287.

Updated2:

I have one more problem with this communication between two microcontrollers using USART. I have the interface code for both controllers like this.

I have the first board with AT90USB1287 microcontroller which is connected to PC using USB, I have several commands like this "CMD", "TEMP" … When i send command it gives the response. Which is working perfectly. I have to connect another board which contains ATmega32A microcontroller to this board using USART. I have connected and I implemented USART communication in both boards, even the second board also have the similar type of command format and i need to send command to get response as well.
To send the Command from PC to second board i have to send the command first to AT90USB1287 board using USB communication and from that to ATmega32A using USART. I have done that and i am able to get response from second board also but i have one problem. What happening with my code is whatever command i send from PC it is sending to second board, i need to change like only commands which exist in second board has to sent to the second board.

Can anyone suggest me that how to stop sending all commands and send only those 5 commands which related to second board? Please help me with this.

Best Answer

I dont understand your confusion. You want to communicate between the two controllers with UART, then ofcourse both of them need UART capabilities and code for this. Your computer has drivers and software for dealing with this, so your microcontroller also needs a driver for it.

Your ATmega32 need UART TX functionality, while AT90USB1287 need UART RX functionality. Having already written both TX and RX for your ATmega32, writing the UART RX routine for AT90 should be a breese. You could add a bitvalue of some kind, identifying that the UART RX on AT90 is coming from your ATmega32, and directly pass it through to USB.

EDIT: Code from AVRFreaks that is written for at90usb1287

#include <avr/io.h> 
#include <util/delay.h> 

#define RXC      (RXC1) 
#define TXC      (RXC1) 
#define UCSRC    (UCSR1C) 
#define UCSRB    (UCSR1B) 
#define UCSRA    (UCSR1A) 
#define UDR      (UDR1) 
#define UBRRL    (UBRR1L) 
#define UBRRH    (UBRR1H) 
#define UBRR     (UBRR1) 
#define UDRE     (UDRE1) 
#define RXCIE    (RXCIE1) 
#define TXEN     (TXEN1) 
#define RXEN     (RXEN1) 
#define UCSZ0    (UCSZ10) 
#define UCSZ1    (UCSZ11) 
#define UMSEL0   (UMSEL10) 
#define UMSEL1   (UMSEL11) 

#define FRAMING_ERROR (1<<FE1) 
#define PARITY_ERROR (1<<UPE1) 
#define DATA_OVERRUN (1<<DOR1) 
#define DATA_REGISTER_EMPTY (1<<UDRE1) 
#define RX_COMPLETE (1<<RXC1) 

#define FOSC 16000000 

typedef unsigned char       Uchar; 
typedef unsigned long int   Uint32; 

#define Wait_USART_Ready() while (!(UCSR1A & (1<<UDRE1))) 

#define lowByte(w) ((uint8_t) ((w) & 0xff)) 
#define highByte(w) ((uint8_t) ((w) >> 8)) 

int main(void) 
{ 
/* Disable clock division */ 
clock_prescale_set(clock_div_1); 
   UBRRH = (Uchar)((((Uint32)FOSC)/((Uint32)USART_BAUDRATE*16)-1)>>8); 
   UBRRL = (Uchar)(((Uint32)FOSC)/((Uint32)USART_BAUDRATE*16)-1) & 0x0ff; 

   UCSRB |= (1 << RXEN) | (1 << TXEN); 
   UCSRC |= (1 << UCSZ1) | (1 << UCSZ0); 

   char ReceivedByte; 
   for (;;) // Loop forever 
   { 
      while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been received and is ready to be read from UDR 
      ReceivedByte = UDR; // Fetch the received byte value into the variable "ByteReceived" 

      while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it 
      UDR = ReceivedByte; // Echo back the received byte back to the computer 
   } 
}