STM32 actual combat button lights up LED (interrupt)

Publisher:火星叔叔Latest update time:2018-07-21 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Let's take a look at the simplest experiment of lighting a lamp. The experiment requires you to use KEY4*4 buttons. Press the button to turn on the light, and lift the button to turn off the light, and repeat this cycle:

The idea of ​​the program is as follows:

1. The first thing you need to set is the clock: you turn on the clock of the port to which you connect the pin of the lamp. For example, if the lamp is connected to a pin of port A, you need to turn on the clock of port A.

2. Secondly, set the pins (we need to set the pins of the LED and a key in the matrix keyboard): We need to initialize the GPIO. We only need to call the GPIO_InitTypeDef structure and set each member. We need to set the position of the PIN pin GPIO_Pin, the speed of the pin GPIO_Speed, and the working state of the pin GPIO_Mode. Finally, call the initialization function GPIO_Init.

3. Then start writing our main function: If I want to set the pin of the LED lamp to a high level, I only need to call the GPIO_SetBits function, which sets the pin to a high level. If I want to set it to a low level, I just call the GPIO_ResetBits function.

4. Finally, we need to make it light up when pressed and go out when lifted, so we need an if statement

——————————————————————————————————————————————————-

Below is the program: The chip used is STM32F103C8T6, the LED control pin is C13, and the matrix keyboard is B9

The first step is to control the small light to be a push-pull output


void LED_Init (void)

{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //Initialize GPIOC clock

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOC, &GPIO_InitStructure);

 

}

Then configure the matrix keyboard


void KEY_Init (void)

{

GPIO_InitTypeDef GPIO_InitStructure; //Configure PB9 pull-up input

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //Initialize GPIOB clock

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_Init(GPIOB, &GPIO_InitStructure);

 

}

Next is the main function


int main()

{

LED_Heat();

KEY_Init();

 

while(1)

{

LED = 1;

if(KEY == 0)

{

LED = 0;

}

}

 

}


Keywords:STM32 Reference address:STM32 actual combat button lights up LED (interrupt)

Previous article:STM32 learning notes: key query method to control the on and off of LED lights
Next article:STM32_button interrupt

Recommended ReadingLatest update time:2024-11-16 22:40

Design basis of eight-segment LED digital tube display in single chip system
In the single-chip computer system, LED digital tube displays are often used to display various numbers or symbols. Because of its clear display, high brightness, low voltage and long life, it is widely used. Introduction: Do you still remember the "matchstick game" we played when we were young? Several matchsticks ca
[Microcontroller]
Design basis of eight-segment LED digital tube display in single chip system
In-depth focus: Several difficulties in LED lights entering the home
The invention of electric light has brought more light to mankind. With the advancement of science and technology, household electric lights have gone through several generations of development: the first generation is incandescent lamps (tungsten filament lamps), the second generation is fluorescent lamps, the thir
[Power Management]
In-depth focus: Several difficulties in LED lights entering the home
Application difference of LED driver precision control
LED is a typical current-driven device. Accurately controlling the LED driving current can determine many parameters including light efficiency, power efficiency, heat dissipation and product brightness. When it comes to LED driving accuracy, constant current error usually comes to mind. In fact, driving accuracy is no
[Power Management]
Application difference of LED driver precision control
Design of LED intelligent lighting system based on single chip microcomputer
1 Introduction LED is called the fourth generation of lighting source or green light source. The light emitting device of LED is a cold light source, which has the characteristics of energy saving, environmental protection, long life and small size. The light efficiency of incandescent lamp and halogen tungsten
[Microcontroller]
Design of LED intelligent lighting system based on single chip microcomputer
Comprehensive Analysis of Edge-lit LED Backlight Technology
    The edge-lit type places LEDs on the upper and lower sides of the backlight. The layout adjustment greatly reduces the use of LEDs, thereby reducing production costs and energy consumption, which is of great benefit to manufacturers and consumers. However, its weakness of not being able to dim locally has led to th
[Power Management]
Comprehensive Analysis of Edge-lit LED Backlight Technology
Application of AT89C51 single chip microcomputer in the design of LED display screen control circuit
     LED display screen, also known as electronic display screen, is composed of LED dot matrix, which displays text, pictures, animations, and videos by turning on and off red or green lamp beads. The content can be changed at any time. It is usually composed of display module, control system and power supply system.
[Microcontroller]
Application of AT89C51 single chip microcomputer in the design of LED display screen control circuit
Teach you to design an LED flower that can sense humidity
This article will show you how to make an LED flower that can sense humidity. This video will explain how the circuit works. This is a versatile project. It can be placed in a glass container and applied almost anywhere, including clothing or accessories. Material For this circuit, here are the materials you need.
[Power Management]
Teach you to design an LED flower that can sense humidity
New breakthrough in high-power LED heat dissipation: Ceramic COB technology greatly saves packaging costs
LED packaging The method is to connect the die with its heat dissipation substrate Submount (sub-adhesion technology) through wire bonding, eutectic or flip chip packaging technology to form an LED chip , and then fix the chip on the system board to form a light source module. At present, LED
[Power Management]
New breakthrough in high-power LED heat dissipation: Ceramic COB technology greatly saves packaging costs
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号