GPIO used by STM32

Publisher:创新火花Latest update time:2017-01-03 Source: eefocusKeywords:STM32  GPIO Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//******************************************************************************
//*******************************************************************
/***********************************************************************
main file, GPIO operation, complete the simplest IO operation experiment, which is to control the LED lights.
The 4 LEDs correspond to the 6th, 7th, 8th, and 9th pins of the PC respectively. 4 LEDs display water
*********************************************************************************/
#include "stm32f10x_lib.h"


GPIO_InitTypeDef GPIO_InitStructure;

void LED_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_In itStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);

}

void LED_TurnOn(u8 led)
{
  
}

void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}

main()
{

//RCC_Configuration();
LED_Init();

while(1)
{
   GPIO_SetBits(GPIOC, GPIO_Pin_9);
   Delay(0x8ffff);
   GPIO_ResetBits(GPIOC, GPIO_Pin_9);
   Delay(0x8ffff);
   GPIO_SetBits(GPIOC, GPIO_Pin_10);
   Delay(0x8ffff);
   GPIO_ResetBits(GPIOC, GPIO_Pin_10); Delay(
   0x8ffff    ); GPIO_SetBits(GPIOC,
   GPIO_Pin_11    );    Delay(0x8ffff); _12)    ;    Delay( 0x8ffff);    GPIO_ResetBits(GPIOC, GPIO_Pin_12);    Delay(0x8ffff); } } Note: RCC and GPIO library functions are used here, so these two functions must be added to the project. About the firmware library function in the folder: C:\Keil\ ARM\RV31\LIB\ST\STM32F10xIn  order to avoid changing the library functions in the KEIL folder during operation, the firmware function library can be placed in other folders, such as: E:\jy\work\STM\WxlStm32\LAB\ The stm32f10x_lib.c file in the library contains some definitions of the entire library and is required. The project after adding is: GPIO library function brief description: Function name Function description GPIO_DeInit Reinitialize peripheral device GPIOx related registers to their default reset values ​​GPIO_AFIODeInit Initialize interleaving functions (remap, event control and EXTI configuration) Register GPIO_Init According to the GPIO_initialization structure Initializes the peripheral device GPIOx with the specified elements GPIO_StructInit Fills the elements in the GPIO_initialization structure (GPIO_InitStruct) with reset values ​​GPIO_ReadInputDataBit Reads the input data of the specified port pin GPIO_ReadInputData Reads the input data of the specified port GPIO_ReadOtputDataBit Reads the output data of the specified port pin GPIO_ReadOtputData Reads the output data of the specified port GPIO_SetBits Sets the specified port pin to 1 GPIO_ResetBits Clears the specified port pin to 0 GPIO_WriteBit Sets or clears the selected data port pin GPIO_Write Writes the specified data to the GPIOx port register GPIO_ANAPinConfig Enables or disables GPIO 4 Analog input modeGPIO_PinLockConfig locks the GPIO pin registerGPIO_EventOutputConfig selects the GPIO pin as event outputGPIO_EventOutputCmd enables or disables event outputGPIO_PinRemapConfig changes the mapping of the specified pinGPIO_EMIConfig enables or disables the EMI mode of GPIO 8 and 9Extended experiment: LED lights display on the top On the basis of the key program, let's first look at the principle diagram of the key: Of course, this principle diagram is also quite simple, no need to read the explanation, the only thing to note is that the difference between the OK key and the other three keys is that pressing High level, the other three are low level when pressed. The complete list after joining is as follows: //******************************************** ***************************** // Author: JingYong // Time: 2008/4/24 //***** ************************************************** **************** /************************************* ************************************** GPIO operation, complete the simplest IO operation experiment, use Button control LED light flashing ************************************************ *****************************/ #include "stm32f10x_lib.h"










  





































GPIO_InitTypeDef GPIO_InitStructure;
//Keyboard definition
#define KEY_OK GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) 
#define KEY_DOWN GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1)
#define KEY_UP GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2)
#define KEY_ESC GPIO_ReadInputData Bit(GPIOA, GPIO_Pin_3)
//LED initialization
void LED_Init( void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
//Key initialization
void KEY_Init (void)
{
    GPIO_InitTypeDef gpio_init;
RCC_APB2PeriphClockCmd(RC C_APB2Periph_GPIOA, ENABLE);
    gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
    gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &gpio_init);
}
//Delay function
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
//Main function
main()
{
//RCC_Configuration();
LED_Init();
KEY_Init ();

while(1)
{
   if(!KEY_ESC)
   {
    while(! KEY_ESC) ;
    GPIO_SetBits(GPIOC, GPIO_Pin_9);
    Delay(0x8ffff);
    GPIO_ResetBits(GPIOC, GPIO_Pin_9);
    Delay(0x8ffff);
   }
   else if(!KEY_UP)
   {
    while(!KEY_UP) ;
    GPIO_SetBits(GPIOC, GPIO_Pin_10);
    Delay( 0x8ffff);
    GPIO_ResetBits(GPIOC, GPIO_Pin_10);
    Delay(0x8ffff);
   }
   else if(!KEY_DOWN)
   {
    while(!KEY_DOWN);
    GPIO_SetBits(GPIOC, GPIO_Pin_11);
    Delay(0x8ffff);
    GPIO_ResetBits(GPIOC, GPIO_Pin_11);
    Delay(0x8ffff);
   }
   else if(KEY_OK)
   {
    while(KEY_OK);
    GPIO_SetBits(GPIOC, GPIO_Pin_12);
    Delay(0x8ffff);
    GPIO_ResetBits( GPIOC, GPIO_Pin_12);
    Delay(0x8ffff);
   }
}
}
This example is to press different buttons to flash the corresponding LED lights.


Keywords:STM32  GPIO Reference address:GPIO used by STM32

Previous article:stm32 assembly example
Next article:stm32 rcc clock

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号