Serial UART with STM32 Microcontroller-Transmit and Receive Data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's learn how to use serial uward communication with stm32 microcontroller we will transmit and receive data between microcontroller and the personal computer and finally we will control LED from serial terminal software over uart interface so let's create a fresh new stm32 Pro thanks to Altium software for sponsoring this video the cloud-based RTM 365 offers seamless collaboration between Engineers along with Version Control capability Altium is all in one platform for all the needs of PCB design feel free to check my Altium tutorial series to learn more you can download free trial with the link given in a video description also you can sign up with the link altium.com forward slash YT slash binary updates go to file on top left corner click on new and click on stm32 Project now we have to select the microcontroller stm32 f446re that's our Target microcontroller so select the nuclear board stm32f446 if I can expand then you can see the part number f46re on a nuclear board click on next and now we have to give the name to the project I would like to call it as a UR universal asynchronous receiver transmitter and then click on finish to create a project so this will take a couple of minutes now you can say uart or USA RT use art both are the same uart is basically universal asynchronous receiver transmitter and use art is basically Universal synchronous asynchronous receiver transmitter so I usually call it as a uart because I often use this asynchronous communication between a microcontroller and the personal computer but basically we are using the use art peripheral so now you can see the project is configured and we will be presented with pinout in configuration you can see if I can zoom in you can see the mic controller here and if I can expand this little further you can able to see we have a connectivity so if I can expand connectivity now you can see we have a several different you want here use art you want both are the same thing I would select user2 that's basically use as the USB connector on stm32 nuclear bot so we will select use R2 and then in a mode drop down I select asynchronous that's why I give it the name to the project as a uart you can see in onto the project pen you want and that's why I select asynchronous universal asynchronous receiver transmitter and now you can see here we have a couple of parameters I would like to take you to the parameter settings and you can see the communication the serial communication serial uward communication has a baud rate 115200 by default so 11 5200 bits per second or the symbols per second is the speed of communication of this serial communication and then the word length is 8-bit this is by default you can able to see I can move this little further so you can see the microcontroller here and as soon as I select asynchronous here you can able to see the pa2 pin is selected as a transmitter TX and pa3 pin is selected as a receiver RX now because we will be using an interrupt receive interrupt on uart communication we have to go to the nvx setting nested Vector interrupt controller and you can see here we have a user to Global interrupt to turn on the interrupt on uart or user 2 we have to check this box that will enable the interrupt on the uart pin and now because we have a built-in LED on stm32 nuclear board on pa5 pin just click on this pin and then configure as output because when we will be doing a receiver we will turn on and off the led by sending a data from the serial terminal console so we will use this in future that's why I just configure this pin PFI pin into the output mode once this is done we can click on this button here on a top menu bar device configuration tool code generation or you can just click on Save and it will create a project so click on this button and click on yes or you can press Ctrl s to save and then it will automatically generate the code for us and as you can see the project is configured and main.c file shows up on our screen now first thing first we have to transmit the data from our microcontroller to the personal computer and we will send a text message from the microcontroller to our computer and that's why we will Define a variable here and the variable is basically a buffer so I would just say U int 8 underscore T so basically it's an unsigned integer it bit and I would like to give it an empty X underscore buffer keep it simple in the beginning and the size of the buffer will be let's say a 27 characters and then um in a double quote because it's a string I would just type the message welcome to let's say binary updates a simple message exclamation and then slash n for new line character and slash R4 Carriage written and then finish the line with the semicolon and this is basically a TX buffer that we want to transmit from our microcontroller to the personal computer so that on our personal computer we will able to receive this text message welcome to Binary updates okay now I can move on to the code here further into the main main function where the code execution start if I can go down further you can see we have a never ending while loop and here user could begin I would like to call the API Hall apis all underscore uart underscore transmit and I would just say transmit and then I just have to say n percent and you want handle so basically we are using UI 2 that's why we use UI to handle and then the data that we want to send it's a TX underscore buffer so that's buffer that we want to send and the size of the buffer is 27 bytes you can hard code it put this hard coded or you can just say use the size of operator or something like that it just doesn't matter this is very simple that's why I want to keep it very simple and then the timeout I want to give it as let's say like 10 milliseconds okay the smallest one and then end the line and then we will have to say Hall underscore delay because uh we want to send this text message every one second of the delay right so I can just say Hall delay and 1000 milliseconds because this whole delay function takes the parameter uh the number which is in milliseconds okay so that's basically what it is so as soon as we upload the code on our stm32 microcontroller this two lines of code will be executed and this will transmit the the welcome to Binary update string everyone's second of the delay okay let's compile the code and see if we have any error warning so right click on the project in the left pan and then click on build project and see if we have any error or Warnings and I think we are lucky we have a zero error and zero warning so right click on a project again click on run as and then select stm32 cortex M c C plus plus application and now it will ask us how we want to program the microcontroller and we have to go to debugger and on our stm32 nuclear board we have the St link programmer and debugger so by default it is selected by our stm32q by De so we don't have to do anything just click on OK and now the code upload will start you can see here on the lower menu bar here the code upload is started and you can see download verified successful now the code upload is done now to receive the data to check whether our microcontroller is really sending the string or not we have to first open a device manager so just open a device manager and make sure the com port and then under the ports and Device Manager you can see our stm32 nuclear board is detected as a com 3 com Port 3. this is very important we must know this and now we have to open a piece of software called party if you don't know putty I have another video on my channel you can watch that so open putty and then we have to select the serial because we are using serial uart communication and once we select the serial then we have to put the communication port on which our our computer detect stm3 the nuclear board it's a com3 we just have confirmed from the device manager and the speed of communication is one one five two zero zero that's our baud rate I think you remember parameter settings configuration on uart protocol uart setting on our ioc file in configuration click on open and now you can see our microcontroller is sending us a string and we now can successfully receive the data welcome to Binary updates okay I can minimize this and you can clearly see the messages coming every time on a new line because we added a new line and The Carriage written so everything is working fine right so so this is all we have to do to transmit the data from microcontroller to the personal computer but what if we want to send the data so let's say if we want to type something on this terminal console and we want to send the instruction to the microcontroller so this is the other way around communication and for that we have to go to our code again and let me go back to this user code begin private variable and we have to Define few more variables here okay so let's say u in eight underscore T and this time I have to say RX underscore I and the X index so this is basically a variable we will use in the future and then you also need few more variable you win 8 underscore T and this will be RX underscore data this will be a buffer of size let's say let's say a two bytes of buffer we don't want very very big size and then we want you in 8 into 40 one more buffer we need or X underscore buffer and this buffer size I would just make it as a 100 characters I mean 100 integer variables and then we need one more u in eight underscore T and this will be a variable transfer complete transfer cplt okay so this is basically we will keep track of this variable whether the transmission is completed or not okay so that's why we need this few variables if we want you can make the RX data variable to be a little bigger in case if we want to send the bigger size of the data from our serial terminal console so let me make it as a six bytes okay so just for you to show and going further here into the code first thing first in the while loop never ending while loop I will comment this transmit code that we just have tested so I select and then control and forward slash and that's basically comment the code so this is no longer the part of our program you can even delete if you want okay so uh now we will have this user code begin to here we will have to say Hall underscore uart underscore receive and we have to use receive interrupt right because we turn on the global interrupt and then I would say and percent you are 2 because we are using we have to pass the handle for u r 2 because we are using the user uart user both are the same thing and then here we have to put the buffer that basically the RX data buffer and this is a six byte that you remember I said this before and the size is a 6 byte and then I would end this with a semicolon okay save the file now we have to create a function here into this main.c file you see user code begin for here here we will have to create receive complete callback function and this function is a weak function and that is defined into the Hall uart Library so we will go here on the drivers and all drivers expand and source file and we have to look for all you want so this is a whole ur.c file and control F and you see RX cplt complete callback so receive complete callback function so let me click on find couple of times to find the function definition okay so here's the function right so this is the white um I copy this function I don't need this anymore so this is basically a weak function you can see I just copy the body of the function so select and copy okay I need this function and then I go to main.c file and the user could begin 4 here I would just create a little bit of space here for you to see so the code will be more readable so paste it here and now you can see like uh if you remember we have enabled the global interrupt on receive so whenever the uart receive any byte then the interrupt will be called and as soon as the interrupt will be called and the bytes will be received then this function will get invoke and we want to transmit the data back to our serial console that's on the putty so all underscore let's say transfer and I would say transmit and here I would just say and percent you want to handle and here I want to send RX data and the size of the RX data is 6 bytes and then the timeout I want to make this again as small as possible 10 milliseconds and that line with the column and that's it semicolon right so that's basically what it is now let me right click on the project I don't need this anymore so I just minimize this right click on the project and build the project to see if I have any error or warning you can see I'm lucky enough to get zero error and zero warning today and now let me upload the code on to the stm32 nuclear bot so right click and run s and click on stm32.x AC plus plus application and you can see the code upload started you can see here on our menu bar here down below so code upload is done download verified successful the code is uploaded now on stm32 nuclear pod so let me minimize the IDE you can see we have this previous message welcome to Binary updates if I close this just for you to see so let me turn on the putty again and freshly open open and put the com3 and one one five two zero zero bought red and there will be no previous jargon here on to the putty terminal console now I type something from my serial terminal console on my keyboard let's say if I type capital B i n a r y and you can see as soon as I type I receive the data back from the microcontroller to the personal computer back so what is happening here when I type any character onto the putty serial terminal console the data goes from our computer to the stm32 microcontroller and then the stm32 microcontroller send it back transfer back to our computer that's why we can able to print this on to the serial console right if I type more let's say if I type in small letter let's say my own name let's say Umesh nothing will get printed okay because I have to press the reset button on my Nuclear pod so now I press the reset button now I type new message so let's say Umesh you can see now u m e s h starts printed that is because we have given the RX buffer size just the six bytes if you remember look at this here when we defined the variable we have given RX data buffer to be just a six byte that's why we can't type more unless we reset then the buffer get empty and then we can type further but I think this proves the point you can send transmit the data and you can also receive the data using the NV interrupt Global interrupt okay that's basically what I want to show you now I want to show you something more magical so when we type LED on on our serial terminal then when we transfer from our personal computer to the microcontroller then microcontroller receives the data and turn on the LED that is connected to pa5 pin that's a built-in LED I have connected external LED as well so we have to make some changes here so look at the code here you into RX data so this RX data buffer we have to just make it let's say one or two bytes so I would just make it as a two bytes of data and then I go further into the code and you can see we have this all uart receive interrupt function that we have called API Hall apis and here I want to just make it as just a one byte of data that I want to receive if so whenever I receive one byte of data so every one byte of the data will be received and then interrupt get called right and then I go further down into the code and then we have a receive complete callback function so here's our uart receive complete callback function and here in this function I don't need to write this one line of code basically to transfer the data I would just remove this one line of code and then I would paste another piece of code I don't want to go so much detail into this I mean you can see this this is the piece of code that I have added by just removing that one lineup code which was basically this is that one lineup code Hall transmit you want and what this code does is basically whenever we type something on serial monitor let's say when we type LED on message look at this led on message whenever we type that when we hit enter then it will just see new line and the carriage return and I think you know always like whenever we hit enter it will be a carriage return that's basically a number 13. you can just figure out the code by yourself I don't want to explain every lineup code it's too much but what we are doing here is we are using the PA 5 Pin gpio a gpio pin 5 and what happens is whenever we type LED on a text message onto the serial terminal on putty and when we hit enter LED on string will be sent it to the microcontroller and microcontroller basically set the pa5 into high and that will turn on the LED you can see gpio pin set and that's basically turn on the LED and when we hit enter Then the LED turns on and when we type something else which is which is not LED on anything else apart from Led on and when we hit enter again then the LED goes all connected to pa5 pin okay that's basically what this code is all about now let me just right click on a project and click on build project and see if we have any error or warning you can see we have six warnings anyways um it's just a warning but there are zero errors so just right click on a project and click on run and select stm32 application and let's see when we upload the code you can see I think it's gone further it just quickly compiled the code and then it moved on but there were zero error and zero warning I think you can trust me a little bit otherwise you can try the code on your own site and you can see the download verified so code upload is done now we are ready to check on our serial terminal console that is on the party so when I type LED on and hit enter the LED lights up when I type LED off and hit enter the LED goes off since I don't want to make this tutorial very long so more advanced topics and projects will be covered in other videos please check the description section visit our channel for more stm32 programming lessons thank you very much and have a good time
Info
Channel: BINARYUPDATES
Views: 68,588
Rating: undefined out of 5
Keywords: stm32, uart, usart, microcontroller, communication, serial
Id: dEQwSl8mCFs
Channel Id: undefined
Length: 18min 43sec (1123 seconds)
Published: Sun Jun 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.