KennethPaul Rodolf P. Castor
Published

Exploring RT-Spark's (RT-Thread: Spark-1) built-in LCD

The basics of Spark-1's built-in LCD, combined with 5 switches/inputs to alter what is currently shown, including the 3 LEDs.

IntermediateProtip1 hour36
Exploring RT-Spark's (RT-Thread: Spark-1) built-in LCD

Things used in this project

Hardware components

PTS 645 Series Switch
C&K Switches PTS 645 Series Switch
5 Switches to cover "Up, Down, Left, Right, and Center."
×5
Jumper wires (generic)
Jumper wires (generic)
Consider having at least 20 jumper wires so that you won't have a hard time thinking how to conserve your wires as you connect each component.
×3
LED (generic)
LED (generic)
Preferably 3 LEDs, which represents Red, Green Blue, or a single RGB LED. But, This project specifically uses separate 3 separate LEDs, you might get lost in translation if you do not fully know how an RGB LED works.
×3
Breadboard (generic)
Breadboard (generic)
Have a breadboard that can hold a lot. It's better to have the big ones so that you won't have a hard time putting the components in a proper place.
×1
Resistor 220 ohm
Resistor 220 ohm
This 220Ω resistors are used for the 3 LEDs. Though, you can use resistors that are valued around "100Ω-500Ω", or other ranges, provided that you know what you're doing.
×3
Resistor 10k ohm
Resistor 10k ohm
Optional: Incase you're not using the RT-Spark's (Spark-1) built-in pull-up or pull-down, might as well set the 5 switches/inputs as pull-down to not confuse yourself further.
×5
RT-Spark (Spark-1) RT-Thread Development Boar
The board that we will be using to code the logic, and connect the components.
×1

Software apps and online services

RT-Thread STM32CubeIDE (V1.19.0)
Github: RT-Thread-Studio / SDK-BSP-STM32f407-Spark

Story

Read more

Schematics

STM32F407ZGT6 LQFP144 Pinout

LCD Pins Location

5 Momentary Switches, 3 LEDs

This Setup is already initialized by "Code 4"

STM32F407ZGT6 MCU Peripherals

Code

Code

C/C++
Code 0 = The Logic
Main = FunctionCalls
Code 4 = Initialization of GPIO Inputs (No need CubeMX, unless you want it that way)
// Code 0:
////////////////////////////////////////////////////////////////////////////////
/* 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 */
////////////////////////////////////////////////////////////////////////////////





//Main:
////////////////////////////////////////////////////////////////////////////////
/* USER CODE BEGIN 2 */
MY_GPIO_Init(); // Your Custom MX_GPIO_Init function to Initialize pins
MY_LEDS_Init(); // Your Custom MY_GPIO_Init function to Initialize built-in LED pins

drv_lcd_hw_init(); // Turns on Back-light, resets the LCD configuration, and initializes the LCD
lcd_clear(BLACK); // Clears everything shown, and changes the background color to white
lcd_set_color(BLACK, WHITE); // Set text color to white, and text background color to black
lcd_show_string(0, 0, 16, "Hello World!"); // Shows "Hello!" to the screen, configured at (x-axis(239max), y-axis(239 max), textsize)
/* USER CODE END 2 */
////////////////////////////////////////////////////////////////////////////////








//Main Loop:
////////////////////////////////////////////////////////////////////////////////
while (1)
{
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
  print_switches();
  ToggleLEDS();
  HAL_Delay(500);
}
/* USER CODE END 3 */
////////////////////////////////////////////////////////////////////////////////
















//Code 4:
////////////////////////////////////////////////////////////////////////////////
/* 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 */
////////////////////////////////////////////////////////////////////////////////

Polled I/O LCD

Credits

Kenneth
2 projects • 0 followers
Paul Rodolf P. Castor
16 projects • 9 followers

Comments