[TUT] [SOFT] Using the USART - Serial communications

Last post
490 posts / 0 new

Pages

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

hello everybody! i have a problem achiving my serial communication.
actually the biggest! it doesn't work!
i have an atmega 16 at 4mhz. i have a serial port on my desktop. and a max232.
what i want for you firstly is to tell me if there is a way to check if every part is doing its job!

- if i connect the Receive and trasmite at the serial port will it echo back?(to check that the serial and pc settings are ok)
- if i do the same on the max232 will it echo back?( to check if the max circuit is ok)

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

Quote:

- if i connect the Receive and trasmite at the serial port will it echo back?(to check that the serial and pc settings are ok)

No, you will have to write some AVR software for that. Did you actually read the tutorial that is at the start of this thread?

Quote:
- if i do the same on the max232 will it echo back?( to check if the max circuit is ok)

No. The MAX232 is a device that deals with conversion of electrical signals levels.

Quote:
it doesn't work!

The questions you ask make it sound that you haven't really started making an attempt yet. If so, then of course it does not work. If you actually have made an attempt to code something then show that. It will help us establish where to start.

And please read the tutorial. Dean wrote it just for people in your situation - make his efforts worthwhile.

"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]

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

i have made the connections as shown at the max232 datasheet. i have connected on the max232 the connections from the serial and from my atmega. i run the programm from the tutorial. i have tryed hyperterminal (also the private version of it) but nothing happens. no echoes.
the max232 is powered from my atmega board(homemade) with 5v. i've checked the v+ v- they output the 8,5V they shoud. i've checked the capacitor connections a thousand times. so i thought if there was a way to check every part alone.
at the hyperterminal a set the same baudrate as at the programe. i select com1 as its the only one left available ^^(at the device manager it says communications port(com1)) but nothing no echoes. any ideas?

thanks in advance!

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

Two tests you can do: short pins 2 and 3 of the serial cable together (disconnect it from the MAX232 first) and see if you get an echo - that will test that your PC is correctly sending and receiving data.

Next, reconnect the MAX232 but short the output TX and RX together (disconnect the AVR first). Again, see if you get an echo - that will test that the MAX232 is up and running.

If both of those work, check that you have correctly swapped over the TX and RX lines to the microcontroller, so that the PC's transmit line is connected to the AVR's receive line, and vice-versa.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

Clock source for the AVR? Internal RC or external crystal?
If the later, fuses correctly programmed?

Quote:
i run the programm from the tutorial

Which one? There are several. How can we tell which one?

The quality of the answers you get is highly correlated with the quality off the question you ask.

"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]

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

external crystal 4Mhz fuse set to ext.crystal/resonator high freq;start up time 16k ck +64ms.
anyway there must be something with the port cause if i cross pin2-3 there is no echo also so i'll check it and if the problem persists i'll come back seeking for your knowledge guys! thanks a lot! now some time off and happy holidays to you all!

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

Quote:

anyway there must be something with the port cause if i cross pin2-3 there is no echo also

There are several other possible reasons. Some might be revealed by the other tests that Dean suggested (apart from crossing the cables). Do those.

And show your actual code - we still do not know what you are running.

"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]

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

Can I IMPLORE people to not use this thread as a UART fault diagnosis thread - do that in AVR Forum. This thread exists ONLY to discuss the article in the first post.

Moderator.

 

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

Hello,
I’ve read through the tutorial several times, and through all… 23 pages of this particular thread!

This question seems to have been posted 3-4 times already on this forum over the past few years, but has yet to have been addressed…

I’m using the same sample code as provided in the tutorial. The Mega1280 is able to transmit characters perfectly fine: I can even transmit through a long stream of characters. But the Mega1280 Receive does not seem to be working. Here is my setup:

PC(XP) USB to RS323 (Vantec), then SparkFun adapter (instead of MAX232), then the Mega1280.
Both PC and Mega1280 are set to 9600 baud, 8-bits, no parity, 1 stop bit, and no Flow Control.
System is already using a previously used external 16MHz clock.
When pin 2 and 3 of the COM port are shorted, the loop-back works fine.
I’ve tried both HyperTerminal and PuTTy: both respond the same way.
Used the Ocilloscope: Tx and Rx pins on the Mega1280. Both have signal movement, and look reasonably valid.
The Tx and Rx signals are correctly crossed over between the Mega1280 and SparkFun.

I’ve tried to mess with the Emulation mode (ANSI, TTY, VT100, etc.) and haven’t gotten any different results

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

Cross post (4 times) from pranayagarwal deleted here.

 

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

Could any body tell me how do I send data from my PC to ATmega128....??

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

Most people would use a terminal program. Until recently all copies of Windows had Hyperterminal but these days (thank God) they've dropped it so you have to choose one for yourself. There's been many threads of recommendations here but start with Brays, Realterm, TeraTermPro and see how you get on.

 

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

berntih wrote:
Hi all!

Beeing a newbie to avrhi, I am a newbie to AVR...
I tried a USART code for ATmega128 wit internal RC oscillator. its working fine but the data transmitted on HYPER-TERMINAL lands into a endless loop.

So please help me....

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

(moved)

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

This thread is only to discuss aspects of the tutorial in the first post - not to diagnose generic UART issues - suggest that is done in AVR Forum.

Moderator

 

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

Okay. Would you like to move my post or should I post anew?

[It'll be easier if you repost - if I split and create a new thread you won't know what it's called or where to look. Moderator]

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

Wow!! excellent tuto!!, you helped me to work with USART!

thanks a lot dude since mexico!!

Armand...

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

hey great tuto abcminiuser...but it doesn´t work on my AT90CAN128 and i don´t know why. can one of you guys fix my problem?

#include 
#define F_CPU 16000000
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
int main (void)
{
																		// Turn on the transmission and reception circuitry:
	UCSR1B |= (1 << RXEN1) | (1 << TXEN1);
																		// Use 8-bit character sizes:
	UCSR1C |= (1 << UCSZ10) | (1 << UCSZ11);
																		// Load lower 8-bits of the baud rate value into the low byte
																		// of the UBRR register:
	UBRR1L = BAUD_PRESCALE;
																		// Load upper 8-bits of the baud rate value into the high byte
																		// of the UBRR register:
	UBRR1H = (BAUD_PRESCALE >> 8);
	for (;;) // Loop forever
	{
																		// Do nothing until data have been recieved and is ready
		uint8_t ReceivedByte;											// to be read from the UDR register:
		while ((UCSR1A & (1 << RXC1)) == 0) {};
																		// Fetch the recieved byte value into the variable
																		// called "ByteReceived":
		ReceivedByte = UDR1;
																		// Do nothing until UDR is ready for more data to be
																		// written to it:
		while ((UCSR1A & (1 << UDRE1)) == 0) {};
																		// Echo back the received byte back to the computer:
		UDR1 = ReceivedByte;
	}
}

I don´t get any response with my HTerm programm...thx

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

What happens if you change your for loop to this:

for (;;)
{
      while ((UCSR1A & (1 << UDRE1)) == 0) {};
      UDR1 = 'W';
}

Regards,
Steve A.

The Board helps those that help themselves.

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

mark

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

works perfectly..thanx dean...
on 20Mhz xtal, it best error (0%) when run on baud 4200 bps...

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

i have got a bit of a problem with serial communication, my atmega16 sends a totally different value from its tx then the one I am expecting it to give when I send data above decimal 127, any suggestions?

i need to send the pwm value from one microcontroller to another.

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

Quote:

i have got a bit of a problem with serial communication, my atmega16 sends a totally different value from its tx then the one I am expecting it to give when I send data above decimal 127, any suggestions

This is known as "Code Pages". Different devices map different characters into positions 127..255

Read:

http://en.wikipedia.org/wiki/Code_pages

For example, if you were Turkish, what you might get is:

http://en.wikipedia.org/wiki/Code_page_857

while if you were Israeli usign Hebrew what you might see is:

http://en.wikipedia.org/wiki/Code_page_862

and so on.

BTW Windows uses a code page called ISO/IEC 8859-1:

http://en.wikipedia.org/wiki/ISO_8859-1#Codepage_layout

 

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

Quote:

i have got a bit of a problem with serial communication, my atmega16 sends a totally different value from its tx then the one I am expecting it to give when I send data above decimal 127, any suggestions?

Yes.
The first suggestion is: Tell us what data you think you are sending, and tell us what data you see "at the other end".
The second suggestion is: Show us the code. Preferrably a minimal program with everything thrown out except the UART send and receive parts, perhaps also the code that you actually use to see what gets sent or what is received (e.g. the code to display data on an LCD, if that is what you are doing. It is probably not of interest to see the ADC code as such. So perhaps do something like this on the sending side (sketchy):

unsigned char c = 0;
do {
   UART_send(c);
   c++;
} until (c == 255);

Cliff! I read the post as he is sending binary data (readings from the ADC from one AVR to another). No Windoze involved, ergo no code pages.

I'd speculate that it is

- either a signed/unsigned problem

- or a problem with how the binary data is ultimately converted into readable form, e.g. to a string to show on a display

- or perhaps even a problem with setting up the ADC and/or take the reaadings from it (8 bit or 10 bit? left or right adjusted result?...)

So we really need more info on what goes wrong, and we really need to see some code..

"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]

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

Thanks a lot for the replies...!
I am sending values like 220 from my avr's serial port, I know my serial port works correctly because it sends ascii letters and numbers correctly. I am using a terminal program on my PC to troubleshoot the MC before connecting it to my other MC.

In the extended ascii mode the data maps onto this:
http://en.wikipedia.org/wiki/ISO_8859-1#Codepage_layout

So this basically means that my serial port is working correctly?

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

Quote:
I know my serial port works correctly because it sends ascii letters and numbers correctly.
I think you answered your own question.

Regards,
Steve A.

The Board helps those that help themselves.

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

Quote:
So this basically means that my serial port is working correctly?

You tell us.

Oh, wait! You did...

Quote:
I know my serial port works correctly

"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]

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

ummm yes...
That link solved a lot of problems! Thanks again...!

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

can't download the tutorial pdf ,please help

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

Quote:

can't download the tutorial pdf ,please help


Were you logged in when you tried? You can only download attachments when logged in. If it still doesn't work what's actually happening? If it happens to be downloading the file with the name "index.php" then after download just rename that to "uart.pdf" and it should work.

Moderator

 

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

abhay289 wrote:
can't download the tutorial pdf ,please help

Just tested it, and it works fine for me. Do you get any error message, or is the download corrupt, or what...?

"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]

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

I can't download the pdf from the first post either. it says "141770/0b, stopped", without stopping it (tried more than once)

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

Hey Guys,

I'm new with AVR and the STK600.
The tutorial is awesome (great job Dean).
I tried the Code, but it's not possible to receive or send data.

I'm using the STK600 with an ATMega1280 and the AVRStudio (Version 4.18).
If I connect the Board via USB to my PC, I can adjust VTarget (5V), Clock Generator (8MHz) and the fuses.
After that I switch everything to the JTAGICEmkII. In the HW Settings the VTarget is still 5V but the Clock Generator is now blank. So what is my Clock now?
I guessed it's still 8MHz, so I choose for F_CPU 8MHz (CKDIV8 fuse non-selected).
Terminal works, because if I short the RX/TX pins of the RS232 Spare, I get an answer.

Here's my code, maybe there is a mistake in there..

#include 


#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (( 8000000 / (USART_BAUDRATE * 16UL)) -1)

int main (void)
{

	char ReceivedByte;

	UCSR0B = (1<<RXEN0) | (1<<TXEN0); 	//Turn on the transmission and reception circuitry
	UCSR0C = (1<<UCSZ00) | (1<<UCSZ01); 	//Use 8-bit character sizes - URSEL bit set to select the UCRSC register
	UBRR0L = BAUD_PRESCALE;				//Load lower 8-bits of the baud rate value into the low byte of the UBRR register
	UBRR0H = (BAUD_PRESCALE >> 8);			//Load upper 8-bits of the baud rate value into the high byte of the UBRR register

	for (;;)					//Loop forever
	{

		while ((UCSR0A & (1 << RXC0)) == 0 )
		{};					//Do nothing until data have been received and is ready to be read from UDR
		
		ReceivedByte = UDR0;			//Fetch the received byte value into the variable "ByteReceived"

		while ((UCSR0A & (1 <<UDRE0)) ==0 )
		{};					//Do nothing until UDR is ready for more data to be written to it
		
		UDR0 = ReceivedByte;			//Echo back the received byte back to the computer
	}		
	return 0;
}

I connected the RX/TX pins of the RS232 Spare to PE0 and PE1.
The switch for the different types of clock is set to ext, as I use the programmable clock generator (I guess...)

Thanks for your help!!

Cheers,
Michael

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

Okay, problem solved, but I don't know why...
I tried to switch the LED's in the upper code on like:

DDRB = 0xFF;
PORTB = 0xF0;

but it didn't work. So I tried to toggle the LED's without uart and it worked.
After that I copied line for line of the upper code in the working code and tried after each line if the LED's are still working.
At the end everything works!!

Murphy's law....

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

We really shouldn't be using this tutorial thread for general fault diagnosis but you are aware, are you not, that the STK500 and STK600 LEDs use inverse logic so 0=lit and 1=off?

Anyway the question is rhetorical as I'm now going to lock this thread. If anyone has genuine feedback to improve the article in the first post then contact js, plons or clawson and ask one of us to temporarily unlock this thread so you can add your feedback. (one of us may then be able to edit the original article).

 

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

As a sidetrack of my SD/FAT tutorial, I wrote a post on UART with ATtiny2313 and MAX3232* in my blog:

http://codeandlife.com/2012/04/12/3-3v-uart-with-max3232cpe/

My tutorial includes an example breadboard setup for the MAX(3)232 images and a simple test code, so I thought it would be useful companion article for Dean's excellent USART tutorial.

*) MAX3232 is pin-compatible with MAX232 so the tutorial can also be followed without the 3.3V requirements if using MAX232 (with the non-ACPE model, you'll need 1.0 uF caps though instead of 0.1 uF).

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

Hi all, good night.
I a new with attiny2313 works. I´m trying to setup a PC to attiny communication in a way that if I issue the command LED1 the led on PORTD6 will light on. So far I´m learning how to use seral communication and I took an example code and modified a little bit by indroducing a if/else condition. The strange thing that I´m not undestanding is:
1) If I send 'S' (=83) the led turns on after the third time that the caracter is sent.
2) If I send an A, I have to send it 3 times befor I get back the letter B ( the code adds 1 to the letter sent).
Cand someone explain that to me? I´m posting the code here and I do not have any issue with fuses. It is working but with a strange behaviour.
[code]/*
* serialcom3.c
*
* Created: 11/04/2012 20:39:57
* Author: Alf
*/

#include
#include
#define F_CPU 8000000 // Oscillator frequency.
#define BaudRate 9600
#define MYUBRR ((F_CPU / 16UL / BaudRate ) - 1 )

/* Prototypes */
void InitUART (unsigned char baudrate);
unsigned char ReceiveByte (void);
void TransmitByte (unsigned char data);

/* --------------------------------------------------------------
Main - program that recieves a character then transmits back the next character.
An example would be if you send in an A, the chip will return a B
---------------------------------------------------------------- */
int
main (void)
{
DDRD |= _BV(1) | _BV(6);

unsigned char i;
unsigned char data;
InitUART (9600); /* Set the baudrate to
9600 bps using a 8 MHz internal clock */
while (1)
{
//TransmitByte (ReceiveByte () + 1);
/* Echo the received character + 1. Example send in A then send out B */
ReceiveByte();

if (ReceiveByte()==83) {
for (i = 0; i < 200; i++);
PORTD ^= _BV(6);} // Turn on LED @ PD6

else
{
TransmitByte(ReceiveByte()+1);
}
}
}

/* Initialize UART */
void InitUART (unsigned char baudrate)
{
/* Set the baud rate */
UBRRH = (unsigned char)( MYUBRR >> 8);
UBRRL = (unsigned char) MYUBRR;

/* Enable UART receiver and transmitter */
UCSRB = (1 << RXEN) | (1 << TXEN);

/* set to 8 data bits, 1 stop bit */
UCSRC = (1 << UCSZ1) | (1 << UCSZ0);

}

/* Read and write functions */
unsigned char ReceiveByte (void)
{

/* Wait for incomming data */
while (!(UCSRA & (1 << RXC)));

/* Return the data */

return UDR;
}

void TransmitByte (unsigned char data)
{
/* Wait for empty transmit buffer */
while (!(UCSRA & (1 << UDRE)));

/* Start transmittion */
UDR = data;
}
[code]
looking for any help
Alfredo

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

armoli wrote:

ReceiveByte();

if (ReceiveByte()==83) {
...

I think this question would be better asked somewhere else than this tutorial thread. But in any case:

Every time you call ReceiveByte(), your program will wait for another key over UART. Instead, you should use:

ch = ReceiveByte();

if(ch == 83) {
...

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

Quote:

I think this question would be better asked somewhere else than this tutorial thread.

True. And for that reason I'll lock it. Suggest this continues elsewhere.

 

Pages

Topic locked