Shop OBEX P1 Docs P2 Docs Learn Events
UART between two micro controllers. — Parallax Forums

UART between two micro controllers.

veerubijiveerubiji Posts: 2
edited 2012-12-15 06:05 in General Discussion
Hi,

I have connected ATmega32a to AT90USB1287 using UART and AT90USB1287 to PC using USB.

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

[FONT=Tahoma, Arial, sans-serif]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.
I have the fallowing code.
[/COLOR][/FONT]

#include <90usb1287.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <interface.h>


#define CMD_SIZE     9
flash unsigned char * flash cmdList[CMD_SIZE] = {
    "",             // [0] no com
    "INF",          // [1] Displayes version date etc.
    "TEMP",          // [1] Displayes temp etc.
    "RESET",          
    "GOTO_BOOT",          
    "SETID",         
    "SENS1",          // [1] Displayes sensor1  etc.
    "USB",            // [1] Displayes usb  etc.
    "DISP",          // [1] Displayes calibration etc. 
};


//this was implemented by me ////
/*************************************************** 
C O M U N I C A T I O N  RS-232 
****************************************************/ 
unsigned char SerIn[SIBUFSIZE];     // Input buffer (raw data)
unsigned char RxCnt;                // Location of next byte to be written
unsigned char RdCnt;                // Location of next byte to be read
unsigned char BufCnt;               // Size of unread contents in ring buffer
unsigned char CompIndex;            // Index in Copmare array 
unsigned char Compare[COMPBUFSIZE]; // Command string tokenizer 
 unsigned char Command;              // Current Command is executed 
 unsigned int  Param;                // Parameter used in command 
       float  Param2;               // Optional (second) parameter used in command 
unsigned long Param3;               // Optional (third) parameter in command 
extern unsigned char Plot;
unsigned char Step; 


//extern unsigned char state; 
//extern unsigned int status; 
//extern unsigned char debug; 


#define RXB81 1 
#define TXB81 0 
#define UPE1 2 
#define OVR1 3 
#define FE1 4 
#define UDRE1 5 
#define RXC 7 


#define FRAMING_ERROR (1<<FE1) 
#define PARITY_ERROR (1<<UPE1) 
#define DATA_OVERRUN (1<<OVR1) 
#define DATA_REGISTER_EMPTY (1<<UDRE1) 
#define RX_COMPLETE (1<<RXC) 


 //USART1 Receiver buffer 
#define RX_BUFFER_SIZE1 32 
char rx_buffer1[RX_BUFFER_SIZE1]; 


#if RX_BUFFER_SIZE1<256 
unsigned char rx_wr_index1,rx_rd_index1,rx_counter1; 
#else 
unsigned int rx_wr_index1,rx_rd_index1,rx_counter1; 
#endif 


// This flag is set on USART1 Receiver buffer overflow 
bit rx_buffer_overflow1; 


// USART Receiver interrupt service routine 
interrupt [USART1_RXC] void usart1_rx_isr(void){ 
    char status; 
    status=UCSR1A; 
   // data=UDR1;  
   SerIn[RxCnt] = UDR1;
    if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0) 
       { 
       rx_buffer1[rx_wr_index1]=SerIn[RxCnt]; 
       if (++rx_wr_index1 == RX_BUFFER_SIZE1) rx_wr_index1=0; 
       if (++rx_counter1 == RX_BUFFER_SIZE1) 
          { 
          rx_counter1=0; 
          rx_buffer_overflow1=1; 
          }; 
       }; 
} 


#ifndef _DEBUG_TERMINAL_IO_ 
// Get a character from the USART Receiver buffer 
#define _ALTERNATE_GETCHAR_ 
#pragma used+                                
char getchar(void){ 
    char data; 
    while (rx_counter1==0); 
    data=rx_buffer1[rx_rd_index1]; 
    if (++rx_rd_index1 == RX_BUFFER_SIZE1) rx_rd_index1=0; 
    #asm("cli") 
    --rx_counter1; 
    #asm("sei") 
    return data; 
} 
#pragma used- 
#endif 
////// USART Transmitter buffer 
#define TX_BUFFER_SIZE1 32 
char tx_buffer1[TX_BUFFER_SIZE1]; 


#if TX_BUFFER_SIZE1<256 
unsigned char tx_wr_index1,tx_rd_index1,tx_counter1; 
#else 
unsigned int tx_wr_index1,tx_rd_index1,tx_counter1; 
#endif 


//// USART Transmitter interrupt service routine 
interrupt [USART1_TXC] void usart1_tx_isr(void){ 
    if (tx_counter1){ 
        --tx_counter1; 
        UDR1=tx_buffer1[tx_rd_index1]; 
        if (++tx_rd_index1 == TX_BUFFER_SIZE1) tx_rd_index1=0; 
        //#asm("WDR"); // For long words and slow baud-rates 
    } 
} 


#ifndef _DEBUG_TERMINAL_IO_ 
//// Write a character to the USART Transmitter buffer 
#define _ALTERNATE_PUTCHAR1_ 
#pragma used+ 
void putchar(char c){ 
     //chz c = getcharb(); 
    while (tx_counter1 == TX_BUFFER_SIZE1); 
    #asm("cli") 
    if (tx_counter1 || ((UCSR1A & DATA_REGISTER_EMPTY)==0)){
        tx_buffer1[tx_wr_index1]=c; 
        if (++tx_wr_index1 == TX_BUFFER_SIZE1) tx_wr_index1=0; 
        ++tx_counter1; 
    } 
    else 
    UDR1=c; 
    #asm("sei") 
} 
#pragma used- 
#endif
                




// USB Receive
void catchString(void){
    
    while(UEBCLX){
    
        if(++BufCnt >= SIBUFSIZE){               // Increment & check for buffer overflow  
            BufCnt = SIBUFSIZE-1;                // Set to max value 
//            printf("!Overflow\r\n");
//            UENUM = 2;   
            return;                              // Skip char
        }else{                                   // Else: if buffer ok
            if(++RxCnt >= SIBUFSIZE) RxCnt = 0;  // Increment read counter, if 10 -> 0 (max 9)
            SerIn[RxCnt] = UEDATX;               // Write to SBUF (load the transmit register)
        }
     }  
}
//
//
//// Read from ringbuffer
char getcharb(void){


    if(BufCnt){                                 // If anything
        BufCnt--;                               // Decrement buffer counter
        if(++RdCnt >= SIBUFSIZE) RdCnt = 0;     // Increment read counter, if 10 -> 0 (max 9)
        return SerIn[RdCnt];                    // Read from SBUF (access receive register)
    }
    return 0;
}






void help(void){


      unsigned char  i;
        
      printf("Commands: ");
      for(i=0;i<CMD_SIZE;i++){
            printf(cmdList[i]);
            printf(", ");
      }printf("\r\n");
}




/***************************************************
S T R I N G   T O K E N I Z E R
Searches the input buffer for valid commands
returns the id of the command if a match is found or 0 if no cmd   
****************************************************/
                                                                 
void getcom(void){
      
      unsigned char c;
       
      // Read from ring-buffer and fill in Compare buffer
      while(BufCnt){                          // while unread contents in ring-buffer
            c = getcharb();                    // fetch next byte
            if(CompIndex >= COMPBUFSIZE) CompIndex = 0;// overflow protection                    
            // Analyze char
            if(c == '#'){                     // Manual start
                  CompIndex = 0;
            }else if(c == '\r'){              // CR continue (end of cmd without argument)                         
                  Compare[CompIndex]='\0';    // fill in end character of comp string
                  break;                      // possible valid cmd received -> check out
            }else if(c == '\n'){              // New line (ignore)                         
                  // Do nothing (ignore)
            }else if(c == 8){                 // Backspace
                  if(CompIndex) CompIndex--;  // decrement index
            }else if(c == 9){                 // Horizontal TAB 
                  help();                     // Write out cmds
            }else if(c == 27){                // ESC button
                  Command = 0;                // Stop current command
                  Param = 0;                  // Clear argument
                  Plot = 0;                   // Stop plotting
                  Step = 0;
            }else{
                  Compare[CompIndex++]=c;     // Default action: Store character
            }if(!BufCnt) return;              // if no more data to read -> exit                                          
      }CompIndex=0;                           // reset, ready for next command
      
      c = 1;
                            
     
      while(c<CMD_SIZE){          // For each command       
            if(strncmpf(Compare,cmdList[c],strlenf(cmdList[c])) == 0) break;
            c++; 
      }
       
      if(c<CMD_SIZE){             // If match on normal commands
            Command = c;      
            if(isdigit(Compare[strlenf(cmdList[c])])){
                  Param = atoi(&Compare[strlenf(cmdList[c])]);
                  c = strpos(Compare,':');                 
                  if(c > 0){
                        Param2 = atof(&Compare[c+1]);
                        c = strrpos(Compare,':');
                        if(c > strpos(Compare,':')) Param3 = atol(&Compare[c+1]);
                        else  Param3 = 0; 
                  }else{ 
                    Param2 = 0;
                    Param3 = 0;
                  }
            }else{
                Param  = 0;
                Param2 = 0;
                Param3 = 0;
            }
            printf("@%s\r\n",&Compare); //Ack command
      }else{
            printf("&E;1;\r\n");  // Command not found
            printf("->Unknown command: '%s'\r\n",&Compare); // If no match
            Command = 0;
             Param  = 0;
             Param2 = 0;
             Param3 = 0;
      }
          
}


[FONT=Tahoma, Arial, sans-serif][COLOR=#000000]
[/FONT]

Comments

  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2012-12-14 05:11
    Hmmm. I don't think you are going to get much help here as the Propeller doesn't have a UART, this is done in software. You might try AVRfreaks and read the PDFs for the related processors in order to determine what is possible.

    It is a big world and supporting each and every AVR and ATmega chip in every possible configuration has a different support culture.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-12-14 10:55
    I think it's fine to ask for help on multiple forums but I think it would be polite to let others what has been previously said about the topic and a link to the other forum post(s). This isn't a rule, just my personal preference.

    It looks like there has been some help given on this school project already on AVRFreaks (and StackExchange, and All About Circuits etc). Kind of a shotgun blast approach to asking for help.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2012-12-15 06:05
    Well, it is is homework... it really is best to DIY.

    Conceptually, it seems the UARTs are all working. So it is a matter of polling the buffers and moving data along. The middle device has to have two Rx and Tx buffers - a pair for the RS232 and a pair for the USB. Data has to cross over from a Rx buffer to a Tx buffer in order to move along in either direction.

    If that is all you want to do, it is rather simple. But if you want to have the communications change to a different configuration, you need to have some idea of what is going to drive that change and what is going to be a default configuration.

    I do have my own AVR projects unrelated to Parallax products and I wouldn't not expect to get someone here to sort out my code. Same goes for my CubieBoard and my TINI ds80c400.
Sign In or Register to comment.