Recording the data recieved from UART

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Recording the data recieved from UART

Jump to solution
3,880 Views
Alex_2221
Contributor IV

Hello everyone, in my project, I'm sending temperature via UART and want to recieve a 2 digit number and record it in a variable. I'm able to read data and echo it back, but as far as I understood I need to recieve a two bytes of data and I'm receiving only one byte everytime UART interrupt works. Plus I wanted to see the content of the variable, in which I save the data and it's only the left most digit of the number I'm sending and the ASCII equivalent of it. For example, If I send 15, my debugger shows me something like (49' 1) and as you may know 49 is the ASCII form of 1 and when I run my code I can recieve back my right most digit in ASCII for example, for 15 I get 53 which is the ASCII form of 5. I also try to send back the data together with the temperature by UART_Send function, and in this case I only can see the ASCII form of the right most digit. So, I'm now able to only recieve the right most digit of the numbers I send and they're in ASCII what can I do to save both digits in decimal in a variable?

In order that you better understand what I'm saying, I added the screenshot of my debugger, my serial port terminal, and my main.c file, so that you can see my code.

Your help will be greatly appreciated,

Sincerely, 

Alex

Labels (1)
0 Kudos
1 Solution
3,832 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

Regarding your following code, I think you can define an array.

//global variable

#define N 100

char array[N];

uint32_t index=0;

void UART_IntReceive(void)
{


data[index]=UART_ReceiveByte(LPC_UART3);

index++;

if (index>==N) index=0;
//I do not suggest you call UART_SendByte() in the receiver ISR
//UART_SendByte(LPC_UART3,data);

}

Is it what you expected?

BR

XiangJun Rong

View solution in original post

7 Replies
3,851 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

For usart module, it can transmit or receive only ONE byte, pls refer to the usart data registers:

xiangjun_rong_0-1637571773371.png

 

So the Usart will trigger interrupt after it has received/transmitted a Byte.

I see that your data is half word(16 bits), I think you can transfer the half word(temperature variable) with two byte.

uint16_t temperature;

char byte_L, byte_H;

void halfWord(uint16_t var)

{

byte_L=(char)(var&0xFF);

byte_H=(char)((var>>8)&0xFF);

UART_SendByte(LPC_UART3,byte_L);

UART_SendByte(LPC_UART3,byte_H);

UART_SendByte(LPC_UART3,0xFF);

UART_SendByte(LPC_UART3,0xFF);

}

I suggest you delete the line  uint8_t len=sprintf((char*)buffStr,"Hello.Temp is %u\n %u\n",temp, data1);

You can transfer data directly. The 0xFFFF is a separator or mark so that the receiver know which is high byte which is low byte.

Hope it can help you

BR

XiangJun Rong

 

 

3,846 Views
Alex_2221
Contributor IV
  • @xiangjun_rong Thank you very much for your time and your reply. However, I can send the temperature, which is 2 bytes fine. My problem is I cannot save the data I enter via serial port using uart_receivebyte in the interrupt! How can I save the data which is 2 bytes ( 2 digit number string)?
0 Kudos
3,847 Views
Alex_2221
Contributor IV

@xiangjun_rong Thank you very much for your time and your reply. However, I can send the temperature, which is 2 bytes fine. My problem is I cannot save the data I enter via serial port using uart_receivebytr in the interrupt! How can I save the data which is 2 bytes ( 2 digit number string)?

0 Kudos
3,833 Views
xiangjun_rong
NXP TechSupport
NXP TechSupport

Hi,

Regarding your following code, I think you can define an array.

//global variable

#define N 100

char array[N];

uint32_t index=0;

void UART_IntReceive(void)
{


data[index]=UART_ReceiveByte(LPC_UART3);

index++;

if (index>==N) index=0;
//I do not suggest you call UART_SendByte() in the receiver ISR
//UART_SendByte(LPC_UART3,data);

}

Is it what you expected?

BR

XiangJun Rong

3,734 Views
Alex_2221
Contributor IV

@xiangjun_rong Thanks a lot, man. It worked

0 Kudos
3,826 Views
Alex_2221
Contributor IV

Yeah absolutely, it's what I expected and I think it makes sense. Thank you so much, I'll give it a try.  

0 Kudos
3,877 Views
Alex_2221
Contributor IV

@xiangjun_rong Your replies always helped me. I always value your opinion.

0 Kudos