Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to send and receive 16 bit data using USART of ATmega32 ?

Status
Not open for further replies.

ruchirvn

Newbie level 4
Joined
Jan 19, 2012
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,358
Hi,
I want to send and receive 16 bit data using USART. How it can be done ? If I transmit by splitting 16bit data into two bytes will I receive in the same order ? If No then plz suggest me an alternate solution
My Code is given below
/*
* AVRGCC3.c
*
* Created: 1/14/2012 12:11:38 AM
* Author: Ruchir
*/


//#define __AVR_ATmega16__ 1
//#define OSCSPEED 8000000 /* in Hz */
#define DATA_REGISTER_EMPTY (1<<UDRE)

#include "avr/io.h"


void Initialize(void)
{
PORTB = 0xFF;
PORTC = 0xFF;
PORTD = 0x00;

DDRB = 0x00;
DDRC = 0x00;
DDRD = 0x00;
}

void UARTInit(void)
{
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
}

void UARTTransmit(unsigned char data)
{
/* Wait for empty transmit buffer */
while ( ( UCSRA & DATA_REGISTER_EMPTY )==0 );
/* Put data into buffer, sends the data */
UDR = data;
}

int main()
{
unsigned char ch,ch1;
Initialize();
UARTInit();
while (1)
{
ch = PINB;
ch1 = PINC;
//ch = (PINB | 0xC0);
//ch1 = (PINC & 0x3F);
//ch = UARTReceive();

UARTTransmit(ch);
UARTTransmit(ch1);

}
}

Thanks
 

hai..

I want to send and receive 16 bit data using USART. How it can be done ?

The data sheet says

Supports Serial Frames with 5, 6, 7, 8, or 9 Data Bits and 1 or 2 Stop Bits
.

So you have to split the 16 bit in 2 variables and then transmit.

If I transmit by splitting 16bit data into two bytes will I receive in the same order ?

You have to transmit the higher order byte first, then the lower order byte. The receiver also receives as higher order byte first and then lower order byte.

Have you transmitted and received single byte? because i didnt see you have written code for receive
 
Hi,
I am posting my my tx and receiver code together....
Tx Code as follows
//#define __AVR_ATmega16__ 1
//#define OSCSPEED 8000000 /* in Hz */
#define DATA_REGISTER_EMPTY (1<<UDRE)

#include "avr/io.h"


void Initialize(void)
{
PORTB = 0xFF;
PORTC = 0xFF;
PORTD = 0x00;

DDRB = 0x00;
DDRC = 0x00;
DDRD = 0x00;
}

void UARTInit(void)
{
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
}

void UARTTransmit(unsigned char data)
{
/* Wait for empty transmit buffer */
while ( ( UCSRA & DATA_REGISTER_EMPTY )==0 );
/* Put data into buffer, sends the data */
UDR = data;
}


int main()
{
unsigned char ch,ch1,ch2,ch3;
Initialize();
UARTInit();
while (1)
{
ch = PINB;
ch1 = PINC;
ch2 = (ch | 0xC0);
ch3 = (ch1 & 0x3F);
UARTTransmit(ch2);
UARTTransmit(ch3);
//ch1 = USARTReadChar();
//PORTC = ch1;


}



}
Receiver code as follows

/* Sample program for Olimex AVR-P40 with ATMega16 processor
* Echoes back the received characters on the uart. In order to work,
* connect the RX pad with PD1(pin 15) and TX pad with PD0(pin 14)
* Compile with AVRStudio+WinAVR (gcc version 3.4.6)
*/

#define __AVR_ATmega16__ 1
#define OSCSPEED 8000000 /* in Hz */

#include "avr/io.h"


void Initialize(void)
{
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;

DDRA = 0X00;
DDRB = 0xFF;
DDRC = 0xFF;
DDRD = 0x00;
}


void UARTInit(void)
{
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
}


unsigned char UARTReceive(void)
{
if (UCSRA & 0b10000000)
return UDR;
else
return 0;
}


int main()
{
int i;
unsigned char ch,ch1;
Initialize();
UARTInit();
while (1)
{
ch = UARTReceive();
ch1 = UARTReceive();
PORTB = ch;
PORTC = ch1;

}
}

I have connected Tx of first ATmega32 with the Rx of second ATmega32 but i didnt get proper values on ports(Port B and C) of second Atmega32. Please tell me proper suggest me for this.
Thanks
 

Have you configured both chip's fuse bits to work at 8 MHz?

No,actually i have used AVR development board at the Tx side and at receiver side i have used crystal ......
will it work or i have to fuse bits for this?
if yes then plz tell me how to configure fuse bits?
 

Default factory setting is 1 MHz internal oscillator. although you use any crystal, chip still run at 1 MHz if you didn't set the fusebits. If you want to use external crystal 8 MHz, I suggest, just configure fusebits High to 0xD9 and Low to 0xFF. For more detail, please read AVR datasheet at Clock Source section. If you use AVR Studio, you can configure it easily. You only have to configure it once, except you want to re-configure with new value. Be careful, wrong setting will make chip can not be read by most serial programmer and you will need parallel programmer or such as "fusebits doctor" to normalize it.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top