The circuit of the project is pretty straight forward. I connected the led to pin#pc-13 of stm32f103c8t6 and button to pin#pa-10.
The Led pin Pc-13 is declared as output and button Pa-10 as input. Pa-10 pin as an internal pull up resistor. I enabled it and connected my button directly to ground. In the upper picture you can see that GPIO PA10 is in Input Mode and PC-13 in Output Push Pull Mode. Initial output level at PC-13 led pin is declared as high. Output frequency for Led pin set to medium. If you did not understand any thing at this level please take the tutorial mentioned above. It explains all this stuff deeply.
Stm32f103 microcontroller supports internal and external clock sources. Internally it has 2 RC oscillators HSI(High speed internal) and LSI(Low speed internal). High speed internal has 8Mhz clock and Low speed internal has 40khz clock. Externally stm32f103 supports HSE(High speed external) and LSE(Low speed external). High speed external supports max 16Mhz input clock and Low speed external supports 1000khz maximum clock. Stm32f103 has an internal PLL circuit from which we can generate clock signal of 72Mhz. We can go above 72Mhz but since stm32 works at max clock of 72Mhz so we did go above 72Mhz. In this tutorial i am using High speed internal clock source 8Mhz. Settings for HSI(High speed internal) are shown below.
The while(1) loop in main() function is where we write our code. I write the below code their for our logic and its working perfectly and according to the desired output.
if(HAL_GPIO_ReadPin(Ibutton_GPIO_Port, Ibutton_Pin)==GPIO_PIN_RESET) //Check if button pressed
HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin,GPIO_PIN_SET); //If pressed Led Switch On
HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin,GPIO_PIN_RESET); //Else Led Switch Off
HAL_GPIO_WritePin() writes to the specified pin. Makes it high or low. It needs 3 parameters port, pin and data. In our case port is Led_GPIO_Port pin is Led_Pin and data is SET/RESET.
- GPIO_PIN_RESET – Write 0 to pin, Makes pin Low
- GPIO_PIN_SET – Write 1 to pin, Makes pin High
Watch the project video….