Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Uart communication between two different microcontrollers

Status
Not open for further replies.
Don't use a string array. Use seperate array for each string (Command). Compare each command of ATMega32 with the command that comes through usart.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
//ATMEGA32 Commands
 
unsigned char usartIN[];
unsigned char cmd0[] = "SCAN";
unsigned char cmd1[] = "NO_COM";
//...
 
if strcmp(cmd0, usartIN) || strcmp(cmd1, usartIN) || strcmp(cmd2, usartIN)...)
 
// send cmd to ATMEGA32.


Thanks for the reply,
I understand the concept that you are suggesting but what i didn't understand in my code is in which variable i have the incoming command. This was written by someone not by me and i am trying to edit it.

By debugging my code i understand that "&Compare" have the incoming command, because in my code this line
Code:
printf("@%s\r\n",&Compare); //Ack command
if i send PSCAN command from PC then the above line is printing in the screen like "@PSCAN". So i am thinking I have incoming command is in "&Compare".

I can compare that like as you suggested but i am not sure how to send that to usart.

because i have written usart for sending character if i use
Code:
Uart_putchar(Compare);
i might get error. Even the below function also not working.
Code:
uart_puts(Compare);

function is like this

void uart_puts(const char *s)
{
    while(*s)
    {
        uart_putchar(*s++);
	//	printf("*");
	}
}
.

This code was written by an experienced person and i have recently joined in place of him and i am learning the ropes of programming. Can you help me with this.
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
unsigned char compare[];
unsigned char cmd0[] = "SCAN";
unsigned char cmd1[] = "NO_COM";
//...
 
if strcmp(cmd0, usartIN) || strcmp(cmd1, usartIN) || strcmp(cmd2, usartIN)...)
 
// send cmd to ATMEGA32.
 
 
uart_puts(&compare);
 
 
void uart_puts(const char *s)
{
    while(*s)
    {
        uart_putchar(*s++);
    
    }
}



Post the code which reads uart data. When Command is received it should also send the sring terminating character or add the null character after cmd is received otherwise strcmp() will fail.
 

This is the code i have in AT90usb1287 controller. Which is receiving commands from PC .
Code:
#include <90usb1287.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <interface.h>
#include <uart_drv.h>
                                                           
#define CMD_SIZE     57
#define USB_CMD_SIZE     52
flash unsigned char * flash cmdList[CMD_SIZE] = {
    "",             // [0] no com
    "INF",          // [1] Displayes version date etc.
    "RESET",        // [2] Reset CPU
    "boot",         // [3] Goto boot
    "DUMP",         // [4] Display manual debug info
    "AUT",          // [5] Start automatic scanning
    "STOP",         // [6] Stop scanning
    "STAF",         // [7] Set start frequency
    "STOF",         // [8] Set stop frequency
    "RES",          // [9] Display manual debug info
    "RATE",         // [10] Display manual debug info
    "GAIN",         // [11] Set gain
    "SCAN",         // [12] Start custom scan
    "SETUP",        // [13] Display manual scan setup info
    "TEMP",         // [14] Set temperature (Celsius)
   .....................
   ...................... (52 commands for USB board and 5 commands for ATmega32A board )
    
};

/***************************************************
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 Uart_CompIndex;            // Index in Copmare array

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;


// USART1 Receiver interrupt service routine
interrupt [USART1_RXC] void usart1_rx_isr(void){   

char status,data;
status=UCSR1A;
data=UDR1;

printf("%c", data);

}

// 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;
int comp_i=0;
       
      // Read from ring-buffer and fill in Compare buffer
      while(BufCnt){                          // while unread contents in ring-buffer
            c = getcharb();                    // fetch next byte
//  uart_putchar(c);

            if(CompIndex >= COMPBUFSIZE) CompIndex = 0;// overflow protection                    
            // Analyze char
            if(c == '#'){                     // Manual start
                  CompIndex = 0;
uart_putchar(c);
            }else if(c == '\r'){              // CR continue (end of cmd without argument)                         
                  Compare[CompIndex]='\0';    // fill in end character of comp string
uart_putchar(c);
                  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
         uart_putchar(c);
            }else if(c == 9){                 // Horizontal TAB 
                  help();                     // Write out cmds
         uart_putchar(c);

            }else if(c == 27){                // ESC button
                  Command = 0;                // Stop current command
		uart_putchar(c);

                  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<USB_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{

      if(c>CMD_SIZE-1){             // If match on normal commands

            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;
	}
      }
          
}
 

SerIn[] is yout uart buffer. Command will be in this buffer when you send the command. Compare cmd0, cmd1, cm2,... with SerIn. You should make sure to terminate SerIn[] with null character to make it a string.
 

SerIn[] is yout uart buffer. Command will be in this buffer when you send the command. Compare cmd0, cmd1, cm2,... with SerIn. You should make sure to terminate SerIn[] with null character to make it a string.

Ok but is it same with the code as shown below
Code:
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;
}
because the above method is called in the function as shown below
Code:
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();
so where should can i compare easily? can you suggest me how to do it?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top