atmega128a uart1 transmitting but it doesn't receive

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

hello

i'm using atmega128a in a project i have a problem with UART1 , i'm using external crystal 16MHZ the problem is that i'm able to transmit from the atmega128a to my pc using USB TTL and tera term but i can't send any commands from my PC to the atmega128a

but in case that i'm connecting the TX1 with RX1 it works fine

my frame is 8N1 and my baud rate on the tera term is 9600

here is my code

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

#define MYUBRR 103

void USART1_Init( unsigned int ubrr );
void USART1_Transmit( unsigned char data );
unsigned char USART1_Receive( void );

int main ()
{    DDRA=0xFF;
    DDRD|=0xff;
    USART1_Init(MYUBRR);
    char y=0;
    while(1)
    {
      
        USART1_Transmit('1');
        y=USART1_Receive();
        if(y=='1'){
        PORTA=0xFF;
        PORTD=0xFF;
        _delay_ms(1000);
        PORTA=0x00;
        PORTD=0x00;
        _delay_ms(1000);}

    }
    return 0;
}

void USART1_Init( unsigned int ubrr )
{
     UCSR1B |= (1 << RXEN1) | (1 << TXEN1);
     UCSR1C |= (0 << UMSEL1) | (1 << UCSZ10) | (1 << UCSZ11);
     UBRR1H =(unsigned char)(ubrr>>8);
     UBRR1L =(unsigned char)ubrr;
}

void USART1_Transmit( unsigned char data )
{
    /* Wait for empty transmit buffer */
    while ((UCSR1A & (1 << UDRE1)) == 0) {};
          UDR1 = data;
}

unsigned char USART1_Receive( void )
{
    /* Wait for data to be received */
    while ( !(UCSR1A & (1<<RXC1)) )
    ;
    /* Get and return received data from buffer */
    return UDR1;
}

 

Last Edited: Fri. Apr 20, 2018 - 01:40 AM
  • 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]

 

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

And post in the correct forum please. Moving.

 

Have you turned off the Mega103 compatibility fuse?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Fri. Apr 20, 2018 - 01:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you defined F_CPU anywhere?

   (It is needed by _delay_ms(), otherwise will "assume" 1MHz)

 

How are the  fuses programed?

How often do PORTA and PORTF toggle when RX1 is connected to TX1?

David (aka frog_jr)

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

amrbadr wrote:
but in case that i'm connecting the TX1 with RX1 it works fine
Connecting where? On the PC usbt <==> uart or on the AVR side?

 

One way to check for shorts is to connect RxD and TxD on the USB <==> Uart dongle together and connect both to the RxD pin of your AVR.

If the data does not get corrupted you have a good chance that there is no short circuit.

Are you sure GND is connected properly?

 

If you can send data from the AVR to the PC, then the baudrate settings should be all right.

 

A better way to check for received data on the AVR is to make it "catch all".

Use a piece of code like:

 

unsigned char USART1_Receive( void )
{
    static uint8_t Cnt;         // static wil preseve contents between calls.
    
    if( UCSR1A & (1<<RXC1)) {
        PORTD = ++Cnt;          // A change on any received byte.
        PORTA = UDR1;           // Echo of last received byte to a port.
    }
    return PORTA;
}

 

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

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

BTW I added a link the other way round but just realized that I forgot to add a link here to say this thread is a continuation of a previous one the OP abandoned here:
.
https://www.avrfreaks.net/forum/disabling-atmega103-compatibility-mode-atmega128

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

yes i turned it off