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.

UART Problem : between atmega32 and atmega8

Status
Not open for further replies.

engallam

Junior Member level 1
Joined
May 26, 2014
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
313
Hi I'm try to connect atmega32a with atmega8a using UART protocal but...

(in simulation)
- when I send byte from atmega32 to atmega8, the connection work good
- when I send byte from atmega8 to atmega32,the connection not work

(in real)
- the data recived by atmega8 (transmuted by atmega32) , be wrong data (Ex: when i send (1) from atmega32 the data received by atmega8 be (24)

- when I send byte from atmega8 to atmega32, the connection not work


what the problem ??!



code of atmega32a

Code:
#define  F_CPU 16000000
#include <avr\io.h>
#include <avr\pgmspace.h>
#include <avr\interrupt.h>
#include <util\delay.h>
#include <math.h>

#define ButtonPort		PORTA			// Buttons port
#define ButtonDir		DDRA			// Buttons Direction
#define ButtonPin		PINA			// Buttons port
#define Down			0			// Down Button
#define Up				1			// Up Button
#define Right			2			// Right Button
#define Left			3			// Left Button
#define Run				4			// Run Button
#define Back			5			// Back Button

int main(void) 
{
	DDRB=0xff;	
	PORTB=0x00;
	ButtonDir=0x00;		
	ButtonPort=0xff;
	uint16_t baud = 2400;


	uint16_t UBRR_Value = lrint (( F_CPU / (16 * baud) ) - 1); 
		
	UBRRL = (uint8_t) UBRR_Value;
	UBRRH = (uint8_t) (UBRR_Value >> 8);
	UCSRB = (1<<RXEN) | (1<<TXEN);  
	
    UCSRC |= (1 << UPM1);	//Sets parity to EVEN
	UCSRC |= (3 << UCSZ0);	//8-bit data length

	while (1)
	{

		if (bit_is_clear(ButtonPin,Up))
		{
			_delay_ms(500);
			while( ! (UCSRA & (1<<UDRE) ) );
			UDR = 1;
		}
		if (bit_is_clear(ButtonPin,Down))
		{
			_delay_ms(500);
			while( ! (UCSRA & (1<<UDRE) ) );
			UDR = 2;
		}
		if (bit_is_clear(ButtonPin,Right))
		{
			_delay_ms(500);
			while( ! (UCSRA & (1<<UDRE) ) );
			UDR = 4;
		}
		if (bit_is_clear(ButtonPin,Left))
		{
			_delay_ms(500);
			while( ! (UCSRA & (1<<UDRE) ) );
			UDR = 8;
		}
		if (bit_is_clear(ButtonPin,Run))
		{
			_delay_ms(500);
			while( ! (UCSRA & (1<<UDRE) ) );
			UDR = 16;
		}
		

		while (! (UCSRA & (1 << RXC)) );
			PORTB= UDR; 
		 _delay_ms(500);
	}

}

code of atmega8a

Code:
#define  F_CPU 16000000
#include <avr\io.h>
#include <avr\pgmspace.h>
#include <avr\interrupt.h>
#include <util\delay.h>
#include <math.h>



int main(void) 
{
	DDRB=0xff;	
	PORTB=0x00;

	uint16_t baud = 2400;
	char data=5;	

	uint16_t UBRR_Value = lrint (( F_CPU / (16 * baud) ) - 1); 
		
	UBRRL = (uint8_t) UBRR_Value;
	UBRRH = (uint8_t) (UBRR_Value >> 8);
	UCSRB = (1<<RXEN) | (1<<TXEN);  
	
        UCSRC |= (1 << UPM1);	//Sets parity to EVEN
	UCSRC |= (3 << UCSZ0);	//8-bit data length

	while (1)
	{
		while (! (UCSRA & (1 << RXC)) );
			PORTB= UDR; 
		 _delay_ms(500);

		while( ! (UCSRA & (1<<UDRE) ) );
			UDR = data;
		_delay_ms(500);
	}

}
 

At a glance, the first thing that trikes the eyes is the addition of a delay in the serial pooling routine; In real world it could even work if you tap each button once at long intervals (assuming that there is a debouncer in hardware), but surelly would not work in simulation, where the things happens in 'slow motion' from our perspective. I did not look at the remaining code after that.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top