UDR always writes a 0x00 once

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

Hello, usually i am working with ASCII so it slipped through my eyes. Today i was working on USART of ATmega32 in hexadecimals and i found that before the actual data, the UDR itself always sends a 00 :s

i tried several different things but it always does so. This 00 cannot be seen in ASCII mode. Have a look at the screen shot... is it intended? The code is the simplest one can have. How can i get rid of it?
I have to send a sequence to fingerprint reader and i suspect this extra byte might make problems.

#define F_CPU 16000000UL
#include 
#include 
#define USART_BAUDRATE 57600
#define BAUD_PRESCALE ((( F_CPU / ( USART_BAUDRATE * 8UL))) - 1)

int main ( void )
{
	UCSRA |=(1<<U2X);// double speed mode
	UCSRB |=(1<<RXEN)|(1<<TXEN);// Turn on the Tx and Rx
	UCSRC |=(1<< URSEL)|(1<<UCSZ0)|(1<<UCSZ1); // 8bit Format
	
	UBRRH = ( BAUD_PRESCALE >> 8);// Load upper 8-bits
	UBRRL = BAUD_PRESCALE ;// Load lower 8-bits


for (int i=0;i<3;i++)
{
	while (( UCSRA & (1 << UDRE )) == 0); // Do nothing until UDR is ready for more data	
	UDR = 0x41 ; //Send byte
}
}

Attachment(s): 

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

I gather it only happens on startup? Since in reset the txd port pin is not initialised, it can drift logic low giving you a 'break' condition. Your code fires up, inits the usart and starts sending 'A's. Happy times from then on.

Without seeing your hardware, i could suggest that you add a pullup resistor to txd, or simply ignore the transient problem.

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

Kartman wrote:
I gather it only happens on startup? Since in reset the txd port pin is not initialised, it can drift logic low giving you a 'break' condition. Your code fires up, inits the usart and starts sending 'A's. Happy times from then on.

Without seeing your hardware, i could suggest that you add a pullup resistor to txd, or simply ignore the transient problem.

I have ordered the hardware so it will arrive in a day or two. Right now, i am using proteus to just check out my usart is in working condition.

Btw i found (by commenting lines and testing) that this 00 is written on line

UBRRL = BAUD_PRESCALE ;

why UBRRL will cause the UDR to be written with 00?

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

HellsGuardian wrote:
Right now, i am using proteus
HellsGuardian wrote:
why UBRRL will cause the UDR to be written with 00?
Then ask the Proteus support about it.

Stefan Ernst

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

sternst wrote:
HellsGuardian wrote:
Right now, i am using proteus
HellsGuardian wrote:
why UBRRL will cause the UDR to be written with 00?
Then ask the Proteus support about it.

So its only happening due to simulator? Okay... can you please how can i confirm it with actual hardware? i doubt if i can read a 00 with an oscilloscope since its happening within the register and the line might already be low.

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

HellsGuardian wrote:
i doubt if i can read a 00 with an oscilloscope since its happening within the register and the line might already be low.
Then ensure that the line is high before configuring the UART, either by making it a high output in software, or by an pull-up in hardware.

Stefan Ernst

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

i wonder why you are so worried about the leading 0? Plugging stuff in, power up etc will cause dud characters to be received - it's a fact of life. You need to formulate your protocol to check for illegal characters.

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

Kartman wrote:
i wonder why you are so worried about the leading 0? Plugging stuff in, power up etc will cause dud characters to be received - it's a fact of life. You need to formulate your protocol to check for illegal characters.

You are right and yes ill fix it in my code.

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

I would try rearranging my code a bit, do not "enable" tx, rx until all other serial port parameters have been defined. Hope that helps.
JC