Search code examples
catmega

How do i use the Serial rx/tx on my atmega2560


#include <atmel_start.h>
#include <util/delay.h>
#include <avr/io.h>
#define F_CPU   8000000
#define BAUD    9600
#define BRC     ((F_CPU/(16UL * BAUD)) - 1)
void USART_Transmit_2( unsigned char data )
        {
            /* Wait for empty transmit buffer */
            while ( !( UCSR2A & (1<<UDRE2)) )
            ;
            
            UDR2 = data;/* Put data into buffer, sends the data */
            PORTB = 0b00100000; /* Turn on first LED bit/pin in PORTB */
            _delay_ms(500); /* wait */
            PORTB = 0b00000000; /* Turn off all B pins, including LED */
            _delay_ms(500);
        }
void USART_init_2(void){
        UBRR2H = (BRC >> 8);
        UBRR2L = BRC;
        
        UCSR2B = (1 << TXEN2) | (1 << RXEN2);
        UCSR2C = (1 << UCSZ21) | (1 << UCSZ20);
}
int main(void)
{
    
    /* Initializes MCU, drivers and middleware */
    //atmel_start_init();
    
    USART_init_2();
    DDRB = 0b00000001;
    /* Replace with your application code */
    while (1) {
        
        USART_Transmit_2('h');
        _delay_ms(1000);
        
    }
}

This is my barebone code, i can't figure out why there is no signal coming out from the pin. On TX the signal is always high.

the lights in USART_Transmit_2 do turn on and off.

Any help would be greatly appreciated.


Solution

  • so I am a complete fool, somehow i was not checking the right channel, the code is working as is on rx/tx 3 instead of rx/tx 2. not really sure why as the datasheet says otherwise but hey, it works now.