STM32 Nucleo - Keil 5 IDE with CubeMX: Tutorial 6 - SPI Interface

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone that's mama yaqoob and this is another video on the esteem your Cleo tutorial series on at this video I'd like to show you how to use SPI on the new Cleo will ask you if you search for SB I assume you know a little bit about it you know that has got a serial clock provided by the master has got mu Z that goes from the master to the slave and mizu line which goes the other way around from slave to master and a chip select for each independent slave this weed is meant to be a startup guide for people who want to learn how to use SPI on the nuclear board I will show you things like sitting of the SPI interface on the on Cuba Macs and how to transmit and receive data via SPI so inviting me today I'm gonna be using this n R of 24 radio transceiver as my spi slave device so I'm gonna write and read from this device through SPI alright so let's get started and set up a project in cubics a known cuba max click on your project unselect the right boards over using the nuclear so go to board selector it see nucleus 64 plus 1 and the MCU series is of 4 and at C of 401 re and this is the board when out by default I'm gonna clear up and set it up manually so go to panel two clear pan out here yes I want to enable the onboard led first might come in useful and then I need to enable one of the SPI lines so I'll enable us bi one but I'll take the output so you notice this one enabled ea-6b a7 and PB 3 o normally it will enable PA 5 but because I must be a 5-4 LED so you use the alternate bin what I want to also do is that I want to set those to the alternate bens so that I can take them easily on the arduino connector so if I said them with the alternate they will become B B 3 4 or 5 I can take them from here so that's what I'm gonna do when you click on any of them with the control I will show you the alternate and blue so must be before SPI 1 V 2 PSM an alternate is pb5 set up to SBI mozi and that's also I'll enable us be aligned unless enable SBI on the alternate bens so I can take my output on BB three four or five B three being the clock before being the master and blood slave output and BB 5 being the mozi line now I also need to enable a chip select line I'm gonna enable it on the on d8 on the arduino connector which is PA 9 on this DM so PA 9 is this one I'm gonna set it to G by 2 output in this little comment I'm sure you know that chip select line on the SPI you bring it low to activate the device and you bring get high again to deactivate the device so whenever we want to read or write to the slave device we will set this one to low do the read or write operation under bring your higher game okay now it's got the configuration and see if there's anything you can change all this bi I'm not gonna change anything in here but I'm just gonna lower the baud rate a bit so I'm gonna devour prescaler of 32 so whatever clock we have will be divided by 32 so and click ok now we're need to narrate the code so click on generate source code icon give the project a name I'm gonna call it SPI video select a location to store your files in and select the right IDE so mdk-arm v5 in my case and hit OK and now once the source code is generated click on open project and this will take you to your IDE are on Kyle the first thing we need to do is to open the main and we need to set the chip select line high to start the fourth so that the slave will be deactivated and we do that with a multi by all right function I don't know my chip selected burners on port a and it's been number nine it's be a nine and I set this one high to start off with I'll put a little delay of ten milliseconds perhaps so this will guarantee that our external device the NRF is deactivated at the start and now let me show you how to read data vspii so read data as a general rule to read data vsp are you first need to send the register address to open communication with that address have been followed by a read operation for this specific device the NRF according to the data sheet it says phone a read I have to first and a read command which is just the register address I wanna read from and I wanna read from perhaps register oh five the RF channel frequency and this one by default is set to 2 so 0 2 should read 0 2 by default after reset so to read step 1 is to is to put the chip select load and so that's we use the same function as this but instead of set we do reset to bring it low and this will activate the spi device and then the next thing I need to do is to transmit the registered address and we use SPI transmit function for that so hollis bi transmit you click control space to see the ultra typing so transmitted SBR transmit this function takes a few parameter the first one is this bi handle time Jeff it just be I want to define bakura mix and given that name the second parameter is a the an array of the data you want to send then the size so I need to define an array I'll define a globally so that I can see it when I go to the bugging window it's an unsigned 8-bit array I'm going to call it SPI data I'll give it a size of two elements I don't need to pass it to this function after I set it so I just want to send a single data byte which is the register address I want to read from register oh five so now I need to pass this to the function the size is one byte and the last parameter is a timeout ten millisecond should be okay next I need to follow this by a read operation so that I will read the register content until this I need to do Hollis be I receive instead and the receive function takes exactly similar parameters except the SBI data will be where you want to store the return date and I want to store them in index number one not index zero so I would have to make this a pointer to that location I'm receiving one byte time out of 10 milliseconds and then I need to terminate this by bringing by putting the chip select hi again to deactivate it so instead of research is going to be sick ok now we're ready to compile the code and test it compiled ok with no errors now let me load it to the board and we're gonna go to debugging the mode to see the value anon debugging mode I need to add this array to the watch memory so if I can see the variable in real time I'll expand this up I remove the hex display I'll expand this and I expect my read data to be an index number one so let me run the code ok suburb I get to just as I expected I can put the hex display back actually zero to this is what I expect because by default this one all zeros except bit number bit field number one so this is this correspond to two now let me show you how to write data vspii and according to the day sheet of this an RF device to write to a certain register we first have to send a write command which is similar to the right read command except we have to set this material to one now from majority of the CI a-- devices are work with this is not required but for this specific device it is required so so we always have to check with your device datasheet so let me do this so step one is so I'm doing right operation and I'm gonna write two registers your x 0 5 the are of similar register the RF frequency step one is the same I have to pray I have to put this chip select line load and then step two is very similar as well except I have to transmit the register address with that bit field being high which is this one correspond to doing in Orwell's your x20 because this one 0 X 2 0 is just the second bit on the fourth in the upper 4 bit so this is equivalent to this and then followed by the data I wanna write to that register so I'm sending 2 bytes here and I'm gonna write something like 67 just experimented correcting any values and now this will be 2 bytes starting from the start of address so this will send the register what this will be in hi and then followed by the data and the device then RF will understand that we're trying to write 67 and eggs to this register and now I just need to terminate it by bringing the chip select line a hi again to verify that this was written successfully I'm gonna do another read operation reading from the same register again after I do the right so I just copied this down here so after all right George is 205 this will read register o5 again sorry this need to be 2 bytes because I'm sending register followed by data and now I'm ready to compile the code so let me compile it and we'll load it to the board and we'll get back to the debugging window and on the debugging window our array is already added to the watch memory from previous one I'm going to add some break breakpoints I've tried with the first read operation I'll buy a breakpoint here and after all right I'll put a breakpoint and then I read the register so let me start the first read operation got me auto just like why I expect because the register is also by default and then I do my write a bration and then I read and I get 67 so the write operation was successful because I can read at 67 back ok so I've just shown you how to use SPI on the nuclear board that's all for today and as always if you like this video and want to see more videos like this don't forget to Like and subscribe thanks and I'll see you next time bye bye
Info
Channel: Mutex Embedded - Education
Views: 42,899
Rating: undefined out of 5
Keywords: STM32, Embedded, Microcontroller
Id: KnuAP7ga5Lw
Channel Id: undefined
Length: 10min 56sec (656 seconds)
Published: Sat Feb 03 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.