Go Down

Topic: ATSAM3X8E UART_RHR Problem (Read 161 times) previous topic - next topic

Musazal

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
Code: [Select]

#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('$');
}
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.

Musazal

I can't seem to edit my post so I'm replying for the edit but it still not works:
There is a bit mistake in the uart_getchar() function the edited version is as follows:
Code: [Select]

void uart_getchar(void)
{
// Check if the receiver is ready
if((UART->UART_SR & UART_SR_RXRDY)== 0) {
uart_putchar('$');
}
else if((UART->UART_SR & UART_SR_RXRDY) != 0)
{
uart_putchar('^');
unsigned char c =UART->UART_RHR;
uart_putchar(c);
}

and the last lines are as follows:
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 will '^' which confirms that something was received but then it will give the space and goes to the next line and starts over. I'll be thankful if you could solve my problem.

ard_newbie


Before sending a character, you have first to check that the transmitter is ready. And using interrupts to capture input characters would be a good idea.

See this blog for detailed info:

http://www.jaxcoder.com/Post/Index?guid=2cae5348-a4bc-403f-a309-77c66d0dc2d5

Go Up