Question

I am using the UART of Atmega169/AVR Butterfly for transmission to another board, baudrate 56700, no parity, 1 stopbit, no flow control. The oscillator is running at 7,3768Mhz (checked). I can transmit data successfully (checked with the other board and PC/HyperTerminal), but not receive any data - when running the debugger the configuration bits are all set correctly, but RXC is false constantly - I also checked if I can send data to myself (connected TXD to RXD and grounded), but without success. (Tried with ISR as well as polling) Below are the relevant parts of the code, I hope you can deal with it - PORTB is used as output for testing with the oscilloscope (I know I could just use one pin, but there is nothing else on PORTB right now):

int main(void){
OSCCAL_Calibrate();       // calibrate the internal oscillator
int UBRR_VAL  = ((F_CPU)/(BAUD*16)-1);
UART_Init(UBRR_VAL);
DDRB |= 0xFF;
PORTB = 0;
testCharSend();

while(1);
return 0;
}

void testCharSend()
{
char i = 'x';
while(1){       
    Uart_Tx(i);
}
}

void UART_Init(unsigned int baudrate)
{
    // Set baud rate
    UBRRH = (unsigned char)(baudrate>>8);
    UBRRL = (unsigned char)baudrate;

    UCSRA = 0;

    // Enable receiver and transmitter
    UCSRB = (1<<RXEN)|(1<<TXEN);

    // Async. mode, 8bit, No parity, 1 stop bit (like CC2540)
    UCSRC = (0<<UMSEL)|(0<<UPM0)|(0<<USBS)|(3<<UCSZ0)|(0<<UCPOL);

// enable receive interrupt
UCSRB |= (1<<RXCIE);

// flush UART
UART_Flush();
}

void UART_Flush( void )
{
unsigned char  dummy;
while ( UCSRA & (1<<RXC) ) dummy = UDR;
}

void Uart_Tx(char data)
{
    while (!(UCSRA & (1<<UDRE)));
    UDR = data;
}

ISR (USART0_RX_vect)
{
    PORTB ^= 0xFF;
char c = UDR;
}
Was it helpful?

Solution

OK, I tested the connections with an oscilloscope, the RXD line on the board was broken, switched the board and now it's working, so the code above is valid!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top