Key control

Key control1. Learning objectives2. Hardware constructionIndependent key principleKey debounce3. Experimental steps1. Open the SYSCONFIG configuration tool2. Configure pin mode3. Write the program4. Compile4. Program Analysis5. Experimental phenomenon

1. Learning objectives

  1. Learn the basic working principle of the keys of the MSPM0G3507 motherboard.
  2. Use the key module to control the on-board LED light switch.

2. Hardware construction

Independent key principle

Independent keys are usually implemented through mechanical switches, and their hardware structure consists of keys, connecting lines, and pull-up/pull-down resistors. When the key is closed, the line is connected and the signal level changes; when the key is released, the line is disconnected and the signal returns to its initial state.

Key debounce

The mechanical elasticity of the key will cause the signal to jump multiple times in a short period of time when the key is closed or released, which is called key jitter. Usually, jitter is eliminated by software delay debounce or hardware plus RC filter circuit.

This course does not require additional hardware equipment, and directly uses the onboard function keys on the MSPM0G3507 motherboard.

In the schematic diagram of the development board, a button is connected to the PA18 pin. One end of the button is connected to a high level (3.3V), and the other end is connected to a GPIO pin. At the same time, the PA18 pin is connected to the ground through a 47kΩ pull-down resistor R5. When the button is not pressed, the PA18 pin level is pulled down to 0V due to the pull-down resistor; when the button is pressed, the PA18 pin is directly connected to a high level of 3.3V, thereby achieving a change in high and low levels. The development board can determine whether the button is pressed by detecting the level state of the PA18 pin. This design is simple and stable, and can effectively avoid signal interference caused by pin suspension.

MSPM0G3507 main control diagram

image-20241118203957884

Key schematic diagram

image-20241119185237669

3. Experimental steps

In the microcontroller, the input mode of the pin refers to configuring a pin as an input function to receive changes in external signals. The working principle of the key is based on this point: by setting the pin connected to the key to input mode, the microcontroller can detect the level change on the pin. When the key is not pressed, the pin level remains at a low level; when the key is pressed, the pin level becomes a high level. This change in high and low levels is the embodiment of the key input signal, and it is also the basis for the microcontroller to judge the key status. Therefore, the process of receiving external signals is actually the process of detecting the high and low level changes of the key through the input mode.

1. Open the SYSCONFIG configuration tool

Open the blank project empty in the SDK in KEIL.

image-20241118204605335

Select and open, open the empty.syscfg file in the keil interface, with the empty.syscfg file open, then select to open the SYSCONFIG GUI interface in the Tools column.

image-20241118204833195

In SYSCONFIG, select MCU peripherals on the left, find the GPIO option and click to enter. Click ADD in GPIO to add a group of GPIO. This GPIO_GRP_0 is the newly added one.

image-20241119191830159

2. Configure pin mode

Configure pin parameters, name the GPIO instance as KEY, and select PORTA for Port because it is connected to PA18.

Set the pin name to PIN_18 and the input mode to Input. Set Internal Resistor to pull-down resistor Pull-Down Resistor, and set the pin to be controlled in Assigned Pin to 18.

image-20241119192326266

Click SAVE to save the configuration in SYSCONFIG, then close SYSCONFIG and return to keil.

image-20241119193346732

Select Yes to All in the pop-up window

image-20241119143728060

Similarly, we also need to confirm whether the ti_msp_dl_config.c and ti_msp_dl_config.h files are updated. Compile directly, and the compilation will automatically update to keil. If there is no update, we can also copy the file content in SYSCONFIG.

3. Write the program

In the empty.c file, write the following code

4. Compile

Click the Rebuild icon. The following prompt appears, indicating that the compilation is complete and there are no errors.

image-20241119150803274

image-20241119150902489

4. Program Analysis

uint32_t DL_GPIO_readPins(GPIO_Regs* gpio, uint32_t pins) is used to read the level status of the specified GPIO pin. Get the status of the specified pin by reading the value of the GPIO register. Enter the GPIO port and pin number to be read, and return the level status of the pin.

gpio: The GPIO port to be read.

pins: The pin number to be read.

This code continuously detects the level state of pin PA18 (button access pin) and controls the output level of LED1_PIN_2 (LED control pin) to realize the function of key control LED. When PA18 is high level, the LED is turned on (LED pin outputs high level); when PA18 is low level, the LED is turned off (LED pin outputs low level).

5. Experimental phenomenon

After the program is downloaded, the LED light can be controlled by the button. Press the button to turn on the LED light, and release the button to turn off the LED light.

image-20241119200450318

image-20241119200537063