Mega's Serials is not working .......??

Hello ALL,

I write a code to init the hardware serial of Mega2560. Both are on hardware interrupt.

But when i tried to read & send data i found only garbed data. Any one tell me what i m doing wrong with this?

Serial0 is connected to hype-terminal. & Serial1 is connected to GSM modem.

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
 
#define BAUDRATE 9600
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)

char String0[] ="";
char String1[] ="";
int i = 0;
int j = 0;
int flag0 = 0;
int flag1 = 0;

void setup()
{
  USART_init();
}

void loop()
{
  if (flag0 == 1)
  {
    flag0 = 0;
    USART1_Print(String0);
    USART0_flush();
  }
  if (flag1 == 1)
  {
    flag1 = 0;
    USART0_Print(String1);
    USART1_flush();
  }
}
 
void USART_init(void){
 
 UBRR0H = (uint8_t)(BAUD_PRESCALLER>>8);
 UBRR0L = (uint8_t)(BAUD_PRESCALLER);
 UCSR0B = (1<<RXEN0)|(1<<TXEN0)| (1<<RXCIE0);
 UCSR0C = ((1<<UCSZ01) | (1<<UCSZ00));
 
 UBRR1H = (uint8_t)(BAUD_PRESCALLER>>8);
 UBRR1L = (uint8_t)(BAUD_PRESCALLER);
 UCSR1B = (1<<RXEN1)|(1<<TXEN1)| (1<<RXCIE1);
 UCSR1C = ((1<<UCSZ11) | (1<<UCSZ10));
}
 

void USART0_send( unsigned char data0){
 
 while(!(UCSR0A & (1<<UDRE0)));
 UDR0 = data0;
 
}

void USART1_send( unsigned char data1){
 
 while(!(UCSR1A & (1<<UDRE1)));
 UDR1 = data1;
 
}

 
void USART0_Print(char* StringPtr0){
  while(*StringPtr0 != 0x00){
    USART0_send(*StringPtr0);
    StringPtr0++;
  }
}

void USART1_Print(char* StringPtr1){
  while(*StringPtr1 != 0x00){
    USART1_send(*StringPtr1);
    StringPtr1++;
  }
}

ISR(USART0_RX_vect)
{
  String0[i] = UDR0;
  flag0 = 1;
  i++;
}

ISR(USART1_RX_vect)
{
  String1[j] = UDR1;
  flag1 = 1; 
  j++;
}

void USART0_flush()
{
  for(int incre = 0; incre <=i; incre++)
  {
    String0[incre] = '/0';
  }
  i = 0;
}

void USART1_flush()
{
  for(int incre = 0; incre <=j; incre++)
  {
    String1[incre] = '/0';
  }
  j = 0;
}

Any kind of suggestion would be appreciated.

It's hard to know where to start.

First question. The Arduino comes with a Serial library. Why not use that?

char String0[] ="";
char String1[] ="";

With all due respect, I suggest you read a tutorial on the C language.

Thanks for your reply.

Arduino Comes with Serial Library true. But if you want hardware interrupt on it. So it is not working like that you have to put Serial.available() on it. which i dont want to use coz at the end of the day this is call polling.

Thanks for your advise :slight_smile:

ambuj:
which i dont want to use coz at the end of the day this is call polling.

Why?

Have you some demo code to show that polling is not capable of dealing with the situation?

...R

There is already an interrupt handler that deals with the arrival of ONE BYTE of serial data. If you really, absolutely, positively, must deal with that byte at the instant it arrives:

  1. I'll need convincing
  2. You can add code to the interrupt handler IF you know what you are doing (I'll need convincing)

ambuj:
Thanks for your reply.

Arduino Comes with Serial Library true. But if you want hardware interrupt on it. So it is not working like that you have to put Serial.available() on it. which i dont want to use coz at the end of the day this is call polling.

Thanks for your advise :slight_smile:

Well as the others said, it already uses interrupts. Certainly you poll with Serial.available() to see if any data has arrived, what is wrong with that?

Serial0 is connected to hype-terminal. & Serial1 is connected to GSM modem.

This doesn't sound like a time-critical application.

If you are reading GPS data, which arrives a byte at a time, what are you going to do that is so all-fired urgent when the first byte arrives from the GPS? I mean, you are going to receive something like "$GPRMC". What can you do with the first "$"? Nothing. So what is all the urgency?