sherazi
Banned

- Joined
- Feb 15, 2010
- Messages
- 388
- Helped
- 61
- Reputation
- 126
- Reaction score
- 61
- Trophy points
- 1,318
- Location
- Muscat, Oman, Oman
- Activity points
- 0
i am using two Atmega8 to communicate with each other using UART , but i am having problems at the receiver side, it gets some data all the time, even if the wire on another side is not connected . if i remove the wire it doesn't, have changed wires and even the circuit, had built three times on separate boards, but same problem is occurring, have checked the code and circuit using Proteus its all OK.
receiver code:
and transmitter code
receiver code:
Code:
#include <avr/io.h>
#include <util/delay.h>
#if !defined (F_CPU)
#define F_CPU 1000000
#endif
//#define RAMSTART 0x0060
//#define RAMSIZE (RAMEND-RAMSTART)
#define sleep()
// --------------
// --- USART0 ---
// --------------
void USART0_Transmit(unsigned char data)
{
while (!(UCSRA & (1<<UDRE))) ;
UDR = data;
}
void USART0_TransmitString(char *data)
{
while (*data) USART0_Transmit(*data++);
}
unsigned char USART0_Receive(void)
{
while (!(UCSRA & (1<<RXC))) ;//sleep();
return UDR;
}
void USART0_Init(void)
{
// USART0 settings: 2400 baud 8-n-1
// WARNING: real baud = 9615: err = 0.156249999999991%
UBRRH = 0;
UBRRL = 25;
UCSRB |= (1 << RXEN) | (1 << TXEN);
UCSRC = (1<<UCSZ1) | (1<<UCSZ0)|(1<<URSEL);
}
// -------------
// --- ports ---
// -------------
void Ports_Init(void)
{
DDRB = 255; // iiiiiiii
PORTB = 255; // iiiiiiii
DDRC = 255; // -iiiiiii
PORTC = 255; // -iiiiiii
DDRD = 2; // iiooooii
PORTD = 0; // iiiiiiii
}
int main()
{
Ports_Init();
PORTB=0xfe;
_delay_ms(200);
_delay_ms(200);
PORTB=0;
_delay_ms(200);
_delay_ms(200);
PORTB=0xfe;
_delay_ms(200);
_delay_ms(200);
PORTB=0;
_delay_ms(200);
_delay_ms(200);
PORTB=0xfe;
USART0_Init();
//sei(); // enable interrupts
char a;
uint8_t b,x,y,z;
b=1;
x=1;
y=1;
z=1;
int i=2;
while(1)
{
abc:
a=USART0_Receive();
if (a=='m')
{
a=USART0_Receive();
if (a=='e')
{
a=USART0_Receive();
if (a=='p')
{
a=USART0_Receive();
if (a=='r')
{
a=USART0_Receive();
if (a=='8')
{
PORTB^= 1 << 2;
}
else if (a=='2')
{
PORTB^= 1 << 3;
}
else if (a=='3')
{
PORTB^= 1 << 4;
}
else if (a=='9')
{
i++;
if(i%2==0)
PORTB=255;
else
PORTB=0;
}
goto abc;
}
else goto abc;
}
else goto abc;
}
else goto abc;
}
else goto abc;
}
}
and transmitter code
Code:
//TRANSMITTER :
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#if !defined (F_CPU)
#define F_CPU 1000000
#endif
//#define RAMSTART 0x0060
//#define RAMSIZE (RAMEND-RAMSTART)
// --------------
// --- USART0 ---
// --------------
void USART0_Transmit(unsigned char data)
{
while (!(UCSRA & (1<<UDRE))) ;
UDR = data;
}
void USART0_TransmitString(char *data)
{
PORTB=0;
_delay_ms(100);
while (*data) USART0_Transmit(*data++);
PORTB=255;
}
unsigned char USART0_Receive(void)
{
while (!(UCSRA & (1<<RXC))) ;//sleep();
return UDR;
}
void USART0_Init(void)
{
// USART0 settings: baud 8-n-1
// WARNING: real baud = 9615: err = 0.156249999999991%
UBRRH = 0;
UBRRL = 25;
UCSRB |= (1 << RXEN) | (1 << TXEN);
UCSRC = (1<<UCSZ1) | (1<<UCSZ0)|(1<<URSEL);
}
// -------------
// --- ports ---
// -------------
void Ports_Init(void)
{
DDRB = 0xff; // iiiiiiii
PORTB = 0xff; // iiiiiiii
DDRC = 0; // -iiiiiii
PORTC = 0; // -iiiiiii
DDRD = 2; // iiooooii
PORTD = 0; // iiiiiiii
}
int main()
{
Ports_Init();
USART0_Init();
//sei(); // enable interrupts
PORTB=0xfe;
_delay_ms(200);
_delay_ms(200);
PORTB=0;
_delay_ms(200);
_delay_ms(200);
PORTB=0xfe;
_delay_ms(200);
_delay_ms(200);
PORTB=0;
_delay_ms(200);
_delay_ms(200);
PORTB=0xfe;
while(1)
{
uint8_t a;
a=PIND;
a&=0x40;
if(a==0)
{
do{
a=PIND;
a&=0x40;
}
while (a==0);
USART0_TransmitString("mepr9");
}
a=PIND;
a&=0x10;
if(a==0)
{
do{
a=PIND;
a&=0x10;
}
while (a==0);
USART0_TransmitString("mepr2");
}
a=PIND;
a&=0x08;
if(a==0)
{
do{
a=PIND;
a&=0x08;
}
while (a==0);
USART0_TransmitString("mepr8");
}
a=PIND;
a&=0x20;
if(a==0)
{
do{
a=PIND;
a&=0x20;
}
while (a==0);
USART0_TransmitString("mepr3");
}
}
return 0;
}
Attachments
Last edited: