#7. EXTernal Interrupt using Registers || STM32F4 || STM32F1 || NO HAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to another video of controllers tech this is another video in the register-based programming series and today we will see how to use the external interrupt i will focus on f4 series mainly but wherever the changes are needed for f1 series i will cover them too let's start by creating the project in kyle id let's give some name to this project and click save i am using stm32f446re here we will only select the cm syscor and the device startup files now inside the project folder let's create a main.c file first in this main file we will create a main function and a while loop inside it first thing that we are going to do is copy the clock setup files inside our project now we will include those files in our project i hope you remember this file from the first video let's copy the rcc config.h in our main file create a function to configure the gpio pin here are the steps given to do so first we need to enable the gpio clock then set the pin as the input and finally set up the internal pull up or pull down resistance let's start with enabling the gpio clock in this tutorial i am using the pin pa1 as the input pin so we need to enable the clock for the gpioa this can be done in the rcca hb1 enable register as you can see here the zeroth bit controls the clock for the g p i o a so we need to write a 1 in the zeroth position next we need to set the pin as input and to do this we modify the gpio mode register to set the pa1 as the input pin we need to write zero zero in the second and the third [Music] positions and now finally we will configure the pull-up and pull down register [Music] but before doing that let's see how the button is connected as you can see here the button is connected to the pin pa1 and the ground so if we keep the pin in the pull up mode and then press the button the pin will discharge to the ground and it will read as a low so keeping the pin in the pull-up mode is somewhat easier and safe too to set the pull up or pull down we will modify the pull up or down register here we will be using the pull-up mode so we need to write a01 to the third and second positions this completes the pin configuration in the input mode now let's write another function to configure the interrupt to configure the pin as the external interrupt we need to follow some steps and they are shown here first of all we have to enable the system configuration bit in the rcc register this will basically enable the configuration controller clock then we will configure the external interrupt in the system configuration register next we will disable the mask on the xd line using the interrupt mask register then configure the rising or falling edge trigger for the interrupt and finally we will set the priority and enable the interrupt let's see them all first we will enable the sys config bit in the rcc register to do that we will look into the rcc apb b2 enable register here you can see the 14th bit of this register enables the system configuration controller clock but if you are using a f1 series mcu like f103c8 you have to enable the alternate function io clock for this purpose to do that you have to write a 1 in the zeroth position of apb2 enable register since i am writing this program for f4 series i will continue using sys config bit let's write a 1 in the 14th position to enable this clock next we will configure the external interrupt in the sysconfig controller as you can see here the external interrupt config register 1 can control the xd line 1 to xd line 3. similarly config register 2 can control xd lines 4 to 7. and in the end we have config register 4 which can control xd lines 12 to 15. since we are using pin pa1 we would need the xd line 1 and therefore we will modify the config register one if you want to configure this line for p a port we need to write zero zero zero zero in the fourth fifth sixth and seventh positions if you are using f1 series mcu the external interrupt registers are exactly the same but they are part of afio registers so you need to do the same but with a f i o registers instead of sys config registers now we need to disable the mask on xd line and to do that we will modify the interrupt mask register as you can see here writing a 1 to the respective position disables the interrupt mask so we will write a 1 to the first position in imr register next is the configuration of rising or falling edge this can be done in the rising trigger selection register and falling trigger selection register basically writing a one in the respective position will enable the rising trigger and the same goes for falling trigger also but should we select a rising edge trigger or a falling edge trigger well that depends on your requirement and your connection let's see the button again here the pin pa1 is pulled up by the internal pull-up register when we press the button this pin will connect to the ground and the pin will be low this is considered as the falling edge because the signal went low here as long as the button is pressed the signal will be low when we will release the button the pin will be pulled up again due to the internal pull-up and this will be considered as the rising edge it's better for us to use the rising edge in this case since it will be triggered when we release the button instead of when we pressed it so we will enable the rising trigger for xd1 by writing a 1 in the first position and i am also disabling the falling edge trigger for this xd line now we will set the interrupt priority we will use the inbuilt nvic functions for this purpose so go to the startup file here you can see the list of all the interrupt handlers we are using xt1 so we need this handler let's copy it and go back to the main file here we will call the function n-v-i-c set priority the first parameter is the interrupt so copy the handler here and replace the word handler with n and now give the priority in the next parameter remember that lower is the number higher is the priority that means zero have the highest priority but since we are using only one interrupt it doesn't matter what priority you give here now we will enable the interrupt and to do that we will call nvic enable irq this completes the interrupt configuration now let's write the interrupt handler remember that we have to use the same name that is provided here these are the steps for the interrupt handler so basically we will check which pin have triggered the interrupt then we will process our data and clear the interrupt pending bit let's see the pending register for more details so what happens is whenever an interrupt gets triggered the respective interrupt pending bit is set in the pending register we can use this to check which pin has caused the interrupt then we process our data and clear this pending bit so first we will check if the interrupt is caused by the pin pa1 if it is then we will clear the pending bit i am going to use a flag here which will be set whenever the interrupt gets triggered and we can later use this flag in the while loop actually it would be better if we process the data first and then clear the pending bit so i will fix this in the final code let's create another variable which we will increment every time the interrupt is triggered now let's write our main function first of all we will initialize the system clocks then configure the gpio next configure the interrupt now inside the while loop we will check if the flag is set if it is then we will increment the count variable and reset the flag let's create a function for delay also which can take care of the de-bouncing i think i need to use the 32-bit variable here add some small delay let's build it we don't have any errors go to options and change the frequency here according to your main clock select the st link debugger okay let's debug this thing now i am adding this count to the watch expression let's run it seems like it's not incrementing let's add a breakpoint inside the interrupt handler looks like it's not able to execute this statement pay attention guys as this is able to come to this line that means the interrupt is definitely being triggered by the pa1 and something is wrong with clearing the pending bit so after i went through the data sheet again i found out that the bit can be cleared by writing a one in that position and i was writing a zero let's write a one here now build and debug again let's run it now as you can see now the count is incrementing whenever the button is released the count gets incremented note here that as long as the button is pressed nothing happens but when i release the button the count increments this is due to the fact that we used the rising edge selection here so the interrupt is only triggered whenever the pin goes to high again this is it for this video i hope things were clear and you understood the external interrupt you can download the code from the link in the description i have attached the files for both the f4 series and the f1 series leave comments in case of any doubt keep watching and have a nice day ahead you
Info
Channel: ControllersTech
Views: 16,494
Rating: undefined out of 5
Keywords: stm32, stm32f4, f103, discovery, nucleo, registers, based, programming, external, interrupt, exti, line, button, input, bluepill, example
Id: TMvS42ru9Gs
Channel Id: undefined
Length: 17min 58sec (1078 seconds)
Published: Sun Mar 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.