Microcontroller stops working after some time.

Status
Not open for further replies.

veerubiji

Full Member level 2
Joined
Feb 24, 2011
Messages
122
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Visit site
Activity points
2,301
Hi,

I am working with two hardware kits. I have connected board2 to board1 using USART and board1 is connected to PC using USB.

Board2 having the ATmega32A microcontroller and board1 having AT90USB1287 microcontroller. I am sending some commands from PC to both boards(PC to board1 using USB, from board1 to board2 using USART) and I am able to get response from both boards.

My problem is once i send the command to board2 from PC using board1. I am getting response for some time around 30 mins, after that i don't see any response from board2 and even i am not able to send command. I need to use external reset to send commands once again to get response. then its working.

board1 is working always.

I didn't understand what might be the problem... can anyone explain me how to solve this.I am new to this kind of situation.
Thanks in advance.
 


Do you connected two boards GND to each other? and do you have any return path for your signals?
 

I have connected two boards GND along with USART Tx, Rx and ground.

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.

1) Code for first board (AT90USB1287).
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
        uart_putchar(c);
            }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;
	}
      }
          
}
the function " uart_putchar(c); " is sending the command from this board to second board.

2) interface code for ATmega32A.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <interface.h>
#include <MEGA32.h>
#include <stdint.h>
#include <main.h>
#include "delay.h"


flash unsigned char *cmdList[CMD_SIZE] = {
    "NO_COM",       // [0] no com
    "PINF",          // [1] Displays version date etc.
    "SCAN",         // [2] 
    "WGF",          // [3] 
    "WGP",          // [4] Read Flash memory log (stopLog)
    "ADC"          // [5] Clear Flash memmory (stopLog)   
   
};


/***************************************************
C O M U N I C A T I O N  RS-232
****************************************************/
unsigned char CompIndex;            // Index in Copmare array
unsigned char Compare[COMPBUFSIZE]; // Command string tokenizer
extern unsigned char Command;              // Current Command is executed
extern unsigned int  Param;                // Parameter used in command
//       float  Param2;               // Optional (second) parameter used in command
//unsigned long Param3;               // Optional (third) parameter in command

unsigned char Step;

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

#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// USART Receiver buffer
#define RX_BUFFER_SIZE0 32
char rx_buffer0[RX_BUFFER_SIZE0];

#if RX_BUFFER_SIZE0<256
unsigned char rx_wr_index0,rx_rd_index0,rx_counter0;
#else
unsigned int rx_wr_index0,rx_rd_index0,rx_counter0;
#endif

// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow0;

// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void){
    char status,data;
    status=UCSRA;
    data=UDR;
    if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
       {
       rx_buffer0[rx_wr_index0]=data;
       if (++rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;
       if (++rx_counter0 == RX_BUFFER_SIZE0)
          {
          rx_counter0=0;
          rx_buffer_overflow0=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_counter0==0);
    data=rx_buffer0[rx_rd_index0];
    if (++rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0;
    #asm("cli")
    --rx_counter0;
    #asm("sei")
    return data;
}
#pragma used-
#endif

// USART Transmitter buffer
#define TX_BUFFER_SIZE0 32
char tx_buffer0[TX_BUFFER_SIZE0];

#if TX_BUFFER_SIZE0<256
unsigned char tx_wr_index0,tx_rd_index0,tx_counter0;
#else
unsigned int tx_wr_index0,tx_rd_index0,tx_counter0;
#endif

// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void){
    if (tx_counter0){
        --tx_counter0;
        UDR=tx_buffer0[tx_rd_index0];
        if (++tx_rd_index0 == TX_BUFFER_SIZE0) tx_rd_index0=0;
        //#asm("WDR"); // For long words and slow baud-rates
    }
}

#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c){
    while (tx_counter0 == TX_BUFFER_SIZE0);
    #asm("cli")
    if (tx_counter0 || ((UCSRA & DATA_REGISTER_EMPTY)==0)){
        tx_buffer0[tx_wr_index0]=c;
        if (++tx_wr_index0 == TX_BUFFER_SIZE0) tx_wr_index0=0;
        ++tx_counter0;
    }
    else
    UDR=c;
    #asm("sei")
}
#pragma used-
#endif

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(rx_counter0){                     // while unread contents in ring-buffer
            c = getchar();                    // 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
                  Step = 0;
            }else{
                  Compare[CompIndex++]=c;     // Default action: Store character
            }if(!rx_counter0) 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])]);
                 
            }else{   
                Param  = 0;
            }
            printf("@%s\r\n",&Compare); //Ack command
         }else{
//            printf("Command not found\r\n");   Command not found
//            printf("->??: '%s'\r\n",&Compare);  If no match
            Command = 0;
      }
          
}

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.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…