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.

[SOLVED] problem with atmega32

Status
Not open for further replies.

eng-ahm

Newbie level 2
Newbie level 2
Joined
Mar 14, 2013
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,293
I have a problem with the usart of the atmega32a chip .
I write a program to send text to a serial port but I received the data in a different format like ĆĆĆĆĆĆĆĆ
please help me
this is my program


Code:
#include <avr/io.h>
#include <util/delay.h>
#define USART_BAUDRATE 9600
#define F_CPU 4000000
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)


void usart_init (void)
{
	 UCSRB |= (1 << RXEN) | (1 << TXEN);   
	 UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); 
	 UBRRL = BAUD_PRESCALE;
	 UBRRH = (BAUD_PRESCALE >> 8); 

}
void usart_send (unsigned char cht)
{
	while(! (UCSRA & (1<<UDRE)));
	UDR=cht;
}

int main(void)
{
	unsigned char str[30]="a b c d e f g h";
	unsigned char x;
	unsigned char strlength=30;
	unsigned char i=0;
	usart_init();
    _delay_ms(500);
	while(1)
	{  
		usart_send(str[i++]);
		if (i >= strlength)
		i = 0;
		
	}
	return 0;
}
 
Last edited by a moderator:

Is there MAX232 between MCU and PC? Are you sending data from MCU to hyperterminal? If yes, the baudrate in MCU code and hyperterminal is not matching or the data is inverted. MAX232 inverts the data.
 
In your code you have

Code C - [expand]
1
unsigned char str[30]="a b c d e f g h";

which is wrong. The characters should be seperated by commas and each character should be enclosed in single quotes like

Code C - [expand]
1
unsigned char str[30]={'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};

and if it is a string then you should terminate it with '\0' as the last element.
 
  • Like
Reactions: tpetar

    tpetar

    Points: 2
    Helpful Answer Positive Rating
In your code you have

Code C - [expand]
1
unsigned char str[30]="a b c d e f g h";

which is wrong. The characters should be seperated by commas and each character should be enclosed in single quotes

That is not wrong, it all depends on what you want to do.

Code:
unsigned char str[30]="a b c d e f g h";
is the equivalent of
Code:
unsigned char str[30]={'a',' ','b',' ','c',' ','d',' ','e',' ','f',' ','g',' ','h',0};
where 0 is the null termination ('\0')

- - - Updated - - -

By the way, character arrays should be declares as char to avoid warnings when you try to use the array with standard C functions that expect a char array
Code:
char str[30]="a b c d e f g h";
 

Yes. You are right alexan_e. I didn't see double quotes. I thought it as 'abcdefgh' and not "abcdefgh". My mistake. It is right to assign a string to a string variable and compiler adds the null character automatically to the end of the string.
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top