USART

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

we are trying to communicate between two atmega16 via Xbee (zigbee), we need the basic code for transmitter and receiver, and how to switch between transmitter and receiver........................    

 

* Title spelling corrected. Moderator *

 

i am shirish_avrlover

Last Edited: Sat. Feb 17, 2018 - 04:49 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So what have you done so far?   Please post your code and someone will help you with any problems.

When is your homework due?

 

Jim

 

Click Link: Get Free Stock: Retire early!

share.robinhood.com/jamesc3274

 

 

 

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

Try the tutorial forum - it has articles on both synchronous and asynchronous use of UART.

 

If you go for async you will probably want a ring buffer. I would recommend:

 

http://fourwalledcubicle.com/fil...

 

file for that (it's all the .h) here:

 

http://fourwalledcubicle.com/fil...

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

shirish_avrlover wrote:
we are trying to communicate between two atmega16 via Xbee (zigbee)

The XBee neither knows nor cares what microcontroller(s) you use - it is interely irrelevant to the XBee operation!

 

XBee is a Digi product (not Atmel); documentation &  support here:  https://www.digi.com/xbee

 

 

we need the basic code

What, exactly, do you mean by "basic code" ?

 

You've titled the thread, "USART" - so if you're after the basics of the ATMega16's USART, then you probably need to set the XBees aside for a while and get those basics covered first!

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
You've titled the thread, "USART"
Actually he's titled it "UASRT" ;-)

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

maybe that's why he can't find the "basic code" ... ?

 

frown

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use XBEE modules and as long as you do not change anything they will work together right out of the box.  Just connect your USART pins to them and they do the rest.  As long as you only use two then this is not a problem as they come out of the box as "Broadcast" units.  For the OP's purposes this should be fine.  To change this mode one either has to write code for the AVR to load into the XBEE module at runtime, or the OP can purchase programming boards from Sparkfun or Mouser.

 

For the OP's purposes I would not worry about changing anything for now.

 

One thing to know, is that the XBEE modules run on 3.3vdc and they are not 5v tolerant!  Make sure your AVR is running on 3.3vdc otherwise you will damage them.

 

The out of the box default baud rate for the XBEE modules is 9600 if that helps any.

 

JIm

 

EDIT:  OOPS!! Lee my editor caught a faux pas in the baud rate.

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Wed. Feb 14, 2018 - 08:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
The out of the box default baud rate for the XBEE modules is 5600 if that helps any.

LOL -- I'm guessing it won't help any.  Unless XBee really runs at an uncommon rate.  57600?  Googling.... Default appears to be 9600.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

theusch wrote:

jgmdesign wrote:
The out of the box default baud rate for the XBEE modules is 5600 if that helps any.

LOL -- I'm guessing it won't help any.  Unless XBee really runs at an uncommon rate.  57600?  Googling.... Default appears to be 9600.

 

OOPS!!! blushsurprise

 

9600!!!  9600!!!  9600!!!

 

 

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

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

oops sorry it UASRT i am sorry for it .................

basic code i meant that we need one simple code in which one MC will transmit a character and another one will receive and display it one lcd (atmega16 MC we are using)

(MC=micro controller )  

 

and we have written the code just check it sir..........

this below one is Transmitter code 

/*
 * TX.c
 * tx code 
 * Created: 2/11/2018 9:34:26 PM
 *  Author: e YANTRA
 */ 


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

#define TE  (1<<5)



//UART0 initialisation
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
	UCSRB = 0x00; //disable while setting baud rate
	UCSRA = 0x00;
	UCSRC = 0x86;
	UBRRL = 0x2F; //set baud rate lo  //67 is for 16MHz 9600 baudrate
	UBRRH = 0x00; //set baud rate hi
	UCSRB = 0x98;
}


void init_devices (void)
{
	cli();         //Clears the global interrupts
	//port_init();
	uart0_init();
	sei();         //Enables the global interrupts
}

void uart_tx(char data)
{
	while(!(UCSRA & TE));			//waiting to transmit
	UDR = 1;
}

//Main Function
int main()
{
	init_devices();
	while(1);
}

  this below one is receiver code

 

/*
 * RX.c
 *
 * Created: 2/11/2018 9:31:05 PM
 *  Author: e YANTRA
 */ 


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



void buzzer_pin_config (void)
{
	DDRC = DDRC | 0x08;		//Setting PORTC 3 as outpt
	PORTC = PORTC & 0xF7;		//Setting PORTC 3 logic low to turnoff buzzer
}

//Function to initialize ports
void port_init()
{
	buzzer_pin_config();
}

void buzzer_on (void)
{
	unsigned char port_restore = 0;
	port_restore = PINC;
	port_restore = port_restore | 0x08;
	PORTC = port_restore;
}

void buzzer_off (void)
{
	unsigned char port_restore = 0;
	port_restore = PINC;
	port_restore = port_restore & 0xF7;
	PORTC = port_restore;
}

//UART0 initialisation
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
	UCSRB = 0x00; //disable while setting baud rate
	UCSRA = 0x00;
	UCSRC = 0x86;
	UBRRL = 0x2F; //set baud rate lo  //67 is for 16MHz 9600 baudrate
	UBRRH = 0x00; //set baud rate hi
	UCSRB = 0x98;
}


void init_devices (void)
{
	cli();         //Clears the global interrupts
	port_init();
	uart0_init();
	sei();         //Enables the global interrupts
}

SIGNAL(SIG_UART_RECV)
{
	unsigned char receive_data=0;   // used to save Receiving data

	receive_data = UDR;
	
	
	if(receive_data == 1)        //ASCII value of 7
	{
		buzzer_on();
	}


}

//Main Function
int main()
{
	init_devices();
	while(1);
}

 

i am shirish_avrlover

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

You seem to be making the classic beginner's mistake of trying to do everything all at once - don't do that!

 

Instead, break the problem down into small steps, and fully test & debug each one before moving onto the next.

 

A fairly obvious progression for this would be:

  1. Transmit a single character from the USART, and verify that you can receive it on a PC terminal via a wired connection
  2. Learn to use the ATMega16's on-chip debug facilities to see what your code is doing.
  3. Use the PC terminal to send a character via a wired connection; Receive this via the USART
  4. Display received characters on the LCD
  5. Link the 2 AVRs via a wired connection
  6. Finally, use the wireless connection.

 

shirish_avrlover wrote:
just check it sir.

Have you checked it?

 

Have you tried it?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
SIGNAL(SIG_UART_RECV)

Have you just stepped out of a timewarp? SIGNAL has not been used for more than 10 years so whatever advised you to do that (or was this code taken from the internet?) then it is is very old and out of date. Also as a question of style do not write things like:

UCSRB = 0x98;

No one reading this will have any idea what the does. Obviously 0x98 is 10011000 so we can tell that bits 7, 4 and 3 are being set but it would take a pretty phenomenal memory for anyone to instantly know what bits 7, 4 and 3 in the UCSRB of a mega16 are. The datasheet (which is the only way to check) says:

So I guess we can conclude that it is setting RXCIE, RXEN and TXEN but it would have been so much easier if you had simply written:

UCSRB = (1 << RXCIE) | (1 << RXEN) | (1 << TXEN);

Then anyone reading this can instantly see what it does.

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

shirish_avrlover wrote:

	if(receive_data == 1)        //ASCII value of 7

That comment makes no sense!

 

If you want the ASCII code for the character '7', then just write:

	if( receive_data == '7' )

 

Or did you really mean the ASCII code value of 0x07 - for the BEL character?

 

In that case, following the principles Cliff outlined in #12, it would be far easier to read as, say,

 

#define ASCII_BEL 7

	if( receive_data == ASCII_BEL )

Then the reader (including you, in 2 months time) can see immediately what you mean - without having to ponder the magic significance of the value seven ...

 

https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants

 

EDIT

 

typo

 

Addendum:

 

http://www.asciitable.com/

 

http://en.cppreference.com/w/cpp/language/ascii

 

https://en.wikipedia.org/wiki/ASCII

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Feb 16, 2018 - 12:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//UART0 initialisation
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
	UCSRB = 0x00; //disable while setting baud rate
	UCSRA = 0x00;
	UCSRC = 0x86;
	UBRRL = 0x2F; //set baud rate lo  //67 is for 16MHz 9600 baudrate
	UBRRH = 0x00; //set baud rate hi
	UCSRB = 0x98;
}

At 16MHz, with a UBRR value of 47 (0x2F), the actual baud rate is 20,833 bps.  That puts the baud rate error at 117%.  But at least both the TXer and the RXer are using the same rate...

Greg Muth

Portland, OR, US

Xplained/Pro/Mini Boards mostly

 

Make Xmega Great Again!

 

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

BTW it's "USART," not "UASRT."

Greg Muth

Portland, OR, US

Xplained/Pro/Mini Boards mostly

 

Make Xmega Great Again!

 

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

sir can you explain this 

UCSRB = (1 << RXCIE) | (1 << RXEN) | (1 << TXEN);

i have not used this from of coding anytime..........

i am shirish_avrlover

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

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Sat. Feb 17, 2018 - 06:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

shirish_avrlover wrote:

sir can you explain this 

UCSRB = (1 << RXCIE) | (1 << RXEN) | (1 << TXEN);

(1<<0) = 0b00000001;  // shift '1' zero places left
(1<<1) = 0b00000010;  // shift '1' one place left 
(1<<2) = 0b00000100;  // shift '1' two places left 

UCSRB = (1<<6);  // set bit 6 in register UCSRB, clear other bits (UCSRB=0b01000000)
UCSRB = (1<<6)|(1<<5); // set bits 5 and 6 in register UCSRB, clear others (UCSRB=0b01100000)

// in register UCSRB set bits RXCIE, RXEN, TXEN, clear other bits 
UCSRB = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN);

 

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

shirish_avrlover wrote:
i have not used this from of coding anytime...

But note that it is simply a combination of standard 'C' operators - they can all be looked-up in a standard 'C' textbook.

 

https://en.wikipedia.org/wiki/Bitwise_operations_in_C

 

There is nothing here that is unusual or specific to microcontrollers.

 

Here are some 'C' learning & reference materials for you - including a free online text book: http://blog.antronics.co.uk/2011...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hafta admit I'm aware of this but don't use it.  I use the

 

y = 0bxxxxxxxx ; hopefully with bits (described, not abbreviated!) in the comment line  *

 

format.  I find it has advantages:  It's terse (shorter lines) and easier to flip bits around.  You don't have to add an entire | (1<<whatever) term or having lying around as |(0<<whatever) when removed.  Then again, I am almost always working with the spec sheet lying around, so that's no problem.

 

I guess if you wanted to you could make up something like

 

USICR = (0<<USISIE)|(0<<USIOIE)|(0<<USIWM1)|(0<<USIMW0)|(0<<USICS1)|(0<<USICS0)|(0<<USICLK)|(0<<USITC);  *

 

for every relevant control register and just peck in '1's where you felt like them, but that was tedious to type out, mostly useless, and I'm not going to be pasting it into my code any time soon.  And that still doesn't tell you what the various WM bits and CS bits decode as.  You still need the spec sheet (or the comments) for that.

 

S.

 

* (code window still doesn't work, down 'ere)

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

But note that 0b... is not a standard 'C' format - so not all compilers support it.

 

You don't have to add an entire | (1<<whatever) term

IF you're concerned about saving keystrokes, you could always have

#define WHATEVER_BIT (1<<WHATEVER_BIT_POSITION)

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

> I find it has advantages:
An advantage it lacks is correctness. Comment are not code. Your comments could say one thing, and the magic number could encode something entirely different. How many times have we seen >>that<< around here? Bit names are self-documenting. While the names are 'abbreviated', they are not (too) cryptic. Once I've learned what ADSC is, I won't need a data sheet to remind me. I >>will<< need a data sheet to remind me what bit number it is.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

joeymorin wrote:
How many times have we seen >>that<< around here?

There are at least two in the OP of this very thread!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It could.  But no...

 

joeymorin wrote:
I >>will<< need a data sheet to remind me what bit number it is.

 

Actually, all you do is have to look at the included header files where it's defined.  S.