Git Product home page Git Product logo

stm32_basics's Introduction

STM32 Basics

Implementation of basic components such as a timer, LCD display, external interrupt, and digital pins on the STM32 microcontroller. The microcontroller used is the STM32F103R6 model, operating at a clock frequency of 44MHz.

Two green and red LEDs are connected to digital pins in the microcontroller. We want to turn them on and off periodically.

The important part of the code corresponding to this component is in the main.c while loop as below:

  while (1)
  {
	  HAL_GPIO_WritePin(X1_GPIO_Port,X1_Pin,GPIO_PIN_SET);
	  HAL_Delay(500);
	  HAL_GPIO_WritePin(X1_GPIO_Port,X1_Pin,GPIO_PIN_RESET);
	  HAL_Delay(500);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

Two push buttons with pull-up resistors connected to them as their output are connected to a STM32 microcontroller. The goal is to turn the LED's on and off by pushing those buttons.

The important part of the code corresponding to this component is in the main.c while loop as below:

  while (1)
  {
	  if(HAL_GPIO_ReadPin(Y2_GPIO_Port,Y2_Pin)==0){
		  HAL_GPIO_WritePin(X2_GPIO_Port,X2_Pin,GPIO_PIN_SET);
		  HAL_GPIO_WritePin(X1_GPIO_Port,X1_Pin,GPIO_PIN_RESET);
	  }
	  if(HAL_GPIO_ReadPin(Y1_GPIO_Port,Y1_Pin)==0){
		  HAL_GPIO_WritePin(X2_GPIO_Port,X2_Pin,GPIO_PIN_RESET);
		  HAL_GPIO_WritePin(X1_GPIO_Port,X1_Pin,GPIO_PIN_SET);
	  }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

Two pins are defined on the microcontroller. We have a pushup button and a digital input connected to them. The goal is to have the LED's blink in a certain speed when pushing the buttons.

The important part of the code corresponding to this component is in the main.c while loop as below:

  while (1)
  {
	  if (condition == 0){
	  	  HAL_GPIO_WritePin(X1_GPIO_Port,X1_Pin,GPIO_PIN_SET);
	  	  HAL_GPIO_WritePin(X2_GPIO_Port,X2_Pin,GPIO_PIN_SET);
	  }
	  if (condition == 1){
		  HAL_GPIO_WritePin(X1_GPIO_Port,X1_Pin,GPIO_PIN_RESET);
		  HAL_GPIO_TogglePin(X2_GPIO_Port, X2_Pin);
		  		  HAL_Delay(100);
	  }
	  if(HAL_GPIO_ReadPin(Y1_GPIO_Port,Y1_Pin)==1){
	  	  HAL_GPIO_WritePin(X1_GPIO_Port,X1_Pin,GPIO_PIN_SET);
	  	  HAL_GPIO_WritePin(X2_GPIO_Port,X2_Pin,GPIO_PIN_SET);
		  condition = 0;
	  }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
void HAL_GPIO_EXTI_Callback ( uint16_t GPIO_Pin)
	{
	if(GPIO_Pin == Y2_Pin)
		condition = 1;
	else if(GPIO_Pin == Y1_Pin)
		condition = 0;
	}

Connected an LCD to the STM32 microcontroller, and printed my name and student number in its first two lines. The ports and pins corresponding to the LCD are as follows:

  Lcd_PortType ports [] = {
  		D4_GPIO_Port, D5_GPIO_Port, D6_GPIO_Port, D7_GPIO_Port
  };
  Lcd_PinType pins [] = {
  		D4_Pin, D5_Pin, D6_Pin, D7_Pin
  };

  Lcd_HandleTypeDef lcd = Lcd_create(ports, pins, RS_GPIO_Port, RS_Pin, E_GPIO_Port, E_Pin, LCD_4_BIT_MODE);
  while (1)
  {
	  Lcd_cursor(&lcd, 0,0);
	  Lcd_string(&lcd, "Fardin");

	  Lcd_cursor(&lcd, 1,0);
	  Lcd_string(&lcd, "810199456");
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

The last three digits of my student number will be stored in a variable called 'a', and 'a' will be divided by 7 and stored in a variable named 'b'. On the first line, 'a' will be displayed, and on the second line, 'b' will be shown with four decimal places.

  Lcd_PortType ports [] = {
  		D4_GPIO_Port, D5_GPIO_Port, D6_GPIO_Port, D7_GPIO_Port
  };
  Lcd_PinType pins [] = {
  		D4_Pin, D5_Pin, D6_Pin, D7_Pin
  };

  Lcd_HandleTypeDef lcd = Lcd_create(ports, pins, RS_GPIO_Port, RS_Pin, E_GPIO_Port, E_Pin, LCD_4_BIT_MODE);
  int StNumber = 456;
  float FloatNumber = (float)StNumber/7;
  char String [50];
  while (1)
  {
	  sprintf(String, (char*)"%d" , StNumber);
	  Lcd_cursor(&lcd, 0,0);
	  Lcd_string(&lcd, String);
	  sprintf(String, (char*)"%.4f" , FloatNumber);
	  Lcd_cursor(&lcd, 1,0);
	  Lcd_string(&lcd, String);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

Connected a timer to our STM32 micro controller that resets after some time to an initial value. The code corresponding to this component is in the main.c while loop as below:

  Lcd_PortType ports [] = {
   		D4_GPIO_Port, D5_GPIO_Port, D6_GPIO_Port, D7_GPIO_Port
   };
  Lcd_PinType pins [] = {
   		D4_Pin, D5_Pin, D6_Pin, D7_Pin
   };

  Lcd_HandleTypeDef lcd = Lcd_create(ports, pins, RS_GPIO_Port, RS_Pin, E_GPIO_Port, E_Pin, LCD_4_BIT_MODE);
  int a = 0;
  while (1)
  {
    /* USER CODE END WHILE */
	  while (a<51){
		  Lcd_cursor(&lcd, 0, 0);
		  Lcd_int(&lcd, a);
		  HAL_Delay(50);
		  Lcd_clear(&lcd);
		  a++;
	  }
	  if (a == 51){
		  a = 0;
	  }
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

Course Description

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.