Hey there, I'm trying to read an incoming byte from the Terminal window on Atmel Studio. When I send a character it always reads to 255 in the RXCHR portion of RHR. It is transmitting just fine, the problem is just with the receive
#include <asf.h>
#include <stdio.h>
void uart_getchar(void);
static void uart_getString(uint8_t *c, int length);
static int uart_putchar(const uint8_t c);
static void uart_putString(uint8_t *c,int lenght);
int main (void)
{
SystemInit();
//Config UART Start
WDT->WDT_MR = WDT_MR_WDDIS;
//Disable port functions of required PA
PIOA->PIO_PDR=(PIO_PA9A_UTXD)|(PIO_PA8A_URXD);
//Select peripheral. A function between A and B
PIOA->PIO_ABSR=~((PIO_PA9A_UTXD)|(PIO_PA8A_URXD));
//Enable port functions of required PB
PIOB->PIO_PER=PIO_PER_P27;
//Declare required PB as output
PIOB->PIO_OER=PIO_OER_P27;
//Enable write protect of Peripheral Controller
PMC->PMC_WPMR=1;
//Enable required Peripherals clock
PMC->PMC_PCER0=(PMC_PCER0_PID8)|(PMC_PCER0_PID12);
//Enable pull-ups on RX and TX
//PIOA->PIO_PUER=(PIO_PA8A_URXD)|(PIO_PA9A_UTXD);
PIOA->PIO_PUER=(PIO_PA9A_UTXD);
PIOA->PIO_PUDR=PIO_PA8A_URXD;
//Disable pull-ups on outputs
PIOB->PIO_PUDR|=PIO_PUDR_P27;
//Enable required PB to be written directly
PIOB->PIO_OWER|=PIO_OWER_P27;
//Disable UART transfer
UART->UART_PTCR=(UART_PTCR_RXTDIS)|(UART_PTCR_TXTDIS);
//Set baud rate
UART->UART_BRGR=(SystemCoreClock / 9600) >> 4 ;
//Initially disable all the RX and TX and reset them
UART->UART_CR=(UART_CR_RSTRX)|(UART_CR_RSTTX)|(UART_CR_RXDIS)|(UART_CR_TXDIS);
//Set the Mode of communication
UART->UART_MR=(UART_MR_PAR_NO)|(UART_MR_CHMODE_NORMAL);
//Enable Transfer
UART->UART_PTCR=(UART_PTCR_RXTEN)|(UART_PTCR_TXTEN);
//Enable RX and TX
UART->UART_CR=(UART_CR_RXEN)|(UART_CR_TXEN);
//Config UART Stop
while(1)
{
delay_ms(1000);
uart_getchar();
delay_ms(1000);
}
return 0;
}
void uart_getchar(void)
{
if((UART->UART_SR & UART_SR_RXRDY)== 0) {
uart_putchar('
The thing is when I haven't sent any thing it will print $ in terminal window but when I send some character it will print "ÿ" which has ASCII 255 meaning the RXCHR is always full no matter what after sending the character. I'll be thankful if you could solve my problem.);
}
else if((UART->UART_SR & UART_SR_RXRDY) != 0)
{
uint8_t c = UART_RHR_RXCHR_Msk;
uart_putchar(c);
}
}
static int uart_putchar(const uint8_t c)
{
// Check if the transmitter is ready
// Send the character
UART->UART_THR = c;
while(!((UART->UART_SR) & UART_SR_TXEMPTY)); // Wait for the characters to be send
return 0;
}
static void uart_putString(uint8_t *c,int length)
{
int i = 0;
//unsigned long long int length =sizeof(c);
for(i=0; i<length; i++) {
uart_putchar(c[i]);
}
}
The thing is when I haven't sent any thing it will print $ in terminal window but when I send some character it will print "**ÿ**" which has ASCII 255 meaning the RXCHR is always full no matter what after sending the character. I'll be thankful if you could solve my problem.