MICHAEL CADIZPaul Rodolf P. Castor
Published

Interactive RT-Spark: Switches, LEDs & LCDs

Experiments with the STM32 microcontroller with 5 switch controls to activate 3 LEDs in active low and in-built LCDs by RT-Spark

IntermediateFull instructions provided16
Interactive RT-Spark: Switches, LEDs & LCDs

Things used in this project

Hardware components

LED (generic)
LED (generic)
×3
PTS 645 Series Switch
C&K Switches PTS 645 Series Switch
×5
RT-Spark STM32
×1
Jumper wires (generic)
Jumper wires (generic)
×10
Breadboard (generic)
Breadboard (generic)
×1

Story

Read more

Schematics

STM32F407ZGT6 Pinout

Code

Code

C/C++
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t switch_Up() { //Used by LCD and REDLED
	return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_0) == GPIO_PIN_RESET;
}

uint8_t switch_Down() { //Used by LCD and BLUELED
	return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_1) == GPIO_PIN_RESET;
}

uint8_t switch_Center() { //Used by LCD and GREENLED
	return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_6) == GPIO_PIN_RESET;
}

uint8_t switch_Left() { //Used solely by LCD
	return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_2) == GPIO_PIN_RESET;
}

uint8_t switch_Right() { //Used solely by LCD
	return HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_5) == GPIO_PIN_RESET;
}



void RGBLED_Config(uint8_t red, uint8_t green , uint8_t blue) { //leds_set()
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, !red);
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, !green);
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, !blue);
}

void ToggleLEDS() { //light_leds()
	RGBLED_Config(switch_Up(), switch_Center(), switch_Down());
}

void print_switches() {
	if(switch_Up()) {
		lcd_show_string(0, 0, 16, "Button Up is Pressed    ");
	} else if(switch_Down()) {
		lcd_show_string(0, 0, 16, "Button Down is Pressed  ");
	} else if(switch_Left()) {
		lcd_show_string(0, 0, 16, "Button Left is Pressed  ");
	} else if(switch_Right()) {
		lcd_show_string(0, 0, 16, "Button Right is Pressed ");
	} else if(switch_Center()) {
		lcd_show_string(0, 0, 16, "Button Center is Pressed");
	}
}
/* USER CODE END 0 */

///////////////////////////////////////////

/* USER CODE BEGIN 2 */
  MY_GPIO_Init();      // Initializes your 5 switches
  MY_LEDS_Init();      // Initializes your LEDs
  drv_lcd_hw_init();   // IMPORTANT: Powers on the backlight and clears the screen

  lcd_clear(BLACK);             // Makes the whole screen white
  lcd_set_color(BLACK, WHITE);  // Sets background to white, text to black
  lcd_show_string(0, 0, 16, "Hello World!");
  /* USER CODE END 2 */
  
  /////////////////////////////////
  
 /* USER CODE BEGIN 3 */
	print_switches(); // Updates the LCD text when you press a button
	ToggleLEDS();     // Turns on the LED that matches the button
	HAL_Delay(100);   // A small delay so the screen doesn't flicker
  }
  /* USER CODE END 3 */
  
  ///////////////////////////////////
  
  /* USER CODE BEGIN 4 */
void MY_GPIO_Init(void) { //switches_init()
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  //__HAL_RCC_GPIOG_CLK_ENABLE(); //Not needed because it is already enable from the MX_GPIO_Init function

  // 0 = BtnUp, 1 = BtnDown, 6 = BtnCenter, 2 = BtnLeft, 5 = Right
  GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_6 | GPIO_PIN_2 | GPIO_PIN_5;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
}

void MY_LEDS_Init(void) { //leds_init()
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  __HAL_RCC_GPIOA_CLK_ENABLE();

  // Set as Active-Low, means it is on at 0V, and off at high volts
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);

  // 2 = REDLED, 3 = GREENLED, 0 = BLUELED
  GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  RGBLED_Config(0, 0, 0);
}
/* USER CODE END 4 */

Credits

MICHAEL CADIZ
2 projects • 0 followers
Paul Rodolf P. Castor
17 projects • 9 followers

Comments