my first C application

Hi all,

I have programmed some fairly simple applications in the ARDUINO IDE.
However I want to really understand what is going on behind the source code.
Therefor I want to try and make an embedded application using Atmel studio (6.2).
I have an Arduino due for which I created the attached C code.
The blinking orange led is working fine. However I can't get the USART0 to work.
I attached an logic analyzer to the TX1 (pin18) of the board but nothing is happening on the pin.

Could someone help me find the problem?

Also the datasheet is mentioning write protected registers but in the source of the original arduino libraries i can't find any use of them. Are they not required to change registers?

thanks in advance.

main.c (2.25 KB)

For small amounts of code, please just post it, don't attach it

/**
 * \file
 *
 * \brief Empty user application template
 *
 */

/**
 * \mainpage User Application template doxygen documentation
 *
 * \par Empty user application template
 *
 * Bare minimum empty user application template
 *
 * \par Content
 *
 * -# Include the ASF header files (through asf.h)
 * -# "Insert system clock initialization code here" comment
 * -# Minimal main function that starts with a call to board_init()
 * -# "Insert application code here" comment
 *
 */

/*
 * Include header files for all drivers that have been imported from
 * Atmel Software Framework (ASF).
 */
/*
 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
 */
#include <asf.h>

static void usart_init(void)
{
	//enable peripheral clocks.
	PMC->PMC_PCER0 |= PMC_PCER0_PID17 | PMC_PCER0_PID11; // enable PIOA/USART0 peripheral clock
		
	//configure PA11 for TXD0
	PIOA->PIO_PDR |= PIO_PDR_P11; //enable USART pin control
	PIOA->PIO_ABSR &= ~(PIO_PDR_P11);//select peripheral A (TXD0)
		
	USART0->US_CR = (UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS) ;
	USART0->US_IDR = 0xFFFFFFFF;
	USART0->US_BRGR = 547; //9600baud
	USART0->US_MR = (US_MR_USART_MODE_RS485|US_MR_USCLKS_MCK|US_MR_CHRL_8_BIT|US_MR_NBSTOP_1_BIT|US_MR_CHMODE_NORMAL);
	USART0->US_CR |= UART_CR_TXEN ;
}

int main (void)
{
	/* Insert system clock initialization code here (sysclk_init()). */

	//board_init();
	
	PIOB->PIO_IDR |= PIO_IDR_P27; //disable interrupt 
	PIOB->PIO_PER |= PIO_PER_P27; //assign PIO controller
	PIOB->PIO_OER = PIO_OER_P27; //enable output driver
	
	usart_init();

	uint32_t usart_data = 0;
	while(USART0->US_CSR & US_CSR_TXRDY != US_CSR_TXRDY);
	
	USART0->US_THR = usart_data;
	
	bool SetOutputHigh = false;
		
	while(1)
	{
		if(USART0->US_CSR & US_CSR_TXEMPTY == US_CSR_TXEMPTY)
		{
			usart_data++;
			USART0->US_THR = usart_data;
		}
		
		if (SetOutputHigh)
		{
			PIOB->PIO_SODR = PIO_SODR_P27; //set output high
			SetOutputHigh = false;
		} 
		else
		{
			PIOB->PIO_CODR = PIO_CODR_P27; //set output high
			SetOutputHigh = true;
		}
		
		for(long i=0;i<1000000;i++)
		{
			__NOP();
		}		
	}
	/* Insert application code here, after the board has been initialized. */
}