Recording the data recieved from UART

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Recording the data recieved from UART

跳至解决方案
4,086 次查看
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

标签 (1)
0 项奖励
回复
1 解答
4,038 次查看
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

在原帖中查看解决方案

7 回复数
4,057 次查看
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

 

 

4,052 次查看
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 项奖励
回复
4,053 次查看
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 项奖励
回复
4,039 次查看
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,940 次查看
Alex_2221
Contributor IV

@xiangjun_rong Thanks a lot, man. It worked

0 项奖励
回复
4,032 次查看
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 项奖励
回复
4,083 次查看
Alex_2221
Contributor IV

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

0 项奖励
回复