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.