Problem to read echo from hyperterminal

Status
Not open for further replies.

hardik.patel

Member level 5
Joined
Aug 15, 2012
Messages
94
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
2,008
Hi,

I am interfacing sim908 with atmega1281.
I am sending commands to sim908 via tx-rx pin. As i send "AT" it responds "OK" but somehow i cant able to receive that "OK" inside the mcu. By receiving that "Ok" i can go further steps in code.

In my hardware connection i connected sim908 with UART1 and displaying its response on hyperterminal via UART0.
So please look my code and reply me where i doing mistake.


Here i had attached hyperterminal's snapshot for your reference.

Code:
#define F_CPU 1600000// Clock Speed
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <util/delay.h>

int data_count=0;

#define buff_size 5
char data_in[buff_size];
char data_out[buff_size];
char aux_buff[100] = "\0";

#define RX_BUFF_SIZE	200
#define TX_BUFF_SIZE	200

static volatile uint8_t tx_buff[TX_BUFF_SIZE];
static volatile uint8_t tx_buff_head;
static volatile uint8_t tx_buff_tail;
static volatile uint8_t rx_buff[RX_BUFF_SIZE];
static volatile uint8_t rx_buff_head;
static volatile uint8_t rx_buff_tail;

void init_UART0(void)
{
	UCSR0A = 0;
	UCSR0B = 0;
	UCSR0C = 0;
  
	// UCSR0A |= (1<<U2X0);								// Double Baud Rate
	
	
	UCSR0B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
	//UCSR0B |= (1<<RXEN0) | (1<<TXEN0);// | (1<<RXCIE0);	// | (1<<UDRIE0);// Enable Reception
	UCSR0C |= (1<<UCSZ01) | (1<<UCSZ00);			// 8-bit Data Byte, 1 Stop bits
	
	// Set Baud Rate to 9600
	UBRR0L = 103;	// Load lower 8-bits of the baud rate value into the low byte of the UBRR register
	UBRR0H = 0;		// Load upper 8-bits of the baud rate value into the high byte of the UBRR register
	
	tx_buff_head = tx_buff_tail = 0;
	UCSR0A &=~ (1<<U2X0);				// Enable receiver and transmitter
}

void sendstring_UART0(char* buff)
{
	// Loop through the data
	while(*buff != '\0')
	{
		while ( !(UCSR0A & ((1<<UDRE0))) );	// Wait for empty transmit buffer
		UDR0 = *buff++;						// Start transmission
		_delay_ms(5);
	}
}
void transmit_UART0(char data)
{
	while ( !(UCSR0A & ((1<<UDRE0))) );	// Wait for empty transmit buffer
	UDR0 = data;						// Start transmission
}
void send_log0(char data)
{
	transmit_UART0(data);	_delay_ms(100);
}

void sendstring_log0(char* str)
{
	sendstring_UART0(str);	_delay_ms(500);
}


char receive_UART0(void)
{
	while ((UCSR0A & (1<<RXC0)) == 0);	// Wait for receive buffer full
	return UDR0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

void init_UART1(void)
{
	UCSR1A = 0;
	UCSR1B = 0;
	UCSR1C = 0;
  
	// UCSR0A |= (1<<U2X0);								// Double Baud Rate
	
	
	
	UCSR1B |= (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1);	// | (1<<UDRIE0);// Enable Reception
	//UCSRB=(1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
	
	UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);			// 8-bit Data Byte, 1 Stop bits
	
	// Set Baud Rate to 9600
	UBRR1L = 103;	// Load lower 8-bits of the baud rate value into the low byte of the UBRR register
	//UBRR1L = 207;
	UBRR1H = 0;		// Load upper 8-bits of the baud rate value into the high byte of the UBRR register
	
	rx_buff_head = rx_buff_tail = 0;
	UCSR1A &=~(1<<U2X1);	// Enable receiver and transmitter
	//sei();
}

int8_t checkavail_UART1(void)
{
	uint8_t head, tail;

	head = rx_buff_head;
	tail = rx_buff_tail;
	if (head >= tail)
	return (head - tail + 1);
	else
	return (RX_BUFF_SIZE - tail + head + 1);
	_delay_ms(10);
}
void flush_UART1(void)
{
	uint8_t t_len = 0 ;
	char t_buff = '\0' ;
	uint8_t head, tail;

	head = rx_buff_head;
	tail = rx_buff_tail;
	
	if (head >= tail)		// Data present
	t_len = (head - tail + 1);
	else					// Data present, start has rolled over
	t_len = (RX_BUFF_SIZE - tail + head + 1);
	
	while(t_len-- > 0)
	{
		t_buff = rx_buff[rx_buff_tail++];
		if(rx_buff_tail==RX_BUFF_SIZE)
		rx_buff_tail=0;
	}
	
	t_len = 0;
	while (t_len++ < RX_BUFF_SIZE)
	{
		rx_buff[t_len] = '\0';
	}
	//	tx_buff_head = tx_buff_tail = 0;
	rx_buff_head = rx_buff_tail = 0;
	_delay_ms(50);
}
void sendstring_UART1(char* buff)
{
	// Loop through the data
	while(*buff != '\0')
	{
		while ( !(UCSR1A & ((1<<UDRE1))) );	// Wait for empty transmit buffer
		UDR1 = *buff++;						// Start transmission
		_delay_ms(5);
	}
}
void send_SIM908(char* cmd)
{
	//uint8_t d_len = strlen(cmd), i = 0;
	if( checkavail_UART1() != 0 )
		flush_UART1();	
	/*
	for(i=0; i<=d_len; i++)
		putchar_UART1(cmd[i]);				//transmit the user string
	*/
	sendstring_UART1(cmd);		_delay_ms(50);
	//sendstring_log("\r\nfrom sendSIM: ");	//sendstring_log(dummy_buff);
}


void USART_Transmit1( unsigned char data )
{
	/* Wait for empty transmit buffer */
	while ( !( UCSR1A & (1<<UDRE1)) )
	;
	/* Put data into buffer, sends the data */
	UDR1 = data;
}

unsigned char USART1_Receive( void )
{
	/* Wait for data to be received */
	while ( !(UCSR1A & (1<<RXC1)) );
	/* Get and return received data from buffer */
	return UDR1;
}


void sendstring_log1(char* str)
{
	sendstring_UART1(str);	_delay_ms(500);
}

ISR(USART1_RX_vect)
{
	_delay_ms(100);
	sendstring_log0("\r\n\nTEST1\r\n"); _delay_ms(10);
	for (int i = 0; i<10; i++)
	{
		data_in[data_count] = UDR1;
		data_count++;
	}
	sendstring_log0("\r\n\nTEST2\r\n"); _delay_ms(10);
	sprintf(aux_buff, "\r\n\n==%s",data_in);	_delay_ms(10);
	sendstring_log0(aux_buff);
	data_count=0;
}
void read()
{
	sendstring_log0("\r\n\nTEST1\r\n"); _delay_ms(10);
	for (int i = 0; USART1_Receive!='\0'; i++)
	{
		data_in[data_count] = USART1_Receive();
		data_count++;
	}
	sendstring_log0("\r\n\nTEST2\r\n"); _delay_ms(10);
	sprintf(aux_buff, "\r\n\n==%s",data_in);	_delay_ms(10);
	sendstring_log0(aux_buff);
	data_count=0;
}
void ReadStringData(void)
{
	
	sendstring_log0("\r\n\nTEST8\r\n"); _delay_ms(10);
	for (int i = 0; i<buff_size; i++)
	{
		data_in[data_count] = USART1_Receive();
		data_count++;
	}
	sendstring_log0("\r\n\nTEST9\r\n"); _delay_ms(10);
	sprintf(aux_buff, "\r\n\n==%s",data_in);	_delay_ms(10);
	sendstring_log0(aux_buff);
	data_count=0;
	sendstring_log0("\r\n\nTEST10\r\n"); _delay_ms(10);
}
// Receive a byte
void getchar_UART1(char *data)
{
	*data=rx_buff[rx_buff_tail++];
	if(rx_buff_tail==RX_BUFF_SIZE)
	rx_buff_tail=0;
	_delay_ms(10);
}

void get_SIM908(char* reply, uint8_t len)
{
	//char dummy_buff[100] = "\0";
	//memset(sim_data, '\0', 100);    // Initialize the string
	//sendstring_log("\r\nfrom getSIM: ");
	
	if( checkavail_UART1() != 0 )
	{
		for(uint8_t i=0; i<len; i++)
		{
			getchar_UART1(reply++);
			
			//send_log(*(reply-1));
		}
	}
	_delay_ms(50);
	//strcpy(dummy_buff, reply);	sendstring_log(dummy_buff);
}

int main (void)
{
	unsigned char ch="/0";
	unsigned char dup[10]="/0";
	unsigned char sim_resp[20] = "\0";
	//static volatile uint8_t rx_buff[10];
	char rx_buff[10]="\0";
	init_UART0(); _delay_ms(10);
	init_UART1() ;_delay_ms(10);		// hardware uart-0
	
	sendstring_log0("\r\n\nUART0_TEST\r\n"); _delay_ms(10);
	send_SIM908("\r\n\nUART1_TEST\r\n"); _delay_ms(10);
	
	//while (1)
	//{
		
		flush_UART1();
		send_SIM908("AT\r");		_delay_ms(200);
		get_SIM908(sim_resp,20);
		
		sendstring_log0("\r\n\n$ SIM908_Init_Response:: ");
		//sendstring_log0(sim_resp);
		
		sprintf(aux_buff, "\r\n\n=%s",sim_resp);	_delay_ms(200);
		sendstring_log0(aux_buff);
		
		sendstring_log0("\r\n\n$Done1!!");
		
	 
	//}
}

- - - Updated - - -

AVR Studio - Compiler i am using for ab above task

- - - Updated - - -

I have also tried by removing bits "(1<<RXCIE0);" and "(1<<RXCIE1);" from the uart init part, but though also i achieved desired result.
 

Attachments

  • sim1.png
    719.2 KB · Views: 57

Hi,

maybe hyperterminal adds "Cr/Lf" after the "AT".

Klaus
 

so for that what should i have to change?

Even i want to just print echo as it is....but as you can see in picture its total blank after "="
 

Hi,

so for that what should i have to change?

Try to send "AT" & [Cr] & [Lf].


Klaus
 

Hi,



Try to send "AT" & [Cr] & [Lf].


Klaus

Will you please tell me...that to send AT with Cr+Lf..what exactly i have to pass?

- - - Updated - - -

Hi, KlausST..

I have try to send it...as you suggested but still the result is not as expected

here is my code and result at (https://obrazki.elektroda.pl/7379318800_1465543513.png)

Code:
#define F_CPU 1600000// Clock Speed
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <util/delay.h>

int data_count=0;

#define buff_size 20
char data_in[buff_size];
char data_out[buff_size];
char aux_buff[100] = "\0";

#define RX_BUFF_SIZE	200
#define TX_BUFF_SIZE	200

static volatile uint8_t tx_buff[TX_BUFF_SIZE];
static volatile uint8_t tx_buff_head;
static volatile uint8_t tx_buff_tail;
static volatile uint8_t rx_buff[RX_BUFF_SIZE];
static volatile uint8_t rx_buff_head;
static volatile uint8_t rx_buff_tail;

void init_UART0(void)
{
	UCSR0A = 0;
	UCSR0B = 0;
	UCSR0C = 0;
  
	// UCSR0A |= (1<<U2X0);								// Double Baud Rate
	
	
	UCSR0B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
	//UCSR0B |= (1<<RXEN0) | (1<<TXEN0);// | (1<<RXCIE0);	// | (1<<UDRIE0);// Enable Reception
	UCSR0C |= (1<<UCSZ01) | (1<<UCSZ00);			// 8-bit Data Byte, 1 Stop bits
	
	// Set Baud Rate to 9600
	UBRR0L = 103;	// Load lower 8-bits of the baud rate value into the low byte of the UBRR register
	UBRR0H = 0;		// Load upper 8-bits of the baud rate value into the high byte of the UBRR register
	
	tx_buff_head = tx_buff_tail = 0;
	UCSR0A &=~ (1<<U2X0);				// Enable receiver and transmitter
}

void sendstring_UART0(char* buff)
{
	// Loop through the data
	while(*buff != '\0')
	{
		while ( !(UCSR0A & ((1<<UDRE0))) );	// Wait for empty transmit buffer
		UDR0 = *buff++;						// Start transmission
		_delay_ms(5);
	}
}
void transmit_UART0(char data)
{
	while ( !(UCSR0A & ((1<<UDRE0))) );	// Wait for empty transmit buffer
	UDR0 = data;						// Start transmission
}
void send_log0(char data)
{
	transmit_UART0(data);	_delay_ms(100);
}

void sendstring_log0(char* str)
{
	sendstring_UART0(str);	_delay_ms(500);
}


char receive_UART0(void)
{
	while ((UCSR0A & (1<<RXC0)) == 0);	// Wait for receive buffer full
	return UDR0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

void init_UART1(void)
{
	UCSR1A = 0;
	UCSR1B = 0;
	UCSR1C = 0;
  
	// UCSR0A |= (1<<U2X0);								// Double Baud Rate
	
	
	
	UCSR1B |= (1<<RXEN1) | (1<<TXEN1) | (1<<RXCIE1);	// | (1<<UDRIE0);// Enable Reception
	//UCSRB=(1<<RXCIE)|(1<<RXEN)|(1<<TXEN);
	
	UCSR1C |= (1<<UCSZ10) | (1<<UCSZ11);			// 8-bit Data Byte, 1 Stop bits
	
	// Set Baud Rate to 9600
	UBRR1L = 103;	// Load lower 8-bits of the baud rate value into the low byte of the UBRR register
	//UBRR1L = 207;
	UBRR1H = 0;		// Load upper 8-bits of the baud rate value into the high byte of the UBRR register
	
	rx_buff_head = rx_buff_tail = 0;
	UCSR1A &=~(1<<U2X1);	// Enable receiver and transmitter
	//sei();
}

int8_t checkavail_UART1(void)
{
	uint8_t head, tail;

	head = rx_buff_head;
	tail = rx_buff_tail;
	if (head >= tail)
	return (head - tail + 1);
	else
	return (RX_BUFF_SIZE - tail + head + 1);
	_delay_ms(10);
}
void flush_UART1(void)
{
	uint8_t t_len = 0 ;
	char t_buff = '\0' ;
	uint8_t head, tail;

	head = rx_buff_head;
	tail = rx_buff_tail;
	
	if (head >= tail)		// Data present
	t_len = (head - tail + 1);
	else					// Data present, start has rolled over
	t_len = (RX_BUFF_SIZE - tail + head + 1);
	
	while(t_len-- > 0)
	{
		t_buff = rx_buff[rx_buff_tail++];
		if(rx_buff_tail==RX_BUFF_SIZE)
		rx_buff_tail=0;
	}
	
	t_len = 0;
	while (t_len++ < RX_BUFF_SIZE)
	{
		rx_buff[t_len] = '\0';
	}
	//	tx_buff_head = tx_buff_tail = 0;
	rx_buff_head = rx_buff_tail = 0;
	_delay_ms(50);
}
void sendstring_UART1(char* buff)
{
	// Loop through the data
	while(*buff != '\0')
	{
		while ( !(UCSR1A & ((1<<UDRE1))) );	// Wait for empty transmit buffer
		UDR1 = *buff++;						// Start transmission
		_delay_ms(5);
	}
}
void send_SIM908(char* cmd)
{
	//uint8_t d_len = strlen(cmd), i = 0;
	if( checkavail_UART1() != 0 )
		flush_UART1();	
	/*
	for(i=0; i<=d_len; i++)
		putchar_UART1(cmd[i]);				//transmit the user string
	*/
	sendstring_UART1(cmd);		_delay_ms(50);
	//sendstring_log("\r\nfrom sendSIM: ");	//sendstring_log(dummy_buff);
}


void USART_Transmit1( unsigned char data )
{
	/* Wait for empty transmit buffer */
	while ( !( UCSR1A & (1<<UDRE1)) )
	;
	/* Put data into buffer, sends the data */
	UDR1 = data;
}

unsigned char USART1_Receive( void )
{
	/* Wait for data to be received */
	while ( !(UCSR1A & (1<<RXC1)) );
	/* Get and return received data from buffer */
	return UDR1;
}


void sendstring_log1(char* str)
{
	sendstring_UART1(str);	_delay_ms(500);
}

ISR(USART1_RX_vect)
{
	_delay_ms(100);
	sendstring_log0("\r\n\nTEST1\r\n"); _delay_ms(10);
	for (int i = 0; i<10; i++)
	{
		data_in[data_count] = UDR1;
		data_count++;
	}
	sendstring_log0("\r\n\nTEST2\r\n"); _delay_ms(10);
	sprintf(aux_buff, "\r\n\n==%s",data_in);	_delay_ms(10);
	sendstring_log0(aux_buff);
	data_count=0;
}
void read()
{
	sendstring_log0("\r\n\nTEST1\r\n"); _delay_ms(10);
	for (int i = 0; USART1_Receive!='\0'; i++)
	{
		data_in[data_count] = USART1_Receive();
		data_count++;
	}
	sendstring_log0("\r\n\nTEST2\r\n"); _delay_ms(10);
	sprintf(aux_buff, "\r\n\n==%s",data_in);	_delay_ms(10);
	sendstring_log0(aux_buff);
	data_count=0;
}
void ReadStringData(void)
{
	
	sendstring_log0("\r\n\nTEST8\r\n"); _delay_ms(10);
	for (int i = 0; i<buff_size; i++)
	{
		data_in[data_count] = USART1_Receive();
		data_count++;
	}
	sendstring_log0("\r\n\nTEST9\r\n"); _delay_ms(10);
	sprintf(aux_buff, "\r\n\n==%s",data_in);	_delay_ms(10);
	sendstring_log0(aux_buff);
	data_count=0;
	sendstring_log0("\r\n\nTEST10\r\n"); _delay_ms(10);
}
// Receive a byte
void getchar_UART1(char *data)
{
	*data=rx_buff[rx_buff_tail++];
	if(rx_buff_tail==RX_BUFF_SIZE)
	rx_buff_tail=0;
	_delay_ms(10);
}

void get_SIM908(char* reply, uint8_t len)
{
	//char dummy_buff[100] = "\0";
	//memset(sim_data, '\0', 100);    // Initialize the string
	//sendstring_log("\r\nfrom getSIM: ");
	
	if( checkavail_UART1() != 0 )
	{
		for(uint8_t i=0; i<len; i++)
		{
			getchar_UART1(reply++);
			
			//send_log(*(reply-1));
		}
	}
	_delay_ms(50);
	//strcpy(dummy_buff, reply);	sendstring_log(dummy_buff);
}

int main (void)
{
	unsigned char ch="/0";
	unsigned char dup[10]="/0";
	unsigned char sim_resp[20] = "\0";
	//static volatile uint8_t rx_buff[10];
	char rx_buff[10]="\0";
	init_UART0(); _delay_ms(10);
	init_UART1() ;_delay_ms(10);		// hardware uart-0
	
	sendstring_log0("\r\n\nUART0_TEST\r\n"); _delay_ms(10);
	//send_SIM908("\r\n\nUART1_TEST\r\n"); _delay_ms(10);
	
	//while (1)
	//{
		
		flush_UART1();
		
		
		send_SIM908("AT\r\0d\0a");		_delay_ms(1000);
		get_SIM908(sim_resp,20);
		
		
		/*for (int i=2,j=0;i<20;i++)
		{
			data_in[j]=sim_resp[i];
		}*/
		//strcpy(data_in,sim_resp); _delay_ms(10);
		sendstring_log0("\r\n\n$ SIM908_Init_Response:: ");
		sendstring_log0(sim_resp);
		
		sprintf(aux_buff, "\r\n\n=%s",sim_resp);	_delay_ms(200);
		sendstring_log0(aux_buff);
		
	
		if( strstr(sim_resp,"OK") != NULL)
		{
			sendstring_log0("\r\n\n$ Response is OK ");
		}
		else
		{
			sendstring_log0("\r\n\n$ Response is Not OK ");
		}
		//sprintf(aux_buff, "\r\n\n=%s",sim_resp);	_delay_ms(200);
		//sendstring_log0(aux_buff);
		
		sendstring_log0("\r\n\n$Done1!!");
		
	 
	//}
}
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…