STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 7 DAC - Updated Nov 2017

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Mohammad yaqoob I hope you are all having a great day this is another video update and it's gonna be for the distal to another converter tutorial this time the stm32f4 has got two analog outputs with a notepad range of 0 to 3 volts so you cannot put a sinusoidal signal a triangular signal or any random analog output as long as it's between 0 and 3 volts and the first 5 minutes of this video I will show you how to implement this with cuba max as particularly useful for people who just want to do things very quickly in cannot bother to watch 15 minutes video and in the second part I will show you how to do this without cuba max using holography roughly and possibly in the third but I might show you how to do this with a direct register access method so let's make a start alright first things first opening cuba max then click on your project and select your board - team 30 of 407 and here we've got to enable our output pin and to do this expand research analog converter and select one of the channels I'll select output 1 understand out TVPA 4 so you've got to take our output on PA for now let's go to concurrent - and see if there's anything we can change on digital analog converter configurations so there are only 2 main settings output buffers enable by default and the trigger we don't want to trigger it continuously so believe things as default and click OK now really generate source code click on this icon if you project a name and select the right IDE down click OK and on call Micro vision we need to expand this folder an application user an open domain and in here the first thing we need to do is we need to start the digital to analog converter is a function called hull DAC start and it's a normal start without email and this takes two parameters the DAC handle type death and it's defined here about Cuba max just HDAC and then the channel because there are two channels channel one and two so I've got to say channel one and that's how to start with the AC and then we need to output a value to the digital to analog converter and function is pretty straightforward - it's called how DAC set value and this function takes a few parameters the first one as usual the DAC handle time death and then the channel which is channel 1 again and it takes the data alignment this is interesting 1 8 bit I'm gonna output the data and 8 bits I want to align them to the right and then the data that I wanna output so this data has to be a bite because I specified the data width will be 8 bits so I've got to output the value and byte and then at the output it will appear as analog so the way I like to do this is I like to define two variables one floating point variable which I specify the value and voltage and I want to output something about 2.2 volt because the range is from 0 to 3 volt then I'll define another variable that will convert from floating point into byte so that I can output it properly with this function so value byte and to convert the value from float to byte got to do this and begin to otherwise it's not going to compile properly so to do to convert it from floating point to bite I've just got to match the range so so voltage divided by the maximum which is 3 volt multiplied by the maximum 255 of the output and this will convert the value from 3 volt range in to 255 range I'll typecast it to bite as well I'll ask somebody how to do it now I've just got to pass this variable to the function and now when I do this I should at the output at pen pa4 I should get to point 2 volt so let me compile it upload it to the board and see if you get the correct value so we indeed got two point two volts at the output well we got two point one seven father was close enough the tiny difference exists and the reference voltage not being exactly three volt so last simply how you do it with cuba mix that's all looking for you may stop the video here but I'm gonna carry on I'll show you how to do this without Cuba mix the second part would be very useful for people who want to know more about the hull driver and how things work at the background so to do the to do this without Cuba mix we need to do everything and call Micro vision the first thing we need to do is to go to the project and click on your micro vision project we've got self allocation for our folder I'll create a new folder here I'll call it that one no cube and all the project files will be stored in this location I'll call my project that one no cube as well now we need to select our board we've got to go to STMicroelectronics and stm32f4 and the board is or 7vg t of course and click OK now we need to select the software components the first thing we need to enable is this MSS core need to go to device and enable the startup software component this is extremely important this will start up the device and set up the clocks and everything to default and we need to enable digital to another converter well almost one thing we need to enable the classic to understand on few software components that we need to enable so when you click resolve there will be added automatically now we need to add our digital converter software component and let's require a DMA although we're not gonna use DMA but we can include it too but everything we need setup on the software components now we're ready to write our main and as you can see Cal macrovision generated a few software components for us these are mainly the hull driver and the startup file but I didn't add the main folder for us so I need to include my main file when you click on new file it's a C file and you call it main and enter main file the first thing you need to do is to include the hull library header file and this is stm32f4xx - underscore Hal dot edge and this would include all of the hull library software components altogether next thing I need to add my main function and its end main void no parameters and the infinite wire loop and now we need to configure our debug open and our digital to analog converter peripheral so we need to enable PA for insisted - analog and we also have got to do some configuration to the digital to analog converter like enabling the output buffer and disabling the timer trigger so let's do this and I'll do this in a separate functions as usual void ETB our config to configure the pin and this one takes no parameter so you put void and the second function is void DAC config so the g-value config I'll define it at the bottom here as pretty simple and straightforward all we need to do here is first enable debug abort a clock and to enable debug or a clock there's a complex named function and we can find it in GPIO driver file and how to use this driver give us a function we copy it and read select the board so we are enabling 410 so I need to replace X by a thanks thing I need to call in the function that does the initialization T value in it just like what describe the driver on this function takes two parameter the port and the T value any type def so I first have got to enable a or define a TBI or any type death I don't call it my pen in it but my PennDOT in it I've got select the pen so it's gonna be pen for what you got open for and I need to select the mode these are the two main initialization parameters our next select so the mode is going to be analog because it's an analog output control control tab for the auto text anime select analog I've also going to do the noir pull up Noble down just to make sure it's not there is no pull up and pull down enabled now I need to pass those parameter to the function so that the initialization will take place so the first parameter of this function is the port and as TPO port a and the second parameter is a pointer to the GPIO any type def so I've got to go to the ampersand sign and the name of the type def and this is simply have to enable port and city to analog and now we need to configure our detail to another converter device and we do this in the second function so I've got to copy it and define it at the bottom here and in here we need to enable digital converter clock and to enable the digital converter clock as a similar function to this except it says dark in here next I need to do some of the configurations and I need to define a digital converter type dish and it's described in the dark' driver here you define a handle type def and pass it to this function so I'll copy this function this is usually called last similar to this value and its function and DAC handle time defining to define this globally because I will need to use it in the main to set cities that I'll convert value and I'll call it my dark handle and we can use this to set up the digital converter parameter there aren't many anyway so dot instance we just need to set this an instance to that that's all we need to do apparently and now I can call the digital converter init function to set this initialization the next thing we need to do is to configure the DAC channel setting because as i said as i mentioned in cuba makes the research unlit converter has got two channels channel 1 and channel 2 so we need to do some configuration mainly to select the channel 1 and again the function is described in the driver it's in the doctor I'd rather it's held that conflict channel and we call it at the end as usual and this takes 3 parameters takes the handle captive the dark handle type diff and the duck channel handle tybo so that channel conflict up if I need to define this locally though I'll call it my duck channel cause very long name and for the back channel config the first thing I need to do is I need to enable the output buffer I don't need to disable the trigger just like oh I didn't Cuba makes that's pretty much everything we need to do now I can pass in this to the duck config channel function so that the configuration will take place this function expect three parameter the first one is the normal that candle type def so I still need to use that the second one is the channel conflict type def so this one I read to pass them as pointer so I've got to do the ampersand sign and the third parameter to the channel and this is DAC channel 1 because we're using PA 4 as our ultimate and that's all for the dark configuration no I can call in these functions in the main so that the configuration parameter will be executed so the first function I'm gonna call is as actually the hull init function this will initialize all the parameters it's a good practice to call it as I always mention it's called how Annette and then I need to call in my functions that I just defined now I need to start the digital converter and the function is called how DAC start just like what we did when we did the Kuban mix part two so I'm gonna do at this stage I'm gonna I'm gonna copy everything over from qmix because you already sell these parts and that is everything really now we should be ready to compile and go to the board I bother excuse my flatmate he's having his shisha at the background so let me compile it and upload it to the board compiled successfully without any errors or warnings now we're ready to upload it to the board click on this okay another problem looks like the debugger is wrong so go to advance for options and we need to select the wrong debugger go to debug window and select stealing the bugger for the scheme 3004 discovery board and also go to settings and enable something called trace that should solve the problem and select the core clock to 16 megahertz similar to this DM clock and now upload it to the border perfect as uploading successfully now now let's have a look at the board and see the multimeter and see what the output voltages and we expect somewhere in a 1.6 volt and this indeed worked as well although the value we get in was 1.4 volt which is slightly inaccurate but I wouldn't worry much about this for now so yeah that's how you do it with Hull drivers directly without the help of cubics I know on the third bit I will quickly show you how to do this with directory services this would be quite useful for people who want to know more about the architecture and structure of the cortex m4 device and for the dark twister access I'm not gonna do it line by line I'll just copy the closer hole into line it's not too complex for the digital converter turned out to be quite simple actually so how's the code the first thing it quite obvious so function prototype you finishing and variable declarations so let me go to the tabular your config function and what I'm doing in here I'm nailing TBI or debug abort a clock this is through the RCC registers I'll walk you through this really quickly so when you go today the reference manual and search for our CC registers where they are go to a HPE and I'll register one and this has got port a and the Zipit so when you're right one to this register you enable clock of port a and this is what I'm doing in here and the all sign is just two so that I can change bit field zero without affecting the other bit fields that's why I do the whole sign and then setting PA for as analog and this is in the TPL all registers so you do the same thing as I did you go to tibula all registers what you'll find here and go to the bio port modal register and on this one you can select which pin it is and in our case is pin 4 and when you write one one to this it will set it to analog so either analog and virtual analog output and that's what this line is doing so setting 1 1 through field for we multiply by 2 because each field has got 2 bits and similarly for the dark config the first thing is we enabled the dock clock the rack peripheral clock and as a different clock than the port a clock is not a HP it's a pp 1 and it actually found out in the steam board clock diagram and just like in here so port 8 is triggered by a cloak HP 1 scroll down you'll see that DAC is triggered by a PP one clocks you need to enable this club and what this lock is doing so for the r0 as I shown you both were different click we enable field 29 to enable duck let me go through it quickly in fact just so that you have a better idea so the RTC a PB 1 o'clock which is this one you see field 29 is to enable the DAC peripheral and this is exactly what this line of code is doing and then the league's two line is just to configure the these are to clear out all the other settings to clear the interrupt enable D and things like this throw the dart control register let me want to take you to the dark control register it's this one and made four or five that I come to register control things like D may interpret mask amplitude selection wave generation but we know using the wave generation feature here and some other feature so what we do and what these two lines of code is setting all of the other setting to zero because we don't want to use them but except this one this one is to enable the DAC channel one we will use this later on and for these strange-looking operations if it's not familiar with them just go got something called bitwise orbit masking I'm not sure about the name but that's as far as I remember from my placement TBG okay going up to the main code and the main we call the function the config function this is regular operation we convert the floating point to unsigned integer similar thing then starting the digital converter this is through the back control register the similar one so all this is doing is setting this bit to one so to enable the do start another converter channel one of course a fusing channel to then you've got to enable the second one and with sixteen then we outputting the DAC data as 8-bit through the DAC holding register 8-bit right aligned one four channel and you can find this out in here and they just below the control register few resistors at the bottom these are the other holding register of your outputting data in 12 bit but if you're outputting the data as 8-bit the only automatic to this register and that's exactly what I'm doing in here and that is everything for the dark to discharge this method of programming the digital converter so let me compile the code upload it to the world and convince you that it does actually work the same perfect it does work quite well we've got one point two seven six even better than the hell driver one and that is quite - one point eight so that's all for this video that's all I want to show you thank you very much for watching and please don't forget to Like and subscribe and I'll see you the next video bye
Info
Channel: Mutex Embedded - Education
Views: 21,464
Rating: undefined out of 5
Keywords: STM32F4, ARM Cortex, Embedded ARM, Microcontroller
Id: J_Lg6PpvX9Q
Channel Id: undefined
Length: 19min 11sec (1151 seconds)
Published: Tue Nov 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.