0:08
hello and welcome to controllers Tech
0:12
this is the sixth video in The w25q
0:15
Flash series and today we will see how
0:18
to write and read floats integers and
0:21
also the 32-bit data in the previous
0:24
video we saw how to update the sectors
0:26
and how to read and write a single byte
0:31
we will utilize those functions in
0:34
here is the project from the previous
0:38
we will continue editing the source file
0:41
we have created so far
0:43
a while ago I wrote a code to interface
0:46
the eeprom with stm32
0:49
it has the code for writing the floats
0:51
and integers into the memory
0:53
actually we need to convert the numbers
0:55
into bytes first and then write the
0:58
bytes into the memory
1:00
we use the enems to do the conversion
1:03
let's copy the conversion functions from
1:05
here and paste it in the w25q source
1:10
now let's write a function to write the
1:12
numbers into the flash memory
1:14
the parameters of this function are the
1:16
start page the offset on the start page
1:19
and the float value to right
1:22
as I mentioned we need to convert the
1:24
floats to the byte and Float occupies
1:26
four bytes in the memory so let's define
1:29
an array of four bytes
1:31
inside the function we will first
1:33
convert the float value to the bytes and
1:36
it will be stored in the array we just
1:39
we can write the data using the right
1:41
byte function or using the sector update
1:44
function the choice is yours
1:46
I will write the code for both of them
1:48
here so use according to your
1:52
the right byte function takes the
1:54
address as the parameter so let's
1:56
calculate the address first
1:58
since we are writing 4 bytes of data we
2:01
need to call the right function in a for
2:04
the address must increment with every
2:10
writing the data using the sector update
2:13
method is going to be easier
2:16
we can pass the same parameters to the
2:20
the size is going to be 4 bytes and the
2:23
last parameter is the array we just
2:26
for now I am just commenting out this
2:29
function and we will write the data
2:31
using the byte function
2:32
now let's write another function to read
2:36
this function will return the float
2:38
value and the parameters are the start
2:43
since we need to read the 4 bytes for a
2:45
single float value let's define in an
2:48
array to store four bytes
2:50
now we will use the read function to
2:52
read the 4 bytes of data from the
2:54
provided page and offset
2:57
we have the four bytes now and we will
3:00
use the function bytes to float to
3:02
convert the bytes to the float value
3:04
this value will be returned in the end
3:09
let's define these functions in the
3:20
let me delete all the previous code from
3:27
I am going to test these functions in
3:30
sector 0 itself so let's erase it
3:33
let's define the float number that we
3:35
are going to write to the memory and
3:37
another float variable where we will
3:39
store the value read from the memory
3:42
when the button is pressed we will write
3:45
the value to page 0 at an offset of 10.
3:48
and let's also read the data from the
3:50
same position and store it in the float
3:55
all right let's build and debug the code
4:00
here the live expression I have added
4:04
let's run the debugger now
4:07
I am pressing the button and we have
4:13
this is the same as what we wrote to the
4:16
a little variation is expected in the
4:19
floats when they are stored
4:21
now let's test it using the sector
4:28
in order to write the integral value you
4:31
can just pass the integer value to the
4:47
here you can see we have received
4:50
exactly the same value in the live
4:53
basically we can store any number even
4:58
let's quickly test this
5:03
here we have received the same value in
5:08
so writing numbers works fine
5:11
we can store basically any number within
5:15
now let's see how to store the 32-bit
5:20
dot we will write a new function whose
5:22
parameters will be the start page the
5:25
offset on the start page the size of the
5:28
data and the pointer to the 32-bit array
5:31
we can't store the 32-bit values
5:34
directly to the memory the memory can
5:36
only store data in bytes
5:39
so we need to convert the 32-bit data to
5:41
8-bit data and hence each 32-bit
5:45
variable will take 4 bytes
5:48
so let's define a byte array whose size
5:50
will be four times the size of the
5:54
Define index variable to keep track of
5:57
the number of bytes in the byte array
6:00
now we will split the 32-bit data and
6:03
store it in the four different bytes of
6:11
after the data has been stored we will
6:14
write it for the memory
6:16
we will use the right function and pass
6:18
the parameters directly to this function
6:21
the index variable is equal to the
6:24
number of bytes we have stored so let's
6:26
pass it for the size parameter
6:31
now we will write another function to
6:36
the initial definitions are going to be
6:40
we will first read the data bytes from
6:42
the requested position
6:44
the size is going to be 4 times the size
6:50
now once we have the data in bytes
6:52
format we need to convert it to the
6:56
we can do this by Shifting the data
6:58
bytes and adding them together
7:03
let's build the code now
7:09
we have the Walling about the index
7:11
variable but just ignore it
7:14
Define these functions in the header
7:16
file so that we can use them in the main
7:22
here we will first Define a 32-bit array
7:27
I am storing four values which includes
7:30
8-bit value a 16-bit value a 32-bit
7:33
value and a random one
7:36
Define another 32-bit array to store the
7:40
now in the while loop when the button is
7:43
pressed we will write the data to page 0
7:49
then read the data from the same
7:51
location and store it in the receiver
7:54
all right let's build and debug the code
8:02
I am adding the r data array to the live
8:08
I press the button and you can see the
8:11
data received in the buffer let me
8:13
change the format to hex
8:15
you can see we have the same data that
8:18
we wrote to the memory
8:20
so the 32-bit variable was first
8:22
splitted into four bytes data and it was
8:25
then stored to the memory
8:27
later the four bytes were read from the
8:29
memory and then they were combined to
8:31
Output a 32-bit data this is it for this
8:36
we were able to store and read back the
8:38
float values the integral values and the
8:44
I hope you understood the video
8:46
the link to download the code is in the
8:51
leave comments in case of any doubt
8:53
keep watching and have a nice day ahead