STM32 button control LED (without firmware library)

Publisher:Serendipitous33Latest update time:2019-04-10 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

       The STM32F407ZET6 is used. The four pins of the controlled LED lights are LED0 -> PF9, LED1 -> PF10, LED2-> PE13, LED3 -> PE14. The four pins of the buttons are KEY0--> PA0, KEY1--> PE2, KEY2--> PE3, KEY3--> PE4.


       In the loop, determine whether the button is pressed (the corresponding pin input will become 0). If it is pressed, the corresponding LED pin will output a low level and light up.


    1. Initialize the registers of LED lights and buttons respectively.


Initialize the LED light (set various registers): set the GPIO clock, configure the mode register (general output type), configure the output type (set to output push), set the register that controls the output rate, and finally configure the output data register (let the LED light be off by default)


Initialize the buttons (set various registers): set the GPIO clock, configure the key register (set to input mode), and set the register that controls the output rate.


     2. In the main function, keep checking whether the button is pressed (whether the corresponding pin level is 0)


The function is implemented as follows:


#define rRCCAHB1CLKEN   *((volatile unsigned long *)0x40023830) 


#define rGPIOF_MODER  *((volatile unsigned long *)0x40021400)   

#define rGPIOF_OTYPER *((volatile unsigned long *)0x40021404) 

#define rGPIOF_OSPEEDR  *((volatile unsigned long *)0x40021408) 

#define rGPIOF_IDR  *((volatile unsigned long *)0x40021410) 

#define rGPIOF_ODR  *((volatile unsigned long *)0x40021414) 

 

 

#define rGPIOE_MODER  *((volatile unsigned long *)0x40021000)

#define rGPIOE_OTYPER *((volatile unsigned long *)0x40021004)

#define rGPIOE_OSPEEDR  *((volatile unsigned long *)0x40021008)

#define rGPIOE_IDR  *((volatile unsigned long *)0x40021010)

#define rGPIOE_ODR  *((volatile unsigned long *)0x40021014)

 

 

#define rGPIOA_MODER  *((volatile unsigned long *)0x40020000)

#define rGPIOA_OTYPER *((volatile unsigned long *)0x40020004)

#define rGPIOA_OSPEEDR  *((volatile unsigned long *)0x40020008)

#define rGPIOA_IDR  *((volatile unsigned long *)0x40020010)

#define rGPIOA_ODR  *((volatile unsigned long *)0x40020014)

void key_init()

{




rRCCAHB1CLKEN |= 1 | (1 << 1);




rGPIOA_MODER&=~(1|(1<<1));


rGPIOF_OSPEEDR &= ~(1 | (1 << 1) );




rGPIOE_MODER&= ~(0x3f<<4);


rGPIOE_MODER &= ~(0x3f<<4);

}

void led_init()

{


rRCCAHB1CLKEN |= (1 << 5) | (1 << 4);

 


rGPIOF_MODER &= ~((0x3 << 18) | (0x3 << 20));

rGPIOF_MODER |= (1 << 18) | (1 << 20);  

 

 

rGPIOF_OTYPER &= ~( (1 << 9) | (1 << 10));


 

 

rGPIOF_OSPEEDR &= ~((0x3 << 18) | (0x3 << 20) );


rGPIOF_ODR  |=  (1 << 9 | 1 << 10) ;

 

 

 

rGPIOE_MODER &= ~((0X3 << 26) | (0X3 << 28) );

rGPIOE_MODER |= (1 << 26) | (1 << 28);

 

rGPIOE_OTYPER &= ~( (1 << 13) | (1 << 14));

 

rGPIOE_OSPEEDR &= ~((0x3 << 26) | (0x3 << 28) );

 

rGPIOE_ODR  |=  (1 << 13 | 1 << 14) ;

 


}

 

 

void delay(int i)

{

int v = i;

while(v--);

}

 

void led_on(int i)

{

if (i == 0)

{

rGPIOF_ODR &= ~(1 << 9);

rGPIOF_ODR |= 1 << 10;

 

rGPIOE_ODR |= (1 << 13) | (1 << 14);

}

else if (i == 1)

{

rGPIOF_ODR |= (1 << 9);

rGPIOF_ODR &= ~(1 << 10);

 

rGPIOE_ODR |= (1 << 13) | (1 << 14);


}

else if (i == 2)

{

rGPIOF_ODR |= (1 << 9) | (1 << 10);

 

rGPIOE_ODR &= ~(1 << 13);

rGPIOE_ODR |= 1 << 14;

}

else if (i == 3)

{

rGPIOF_ODR |= (1 << 9) | (1 << 10);

 

rGPIOE_ODR &= ~(1 << 14);

rGPIOE_ODR |= 1 << 13;

}

}

 

int main()

{

int i = 0;

led_heat();

key_init();

while(1)

{


if(!(rGPIOA_IDR&1))

       {

delay(50); //debounce

if(!(rGPIOA_IDR&1))

{

led_on(0);

}

}

else

{

rGPIOF_ODR |= 1 << 9;//µÆÃð

}

if(!(rGPIOE_IDR&(1<<2)))

       {

delay(50);

if(!(rGPIOE_IDR&(1<<2)))

{

led_on(1);

}

}

else

{

rGPIOF_ODR |= 1 << 10;

}

if(!(rGPIOE_IDR&(1<<3)))

       {

delay(50);

if(!(rGPIOE_IDR&(1<<3)))

{

led_on(2);

}

}

else

{

rGPIOE_ODR |= 1 << 13;

}

if(!(rGPIOE_IDR&(1<<4)))

       {

delay(50);

if(!(rGPIOE_IDR&(1<<4)))

{

led_on(3);

}

}

else

{

rGPIOE_ODR |= 1 << 14;

}


}

}

Keywords:STM32 Reference address:STM32 button control LED (without firmware library)

Previous article:STM32-1-LED on and off
Next article:STM32 serial communication--data packaging and sending

Recommended ReadingLatest update time:2024-11-23 02:34

When does the STM32 AFIO clock start?
Why turn on the clock in the first place? Answer: Because you need to read and write registers! In STM32, you need to turn on the clock corresponding to the register to read and write registers . Then it is clear when the AFIO clock is turned on (this is true for all clocks): the AFIO clock is turned on when the "AFIO
[Microcontroller]
Analyzing the STM32 startup process
================================================== ================================================== ============ Compared with the mainstream ARM7/ARM9 core architecture of the previous generation of ARM, the startup method of the new generation Cortex core architecture has changed significantly. After the ARM7/AR
[Microcontroller]
Design considerations for blue light "LED incandescent lamp"
Incandescent lamps have been used around the world for more than 100 years since their invention. Incandescent lamps add AC power directly to a tungsten filament resistor to generate heat and light, thus illuminating the darkness. They have brought brightness and happiness to hundreds of millions of people around th
[Power Management]
Design considerations for blue light
STM32 BOOT1 and BOOT0 settings
OOT1 and BOOT0 have two values, 0 and 1, respectively, which can be combined into three boot modes: BOOT1=X, BOOT0=0   Boot mode: User Flash Memory Boot from user flash memory BOOT1=0, BOOT0=0   Boot mode: System Memory Boot from system memory BOOT1=1, BOOT0=0   Boot mode: Embedded SPAM Boot from built-in SRAM  
[Microcontroller]
LED driver power accuracy
When it comes to the accuracy of LED driver power supply, we usually think of constant current error. In fact, the driving accuracy is not limited to current accuracy. LED is a typical current-driven device. Accurate control of LED driver power supply can determine many parameters including light efficiency, power eff
[Power Management]
STM32 PWM Introduction and Breathing Light Experiment
1. Introduction to PWM Pulse Width Modulation (PWM) is a very effective technology that uses the digital output of a microprocessor to control analog circuits. Simply put, it is the control of pulse width. The schematic diagram is as follows: When CNT CCRx, the output is 0, otherwise, the output is 1. Therefore, t
[Microcontroller]
STM32 PWM Introduction and Breathing Light Experiment
Understanding the operation of STM32 bit band
    With the support of bit operations in the Cortex-M3, single bits can be read and written using normal load/store instructions.   In the bit-banding supported by the CM3, bit-banding is implemented in two regions.   One is the lowest 1MB range of the SRAM region, 0x20000000 ‐ 0x200FFFFF (the lowest 1MB in the SRAM
[Microcontroller]
New thermal management solution improves performance of LED lighting components
To help extend the performance and life of LEDs (light emitting diodes) and LED components, Momentive Advanced Materials has introduced a new series of thermally conductive silicone products for use in LED production and assembly. The new products include the TIA series of curable hot melt adhesives and a
[Power Management]
Latest Microcontroller Articles
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号