UART is not transmitting until it receives

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In this program UART does not transmit "ATE0".I check the output through cool term.

#include <avr/io.h>
#include <string.h>
#include <util/delay.h>

#define F_CPU 8000000UL
#define BAUD 9600
#define MYUBBR F_CPU/16/BAUD-1

void UART_init(unsigned int ubrr)
{
	// Setting the baud rate to ubrr
	UCSR0A = 0<<U2X0;
	UBRR0H = (unsigned char)(ubrr >> 8);
	UBRR0L = (unsigned char)ubrr;
	//Setting the frame format to 8 data bit, no parity bit, 1 stop bit
	UCSR0C = 3<<UCSZ00;
	UCSR0B = (1<<TXEN0)|(1<<RXEN0);
}

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

unsigned char USART_Receive()
{
	while (!(UCSR0A & (1<<RXC0)));
	return UDR0;
}

void transmit_string(char* data)
{
	int i;
	for (i=0;data[i] != '\0';i++)
	{
		USART_Transmit(data[i]);
	}
}

void receive_string(char* data)
{
	char command[4];
	command[0] = '\0';
	command[1] = '\0';
	command[2] = '\0';
	command[3] = '\0';
	int index = 0,i=2,j=0;
	while ((command[2] != 0x0D) && (command[3] != 0x0A))
	{
		data
= USART_Receive(); index += 1; if (index > 3) { command[0] = data[index - 4]; command[1] = data[index - 3]; command[2] = data[index - 2]; command[3] = data[index - 1]; } } data[index-2] = '\0'; while(data[i] !='\0') { data[j]=data[i]; i++; j++; } data[j] = '\0'; } int main(void) { CLKPR = (1 << CLKPCE);// Setting system clock to 8MHz CLKPR = 0; UART_init(MYUBBR); int i; char data[30]; while(1) { for(i=0;i<7;i++) { transmit_string("ATE0\r\n"); _delay_ms(500); } receive_string(data); transmit_string(data); } }

But when I comment the following lines:

while(1)
	{
		for(i=0;i<7;i++)
		{
			transmit_string("ATE0\r\n");
			_delay_ms(500);
		}
	//	receive_string(data);
	//	transmit_string(data);
	}	

UART transmits ATE0 and I can view it on cool term.

So why doesn't UART transmit if it has a receive command below it?Can anyone kindly explain.

 

* For deity's sake, learn to start your threads in  the correct forum! *

 

This topic has a solution.
Last Edited: Fri. Sep 15, 2017 - 11:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I said in the previous thread:

The ATMEGA 168 has on-chip debug - use it to step through the code and see what's actually happening

 

http://www.avrfreaks.net/comment...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

How are you detecting that the program does not transmit?

 

It could well be that it does transmit, but that Cool Term does not receive properly until it has transmitted.

 

Also: You still have not fixed the inconsistency in naming - pointed out to you in your earlier thread. 

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Geeth wrote:
 cool term.

You mean this: http://freeware.the-meiers.org/CoolTerm_ReadMe.txt.html ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have set the cool term settings and tested the program with receive instruction below transmit and without receive instruction . Without receive instruction the code works.

So it is not the cool term.

 

I have made changes in naming :

 

void UART_init(unsigned int ubrr)

void USART_Transmit( unsigned char data )

unsigned char USART_Receive()

 

The other uart functions are sub-functions so they are in small letters:

void transmit_string()

void receive_string()

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

YES

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Geeth wrote:
I have made changes in naming :

 

void UART_init(unsigned int ubrr)

void USART_Transmit( unsigned char data )

unsigned char USART_Receive()

So you still have one called "UART" and the other two called "USART"

 

You are also inconsistent in capitalisation of the word following the USART_ (or UART_) prefix.

 

The other uart functions are sub-functions

Eh?

 

But they are still specifically hard-coded for the USART - so there is no logical reason why they should not also have the USART_ (or UART_) prefix - and not having it hides useful information.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you . I will make these changes immediately. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include <avr/io.h>
#include <string.h>
#include <util/delay.h>

#define F_CPU 8000000UL
#define BAUD 9600
#define MYUBBR F_CPU/16/BAUD-1



void USART_Init(unsigned int ubrr)
{
	// Setting the baud rate to ubrr
	UCSR0A = 0<<U2X0;
	UBRR0H = (unsigned char)(ubrr >> 8);
	UBRR0L = (unsigned char)ubrr;
	//Setting the frame format to 8 data bit, no parity bit, 1 stop bit
	UCSR0C = 3<<UCSZ00;
	UCSR0B = (1<<TXEN0)|(1<<RXEN0);
}


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


unsigned char USART_Receive()
{
	while (!(UCSR0A & (1<<RXC0)));
	return UDR0;
}


void USART_Transmit_String(char* data)
{
	int i;
	for (i=0;data[i] != '\0';i++)
	{
		USART_Transmit(data[i]);
	}
}

void USART_Receive_String(char* data)
{
	char command[4];
	command[0] = '\0';
	command[1] = '\0';
	command[2] = '\0';
	command[3] = '\0';
	int index = 0,i=2,j=0;
	while ((command[2] != 0x0D) && (command[3] != 0x0A))
	{
		data
= USART_Receive(); index += 1; if (index > 3) { command[0] = data[index - 4]; command[1] = data[index - 3]; command[2] = data[index - 2]; command[3] = data[index - 1]; } } data[index-2] = '\0'; while(data[i] !='\0') { data[j]=data[i]; i++; j++; } data[j] = '\0'; } int main(void) { CLKPR = (1 << CLKPCE);// Setting system clock to 8MHz CLKPR = 0; USART_Init(MYUBBR); int i; char data[30]; while(1) { for(i=0;i<7;i++) { USART_Transmit_String("ATE0\r\n"); _delay_ms(500); } USART_Receive_String(data); USART_Transmit_String(data); } }

 

I have edited the code to fix the inconsistency in naming

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I solved the problem by adding a 3 sec delay after 

USART_Transmit_String("ATE0\r\n");
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Really?!

 

surprise

 

Sounds like you've actually just masked the symptoms

 

Can't see why a delay should matter - unless it is, indeed, a "feature" of Cool Term.

 

Have you tried any other terminal emulator(s) ?