0:23
welcome to controllers tech today in
0:27
we will see how can we send and receive
0:30
data from the computer
0:31
without using uart i will use the usb to
0:37
i have already made a video on it but
0:39
that video only covers the sending part
0:42
and this video will cover both the
0:44
sending and the receiving parts
0:47
let's start by creating the project in
0:50
first i am using stm32f103c8 controller
0:58
give some name to the project and click
1:02
here is our cube mx i am selecting the
1:06
external crystal for the clock first
1:09
select serial wire here now
1:13
in the usb select the device
1:18
in the usb device settings select the
1:21
class as communication device class
1:24
leave everything to default here i will
1:28
change this name to see if it shows up
1:36
that's all here now go to clock setup
1:40
i will set the clock manually i have
1:43
eight megahertz crystal
1:45
and i want the system to run at maximum
1:53
note here that usb clock is
1:55
automatically set to 48 megahertz
1:58
that's all for the setup click save to
2:08
the functions used to send or receive
2:10
data can be found in usbd
2:24
but if you look into the header file
2:26
there is only a function available
2:29
and that is to transmit data we will
2:32
modify the receive function in a while
2:37
here is the limit for the send and
2:39
receive buffer you can change them
2:41
if the data is quite big let's try to
2:45
some data to the computer first
2:51
this is the data i am going to send
2:59
we also need to include usbd cdc
3:03
if dot h as the function is available in
3:08
include string dot h also
3:17
now in the while loop we will transmit
3:22
the first parameter is the buffer to
3:27
and second is the length of the data
3:33
let's add some delay here build it and
3:55
it flashed successfully now before
3:58
connecting the controller
4:00
i am going to open the devices
4:09
and now as i connect it a new serial
4:14
it's not showing the name that i entered
4:18
that might be due to drivers in windows
4:21
10 it might show for you though
4:24
anyway the device is connected to com5
4:26
and i will open a serial port there
4:29
you can choose any board rate it works
4:32
for everything pretty much
4:35
you can see the data is being received
4:37
by the computer every one second
4:42
so the transmit function is working
4:48
now let's see how to receive data from
4:55
as i mentioned the function to receive
4:58
data is not available globally
5:06
so here i am defining an array of 64
5:09
bytes to store the data into
5:15
now i am going to give a reference of
5:17
this variable in the cdc
5:19
if dot c file so that we can use it here
5:27
in the cdc receive function i am going
5:31
to copy the data from the incoming
5:35
here the arguments are the buffer to
5:38
the buffer to copy from and the size of
5:42
mem copy takes size t as the parameter
5:45
so we need to do the conversion here
5:54
we are ready to receive the data now
6:00
i should use the debugger here since i
6:03
want to see the buffer
6:12
i have added the buffer in the live
6:25
as you can see the transmission is
6:26
working all right and the controller is
6:29
transmitting every second
6:31
when i send some data to the controller
6:34
it gets saved in the buffer
6:36
i will try transmitting something else
6:44
you can see the new data but there is a
7:02
the data is again received successfully
7:05
if i send this smaller data again you
7:08
see the buffer still have the data from
7:10
the previous transmission
7:19
to handle this situation i am going to
7:21
clear the buffer before receiving any
7:27
mem set can be used for it also make
7:31
sure you clear buff too
7:33
i have noticed that it also keeps the
7:35
old data stored in it
7:49
let's run the code again
7:58
i will add the buffer again
8:25
this time you can see the buffer only
8:27
contains the data we are sending
8:30
you can later parse this buffer
8:32
according to your requirement
8:34
since it can be accessed from the main
8:36
file itself there shouldn't be any
8:40
this video can be useful if you are
8:43
or any discovery board it can save you
8:47
the hectic of connecting the ttl device
8:50
for transferring data using uart
8:53
this is it for this video i hope you
8:56
understood the process
8:58
you can download the code from the link
9:03
keep watching and have a nice day ahead