USART_TX_Interrupt

Hello all,

I am transmitting and receiving through pin 14 and pin 15 of arduino mega using USART3 interrupt.

But in the tx interrupt only dummy values are transmitted.

Can any one correct me? Here is my code.

#define USART_BAUDRATE 9600
#define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

unsigned char frame_error; 
volatile unsigned char trans_flag,trans_char_count,ok_is,B1R1;
volatile unsigned  char rec_char_cnt;
unsigned char trans_data[50];
unsigned char rec_data[50];
unsigned char B2RX0,no_of_bytes,no_trans_char,B0R0,B0R1,rec_error,B1R0,n_add;
volatile unsigned char *ptr,*ptr1;
volatile unsigned char receive_flag;
unsigned int crc;
unsigned char B2R0,B3RX2;
volatile unsigned char rec_int_flag;
volatile unsigned char char_delay, frame_delay;
volatile unsigned char rec_status,data;


#define FRAMING_ERROR (1<<FE3)
#define PARITY_ERROR (1<<UPE3)

ISR(USART3_RX_vect)
{    
     rec_status = UCSR3A;
    rec_status &= (FRAMING_ERROR | PARITY_ERROR);
   
   if (rec_status==0)
{
    *ptr=UDR3;
    ++ptr;
    ++rec_char_cnt;
}
   
    if(rec_char_cnt==8)
    {

    rec_int_flag=0;
   // rec_char_cnt=0;
  
    receive_flag=1;
    ptr= &rec_data[0];

    }
    }

ISR(USART3_UDRE_vect)
{
if(trans_flag==1)
{
UDR3=*ptr;
++trans_char_count;
++ptr;
}
if(trans_char_count==6)
{ 
 trans_char_count=0;
 trans_flag=0;
 UCSR3B &= ~((1<<UDRIE3)|(1<<TXEN3));
}
}


void set_uart()
{   
    UBRR3H = (uint8_t)(UBRR_VALUE >> 8);
    UBRR3L = (uint8_t) UBRR_VALUE;
    UCSR3B=(1<<RXCIE3)|(1<<RXEN3)|(1<<TXEN3);
    UCSR3C=(1<<UCSZ31)|(1<<UCSZ30);
}
 

int main(void)
{

 
    cli();
    set_uart();
    ptr = &rec_data[0];
    sei();
    n_add=1;
    

    while(1)
    {
     if(receive_flag==1) 
    {
      rd_data();
      rec_error = 0;
      rec_char_cnt = 0;
      receive_flag = 0;
    }
 }
}

void rd_data()
{
  rec_error = 0;

  if ((rec_data[0] == 1)) // not valid serial number 
  {  
 
    if(rec_data[1] == 3)
    {
      if(rec_data[2] || rec_data[3] < 0 || rec_data[3] > 26)
        e2_trans();
      if(rec_data[4] || rec_data[5] > (27 - rec_data[3]) || rec_data[5] > 27)
        e3_trans();
    }
    
    else
    rec_error = 1; 

    if (!rec_error)
    {
      no_of_bytes = rec_char_cnt - 2;
  
      calculate_crc( &rec_data[0]);
    
      B0R0 = (char)(crc & 0x00ff);
      B0R1 = (char)((crc & 0xff00) >> 8);
     
      if (B0R1 == rec_data[no_of_bytes+1])
      {
        if (B0R0 == rec_data[no_of_bytes ])
        {
          if(rec_data[1] == 3)
              read_data1();


        }
      }
      else 
      {
        rec_error = 8;
        trans_err_msg();  /* transmits error code 1 illegle fn */
      }
    }
    else 
      trans_err_msg();  /* transmits error code 1 illegle fn */
  }
}

void e2_trans(void)
{
  rec_error = 2;    //* no valid data address 
  trans_err_msg();  //* transmits error code 2 illegle data address 
  return;
}
void e3_trans(void)
{
  rec_error = 3;    //* no valid data value 
  trans_err_msg();  //* transmits error code 3 illegle data value 
  return;
}

void read_data1(void)
{
  
  trans_data[0] = rec_data[0];    //* sr. no. 
  trans_data[1] = rec_data[1];    //* function code echo 
  trans_data[2] = rec_data[5] * 2;        //* byte count of data 
  ok_is = 3;

  
    trans_data[ok_is] = 1;
    ok_is ++;
    trans_data[ok_is] = 2;
    ok_is ++;

  no_trans_char = 5 + trans_data[2];
  no_of_bytes = trans_data[2] + 3;
  calculate_crc( &trans_data[0]);
 
      B0R0 = (char)(crc & 0x00ff);
      B0R1 = (char)((crc & 0xff00) >> 8);

  trans_data[ok_is] = B0R0;
  ok_is ++;
  trans_data[ok_is] = B0R1;
  do_transmit();
}

void trans_err_msg(void)
{
  trans_data[0] = rec_data[0];
  trans_data[1] = ( rec_data[1] | 0x80 );
  trans_data[2] = rec_error;

  no_trans_char = 5;

  no_of_bytes = 3;

  calculate_crc( &trans_data[0] );
  trans_data[3] = ( unsigned char )( crc & 0x00ff );
  trans_data[4] = ( unsigned char )(( crc & 0xff00 ) >> 8);
    do_transmit();
}

void do_transmit(void)
{
     while(!(UCSR3A & (1<<UDRE3)));  
     if(trans_flag==0)
     {
     UDR3=trans_data[0];
     }
     ptr = &trans_data[1];
     trans_flag=1;
     UCSR3B |= (1<<UDRIE3)|(1<<TXEN3);

}


void calculate_crc(unsigned char *array_ptr)
{
  crc = 0xffff;
  for ( B2R0 = 0 ; B2R0 < no_of_bytes ; B2R0++ )
  {
    crc ^= ( *array_ptr );
    array_ptr ++;
    for ( B0R0 = 0 ; B0R0 < 8 ; B0R0++ )
    {
      B3RX2 = crc;
      crc >>= 1;
      crc &= 0x7fff;
      B3RX2 &= 0x0001;
      if ( B3RX2 )
      crc ^= 0xa001;
    }
  } 
}

Everytime through loop, you are resetting ptr:

        ptr = &rec_data[0];

Why aren't you using Serial3 like this:

void loop()
{
  if (Serial3.available()) {
    *ptr=Serial3.read();
    ++ptr;
    ++rec_char_cnt;
    if(rec_char_cnt==8)
    {
      rd_data();
      rec_error = 0;
      rec_char_cnt = 0;
      ptr= &rec_data[0];
    }
  }
}

Cheers,
/dev