Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Stm32 and NRF24L01

Alireza770717

Junior Member level 3
Junior Member level 3
Joined
Nov 21, 2023
Messages
25
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
153
Hello friends
have a good time
I have made one transmitter and one receiver with stm32 microcontroller and NRF
In the transmitter, I send a number to stm using the keypad, which is defined as follows
(for example) int key = 5;
Then the number 5 itself is given and sent to NRF through the following function
sprintf(tx_data,"%c",key);

{
tx_data;
}
nrf24l01p_tx_transmit( tx_data );
(I think the desired number is stored as a character in tx_data.)
Also, tx_data is defined as follows:

volatile uint32_t tx_data[NRF24L01P_PAYLOAD_LENGTH] = {};

in the receiver as well
volatile uint32_t rx_data[NRF24L01P_PAYLOAD_LENGTH] = {};
it will be received.
Now I want to receive my received in the form of an integer and perform mathematical operations.
I did everything to convert it to integer in both the sender and the receiver, but I did not get an answer.

I will give you my codes if you want.
nrf library from the following link:

https://github.com/mokhwasomssi/stm32_hal_nrf24l01p

Thank You
 
Hi,

I´m no C expert ... but when I read your code, then I expect it to work like this:

* I dont know what data type "key" is... (it´s very important to know, not only for us, but also for you)
* if you assign key = 5 then the binary value should be: 0b ???????? 00000101 (the "?" are because of the unknown data type of "key" I don´t know how many bits to show)
* now you define tx_data as UINT32 ... which - for me - is quite uncommon. Usually SPRINTF expects an array of type CHAR. (Read your sprintf documentation!!!)
* now SPRINTF(%c) usually transforms the integer input value into digits, translates each digit to the according ASCII value, places them into the output array and adds the delimiter 0x00.
* so in your case, if you use CHAR as data type, the result should be [0x35], [0x00], two bytes in the array.
(in case key = 234, the result would be [0x32], [0x33], [0x34], [0x00] = four bytes)
* indeed I´m not sure how SPRINTF treates the 32 bit wide array.
* now I´m confused what you try to do with
Code:
{
         tx_data;
}
it makes no sense to me.
* next is: [nrf24l01p_tx_transmit( tx_data );] it sends data via NRF. But whether the syntax and the data type is correct tells you the NRF_library documentation.
* the same is true for the receive fucntion.

***
I´m not sure wheter you need to transfer the value as ASCII at all. What makes you think you need to?

***
My recommendation: Read the documentations.
We can not know what libraries you use ... and if we knew .. we need to read the documentation, too.

According this documentation draw a sketch of data flow .. with all necessary informations, so that every one has all informations to write the according code.

***

Klaus
 
Hello!

It would also be nice to have an accurate description of what you want to doe.
So you have some kind of transceiver NRF401 and STM32.

I have made one transmitter and one receiver with stm32 microcontroller and NRF
In the transmitter, I send a number to stm using the keypad, which is defined as follows
(for example) int key = 5;

Does it mean a chain like this: (the _S and _R mean send and receive)

[KEYPAD] - [STM32_S] - [NRF401_S] ---------- RADIO WAVES --------- [NRF401_R] - [STM32_R]

So you get some key code from the keypad and you want to send it with NRF401_S so that it's
received by [NRF401_R], decoded by STM32_R which interprets the command you have sent?

About what you wrote, and beside what Klaus already said, be aware that sprintf uses quite some
resources. Depending on which STM32 you are using, it might be a huge CPU / memory load.
Supposing you are using a buffer of bytes like this:

uint8 txbuf[BUF_LEN]

Then if you want to sent the ASCII character for 5, then instead of sprintf, you can simply
write: txbuf[0] = 0x30 + key; and possibly txbuf[1] = 0;
Building your frame this way will be done in a few clocks and it's the same.

Now I want to receive my received in the form of an integer and perform mathematical operations.
I did everything to convert it to integer in both the sender and the receiver, but I did not get an answer.

You don't necessarily have to convert. You can send any sequence over some connection,
so you can also directly send binary.
BUT: if you send binary directly, you have to decide a frame format. One way would be
to always send 4 bytes. If you simply want a 8 bit values, then you send 3 bytes for nothing,
but anyway as stated above, sending in ASCII would also cost you 4 bytes. The big advantage
is that if you send an int32, you also need 4 bytes, not more.
Or you can also define your own format. For instance, 1st byte is the payload length, then this
allows you to use a variable frame format. And if you want to send a byte, you need 2 bytes
only, which is more efficient than ASCII.

Have fun!

Dora.
 
Hi,

I´m no C expert ... but when I read your code, then I expect it to work like this:

* I dont know what data type "key" is... (it´s very important to know, not only for us, but also for you)
* if you assign key = 5 then the binary value should be: 0b ???????? 00000101 (the "?" are because of the unknown data type of "key" I don´t know how many bits to show)
* now you define tx_data as UINT32 ... which - for me - is quite uncommon. Usually SPRINTF expects an array of type CHAR. (Read your sprintf documentation!!!)
* now SPRINTF(%c) usually transforms the integer input value into digits, translates each digit to the according ASCII value, places them into the output array and adds the delimiter 0x00.
* so in your case, if you use CHAR as data type, the result should be [0x35], [0x00], two bytes in the array.
(in case key = 234, the result would be [0x32], [0x33], [0x34], [0x00] = four bytes)
* indeed I´m not sure how SPRINTF treates the 32 bit wide array.
* now I´m confused what you try to do with
Code:
{
         tx_data;
}
it makes no sense to me.
* next is: [nrf24l01p_tx_transmit( tx_data );] it sends data via NRF. But whether the syntax and the data type is correct tells you the NRF_library documentation.
* the same is true for the receive fucntion.

***
I´m not sure wheter you need to transfer the value as ASCII at all. What makes you think you need to?

***
My recommendation: Read the documentations.
We can not know what libraries you use ... and if we knew .. we need to read the documentation, too.

According this documentation draw a sketch of data flow .. with all necessary informations, so that every one has all informations to write the according code.

***

Klaus
Hi Klaus
Thanks for your answer
I don't use ASKII code for numbers because I want to do math operations on numbers due to the need for multi-digit numbers.
I want to use this receiver to take data from the user using a single-digit or two-digit or multi-digit number keypad, then show it on the LCD and send it to the receiver using NRF.
On the receiver side, display the value on the LCD and perform math operations on the received.


{
tx_data;
}
This code is based on the library received from github
Hello!

It would also be nice to have an accurate description of what you want to doe.
So you have some kind of transceiver NRF401 and STM32.



Does it mean a chain like this: (the _S and _R mean send and receive)

[KEYPAD] - [STM32_S] - [NRF401_S] ---------- RADIO WAVES --------- [NRF401_R] - [STM32_R]

So you get some key code from the keypad and you want to send it with NRF401_S so that it's
received by [NRF401_R], decoded by STM32_R which interprets the command you have sent?

About what you wrote, and beside what Klaus already said, be aware that sprintf uses quite some
resources. Depending on which STM32 you are using, it might be a huge CPU / memory load.
Supposing you are using a buffer of bytes like this:

uint8 txbuf[BUF_LEN]

Then if you want to sent the ASCII character for 5, then instead of sprintf, you can simply
write: txbuf[0] = 0x30 + key; and possibly txbuf[1] = 0;
Building your frame this way will be done in a few clocks and it's the same.



You don't necessarily have to convert. You can send any sequence over some connection,
so you can also directly send binary.
BUT: if you send binary directly, you have to decide a frame format. One way would be
to always send 4 bytes. If you simply want a 8 bit values, then you send 3 bytes for nothing,
but anyway as stated above, sending in ASCII would also cost you 4 bytes. The big advantage
is that if you send an int32, you also need 4 bytes, not more.
Or you can also define your own format. For instance, 1st byte is the payload length, then this
allows you to use a variable frame format. And if you want to send a byte, you need 2 bytes
only, which is more efficient than ASCII.

Have fun!

Dora.
Hi

If I use ASKII code, how can I convert the data to decimal numbers by micro, because I want to display it on the LCD and perform mathematical operations.

Thank you for your time
 
Hi,

I don't use ASKII code for numbers
Your code says different!
With the SPRINTF you surely generate ASCII code. That´s the job of SPRINTF, It generates printable (human readable) characters.

***

and regarding the "mathematical operation" problems....
you need to know (as already mentioned)
* the type and range of your input value (you store in "key")
* and read all library documentations, especially on data types and ranges

No one can do this for you. We simply don´t have the informations.

You need to give use these informations to enable us to help you.

***

In any case, as soon as you transfer multi_byte informations you need to use some technique to ensure proper alignment at the receiver side.
Let´s say you have an UINT16 range (0...65535 decimal = 0x0000 to 0xFFFFF)
Now let´s say you want to transferthese values: 0x1234, 0x2A45, 0x63C8, 0x8254, 0xE589, ..
Then the bytewise datastream via the interface looks like this(assuming HIGH byte first): 0x12, 0x34, 0x2A, 0x45, 0x63, 0xC8, 0x82, 0x54, 0xE5, 0x89 ...

Even if you power up both devices at exactly the same time .. it may be that the receiver is a bit slower to boot and thus misses the first byte.
Then the receiver sees: 0x34, 0x2A, 0x45, 0x63, 0xC8, 0x82, 0x54, 0xE5, 0x89 ...
How do you know which byte is the HIGH byte and which is the LOW byte of the 16 bit variable? Impossible

you always need something like this:
0x34, [?], 0x2A, 0x45,[?], 0x63, 0xC8,[?], 0x82, 0x54,[?], 0xE5, 0x89,[?], ...

the [?] may be a gap in time, or a special byte or any other coding technique.. but it needs to be defined. Defined by you.

Klaus
 
Hi,


Your code says different!
With the SPRINTF you surely generate ASCII code. That´s the job of SPRINTF, It generates printable (human readable) characters.

***

and regarding the "mathematical operation" problems....
you need to know (as already mentioned)
* the type and range of your input value (you store in "key")
* and read all library documentations, especially on data types and ranges

No one can do this for you. We simply don´t have the informations.

You need to give use these informations to enable us to help you.

***

In any case, as soon as you transfer multi_byte informations you need to use some technique to ensure proper alignment at the receiver side.
Let´s say you have an UINT16 range (0...65535 decimal = 0x0000 to 0xFFFFF)
Now let´s say you want to transferthese values: 0x1234, 0x2A45, 0x63C8, 0x8254, 0xE589, ..
Then the bytewise datastream via the interface looks like this(assuming HIGH byte first): 0x12, 0x34, 0x2A, 0x45, 0x63, 0xC8, 0x82, 0x54, 0xE5, 0x89 ...

Even if you power up both devices at exactly the same time .. it may be that the receiver is a bit slower to boot and thus misses the first byte.
Then the receiver sees: 0x34, 0x2A, 0x45, 0x63, 0xC8, 0x82, 0x54, 0xE5, 0x89 ...
How do you know which byte is the HIGH byte and which is the LOW byte of the 16 bit variable? Impossible

you always need something like this:
0x34, [?], 0x2A, 0x45,[?], 0x63, 0xC8,[?], 0x82, 0x54,[?], 0xE5, 0x89,[?], ...

the [?] may be a gap in time, or a special byte or any other coding technique.. but it needs to be defined. Defined by you.

Klaus
Hello Klaus
I have no problem with these things that I wrote and said, my codes work well, my transmitter and receiver also work, my only problem is math operations, because it receives as characters, I can't do math operations. Thank you very much for your reply though
 
Hi,

Again:

* what data type and range is the input value ("key") ?
* what is your delimiter between numbers in your string?

I´m back when there are clear answers.

Klaus
 
Hi,

Again:

* what data type and range is the input value ("key") ?
* what is your delimiter between numbers in your string?

I´m back when there are clear answers.

Klaus
Hello, after a few days

The value of my key is the number that the user enters through the keyboard, starting from 1 to 100



Thank you very much for your advice


My problem was solved with your help, friends, by removing sprintf and converting uint32_t to uint8_t
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top