STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 20 - Real Time Clock (RTC)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is Mohammed and this is a new video tutorial and it's gonna be about real-time clock of the esteem 3204 discovery board and is pretty much the same for other steam boards as well as long as used cuba makes and how libraries so basically a real-time clock allows you to measure time date and use alarm functionalities and that's what i'm gonna walk you through today and the way i'm gonna go about doing this as I'll first show you how to do this with q-max for people who want to implement things really quick and towards the end of the video I'll walk you step-by-step through doing the same thing but without the help of cuba mix alright let's start with cube mix so first click on your project unselect the right board I seem to have two or four or seven in my case three GT and on here we need to enable the real-time clock profile so scroll down until you find RTC here it is expanders activate clock source and activate calendar and I wanna activate one alarm as well so just an internal alarm without AB without a pen out that's it so this one enable real-time clock as you can see in the clock configuration our TC clock is enabled and low-speed internal oscillator is selected well the internal Slater is not very accurate so I would recommend that you use an external oscillator to get a very accurate clock what you need to do if you use an external oscillator you need a tick of thirty two point seven six eight kilohertz and you connect this through PC 14 and 15 by enabling the low speed external clock so if you enable this as a high pass or ceramic resonator will enable 14 and 15 but in my case I'm just using the internal clock which as I said it's not gonna be very accurate but you will see the seconds will take slightly faster than the normal seconds but not very significantly alright let's carry on I'm gonna enable a couple of LEDs before I carry on with the RTC configuration so enabling the green and orange LED go to a configuration window click on RTC and first thing we're gonna sit is data format I want to say that the data and binary just normal decimal number so hours and twenty three and by the way our format 224 I like it this way minutes 259 so that I can see changing our seconds to 45 daylight-saving we haven't got any daylight saving yet but this is quite good option for people who have winter or summer changing time it's very cool it can substract when i wore or can add one hour weekday so this is the date weekday is let me select 31st of December of this year which is was Sunday and the month to December unday to 31st 2017 so that after 15 seconds it will change our and will change date and I will change year which is pretty cool because I can see this then alarm I'm gonna set the alarm to zero hour which correspond to 12 minutes to zero and seconds to 10 seconds so I want to change this day I'm gonna get an alarm after 10 seconds but the date is 1 so you can set the date here it's gonna be 1st of January but the alarm can only be one month long you cannot do an alarm for next month has to be within this month or within this week if we decided to do weekday alarm ok perfect so I think that's all we need oh no hold on a second I need to enable the alarm interrupt as well so that will interrupt on the alarm and that's all for the RTC configuration so click OK and click on this icon to generate the source code give the project name gonna call it RTC video select a location distort and I wanna store it on my steam projects folder and select the right IDE call Microvision 5 in my case and hit OK and once the project is generated click on open project and this will take you to call macrovision idea I don't hear expand the project folder and go application user on over in the vein unless first have a look at the RTC initialization just to double check because I had issues with it in the past so the RTC initialization set the hours format to 24 set the alarms but it did not set the time and date which is as I expected so we need to do those manually they're very short anyway so first let me set the time just at the time used the time type def which is this one it already defined by cuba makes others are very intuitive so as time dot hours - 23 minutes - 59 because I wanna see changing an hour seconds to 45 just like what we tried to do in kill mix but I didn't do this for us and then all you gotta do is to call RTC set time so how are you see said time and this takes the RTC ended type def which is defined here so RTC handle tab the father the chart you see as a first parameter yeah it takes us as a first parameter a pointer to that and the second parameter is the time type def which is this one and as a pointer to that and the third parameter is the format so this one is for math binary so because I'm just ending normal numbers I can use BCD instead but it's binary is much easier okay so this is simply how to set time now let's set date to set date use the same principle we need to use the date time death which is defined here estate associate date is gonna be 30 first month is December so 12 oh sorry month not quite 12 you have to use the define so RTC month and control space to see the options and as December because if you look at December I don't think the number is 12 I gotta compile first but we'll have a look at it later then the weekday same thing so RTC weekday I will give you the option I think it was on a Sunday yeah and the reason why I chose 31st of December is that I want to see changing month so after 15 seconds as I explained the Cuba mix date will turn to 1st of January and they will change the new year as well will change so last thing is to say year year to 2017 but you shouldn't write mm just write 17 because this is a number between 0 and 100 I believe and then finally we need to call the hull RTC set date function ok so the first parameter is the same one the RTC handles have - and then the date type death and a pointer to last and last one with the same thing the RTC format reasoning just normal binary numbers ok so far so good the next thing to do is to set us to get the time and date in the while loop so I'm gonna read the time and date every 200 milliseconds for example and to get the time use a function called how RTC good time and parameters are exactly the same as a set time 1 so what it's gonna do is that it's gonna get the time and stored and that s time again update this time structure a same thing for date as well so how how RTC get date and the parameters are similar to the date thing here ok I'm also going to go an LED here so my orange LED is on port T number 13 so we got the time we got date one final thing we got to do is to define the event callback for alarm a so when we get an alarm it will jump into the event callback and to define this go to functions and search for our TC in here as a function called alarm any event callback this one we just need to copy it to our main but without this week object and they will write our code if you want to handle the alarm so on the alarm I want to toggle an LED my green LED green LED is on port deep in number 12 so on the alarm it will tickle that led alright I think we're we're ready so yeah we said time and date when we got a time date and we have the alarm call back so let's compile the code then okay convulsive Swilley without any arrows no let's load it to the world and we're gonna have a look at the variables in this team studio so good loaded to the board fine now open your esteems told you I heard old steam studio I'm gonna expand this first I click on this to all to open the project so look it'll click on this navigate to your project folder has the one RTC video go to mdk-arm an RTC video again and you need to load this to the excel file and I want to see the date and weekday month date in year hours minutes seconds and sub and sub seconds I click import' and they're imported here so how lot all of them right click send to variable viewer I wanna see them in a table format here we go now all you need to do is to run this and restart your steam board okay I can see that started from 45 and I expect on 59 or 60 this one changes and your changes and also this ok perfect so 2018 first of January and weekday is Monday I believe I don't then I expect ya the green LED came on on the alarm which is 10 seconds of 1st of January or just in next month alright good ok we got a real time clock walking time date and alarm using Q of X that's all you're looking for you may stop the video here but I'm gonna carry on I'll show you how to do this without cuba max i'm through this without cuba max click on project and have new micro vision project and select a location to store your files in I'm gonna store them in this new cube file I'm gonna call it RT seano cube I'm here save and now we need to select our board it's team 30 or four or seven vgt in my case unless one and here we need to select the software components first one is a sim sis core and then devices startup and the cube framework we need to enable the classic this required other software components we can add them by hitting resolve and then in the steam coop all we need to include the RTC so real-time clock this one and that's all we need so click OK and when you go in the project you see clear macrovision generated files for us mainly the hull arteries but we need to add a main manually so right click on this adding new items the C file and we need to call it main another main first thing we gotta do is to include the whole library header file so steam 3004 x6 underscore hull to take this one called the hollow Barry style project under we need to define our main functions int main void doesn't take any parameters and then the infinite world over the end first thing we need to do in the main is to call the hull initialization function to implement a basic initializations so it's called hull in it it's going to set everything to that default value okay so we're gonna add a few functions the first one is the system clock configuration so we need to configure the clock to enable the low speed internal clock for the real-time clock and the way I'm gonna do this I'm gonna copy the configurations over from Cuba mix but I'm gonna include them down in the video as well in a text file so let's define the system configuration function at the bottom here so I copied it over from Cuba mix but the configurations are self-explanatory so we settin of the internal oscillator the high speed 1 to 16 megahertz and then we enabling the CPU and APB and HPV buses so the peripheral and tamas clock and then enabling the internal low speed oscillator for the real-time clock and then the systick interrupt to enabling that okay the next function we're gonna add is the led config to configure our LEDs and this is so simple you've seen it I'm sure you've seen 800 times if you watch my previous videos so I'm gonna define it at the bottom here it's very symbols we enable both the clock because this is the boat where LEDs are connected to I'm you enable and 12 13 14 or 15 for the green orange red and blue LEDs the mode to output push ball and he called us an attraction to initialize those pin outs and next we're gonna add a function to configure the real-time clock we call it RTC config so the finish of this function first thing we're gonna do is to enable RTC clock using this function similar to this VI you want but this one is only RTC clock so how else you see RTC enable and then to configure the RTC we need first to define a RTC an RTC hundl time dev and we're gonna define a globally because we're gonna need it in the main so an RTC hundl type diff and I'm gonna call it my RTC handle and we'll use at the bottom to configure the RTC so the first one gonna set is instance to RTC and then we're gonna do a few initialization so do ten eight dot hours format - 24 - 24 you can have 12 AM and PM about using 24 our next is the synchronous and synchronous clock dividers so a synchronic prescaler divider is this one is explained in details and the datasheet so let me add the other one to the synchronous prescaler or divider so these are explained in details and the earthman's manual and vanes 785 so these synchronous and asynchronous pre scalars are to set the to create one Hertz clock for the calendar so since we have 32 0.76 kilohertz we need to have the a synchronous one two one two eight and the synchronous divider two two five six you can read more in here to get more ideas but this one start from zero so this one gonna be one two seven zero two one two seven one zero two two five five and next is the output so output is disabled so RTC ultimate two disabled because we don't have any physical output just an internal RTC and finally we're gonna call hull RTC in it to implement those initializations and this one takes only a single parameter the RTC handle I'll point it to that next one here we need to configure the alarm and to do this we need to define an alarm type diff as well I will define it globally to so a long time death I'm gonna call it my alarm and we'll use it to configure the alone so first is the alarm I'm using alarm a then the alarm time so a long time that hours are going to be zero because 12 then minutes to zero as well on seconds to 10 seconds and then we gonna specify the alarm day to weekday so so this one allows you to select between other you wanna alarm on the weekday or or the date I want it to be based on date not weekday so date is and then I can set the value of this so so my alarm did date weekday not select but the but the other one to set the value a lavallee gonna be one because first off January first of the month because if you'll recall from what we did in qmx we set the first to 31st of December and after 15 second it will jump into the first of January so I'm basing my alarm based on first over the month but a far selected weekday then I'll have to select the others Monday Tuesday wire bar okay hope you got the idea I'm finally we need to call RTC sir alarm to implement those configuration for the alarm and we need to set alarm as interrupts because we need to enable an interrupt only alarm the property we need to pass are the RTC hundl type dev so need to pass this one here as a pointer next parameter is the RTC alarm type this pointed to that too and the format so RTC format to binary I'm finally here on this function we need to enable the alarm interrupt and set its priority using these two functions an out here outside the function we need to define the RTC alarm our Q handler to link the system enter of it to the RTC to her library RTC interrupt and we need to do the same for the systick interrupt as well these are usually defined by academics and a file called the MDOT ITV file I believe the reason why we need to do this is because the system and the starter file is programmed to jump into these and interrupt handler know the halat or e1 we do these to link the two together okay we're done with our configuration so let's go and write our main so on the main I need to call in the functions I setup so I need to call the system clock configuration function to implement its code and same for the early config in RTC config sanctions I need to call them in the main alright now we are in the same stage as a project generated by qm x so let me copy the code over with it in qm x so we set the time and date and this required type def assignee to define here for date and time so i define those i don't need to to correct the name of the artist handle tab just because we called it differently here so interchange in here and the gate time date and the infinite wire loop so similarly you need to change the handle type diff name too and finally I need to define the alarm RTC the RTC alarm event callback function alright so I'm gonna copy I'm gonna vote this down in the description and in a text file if you think I'm going really fast in here but all I'm doing now is to copy the stuff I did in kuvings over to here alright I think we're done so we set the time to 23:59 45 seconds and the day to 31st of December on a Sunday and the year was 2017 ok and here we get the time and date so let's compile the code alright great compiled without any errors next before we load it to the ball we need to select the right debugger so click on options for target go to debug and select a stealing debugger and also go to sitting trace set the core clock to 16 megahertz and enable trace and click OK I'm now loaded to the board alright loaded successfully now let's go to a steam studio and want the time and date here is a steam studio I'm gonna expand this first and then click on this to load your project that's one navigate to your project folder and go to objects so this is the one done without qmx the a excel file stored under objects so I'll select the a excel file and I will award the the time in hours minutes seconds in sub seconds and the date weekday month date and year and click import' ok they've been imported successfully so highlight all of them right click and send to variable viewer and I want to see them in a table again so all you need to do is to click run and reset your esteem boarding all right great I can see the time is walking yeah let's just wait until I get to 59 or 60 should see the time changes in the year on the day too alright fantastic this is walking and on the second stand I expect my green lady to come on I'm looking at my board now yeah it did so perfect we're done alright this brings me to the end of my video I've shown you today how to use real time clock on the East iam Board would have done without the cube of X and as always if you found this helpful don't forget to hit like and subscribe have a nice day and I'll see you next time bye
Info
Channel: Mutex Embedded - Education
Views: 30,341
Rating: undefined out of 5
Keywords: STM32, Microcontroller, MCU, Cortex ARM, Embedded C
Id: cFttY8jwFhY
Channel Id: undefined
Length: 22min 28sec (1348 seconds)
Published: Wed Jan 03 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.