stm32之GPIO库函数开发

关于GPIO库函数的重点函数:P122

  GPIO_Init() :根据GPIO_InitStruct中指定的参数初始化外设GPIOx寄存器;

  GPIO_ReadInputDataBit():读取指定端口管脚的输入;

  GPIO_SetBits():设置指定的数据端口位;

  GPIO_ResetBits(): 清除指定的数据端口位;

  GPIO_PinRemapConfig(): 改变指定管脚的映射;----------端口映射是很有特色的功能;也是重点知识

  GPIO_EXTILineConfig():选择GPIO管脚用作外部中断线路;

  1 /*Include---------------------------*/
  2 #include"stm32f10x_lib.h"        //包含所有的头文件
  3 #include<stdio.h>
  4 
  5 //----------------函数声明--------------------
  6 void Delay_MS(u16 dly);
  7 void RCC_Configuration(void);
  8 void GPIO_Configuration(void);
  9 
 10 
 11 /*******************************************************************************
 12 * Function Name  : main
 13 * Description    : Main program.
 14 * Input          : None
 15 * Output         : None
 16 * Return         : None
 17 *******************************************************************************/ 
 18 int main(void)
 19 {
 20     u8  data, i;
 21     #ifdef DEBUG
 22     debug();
 23     #endif
 24     //------------初始化------------
 25     RCC_Configuration();
 26     GPIO_Configuration();
 27 
 28     //------------164通信-----------
 29     //CLK:PB5 上升沿 CLR:PE11置1 DATA:PE10
 30     GPIO_SetBits(GPIOE, GPIO_Pin_11);
 31     data = 0x30;
 32     for(i=0;i<8;++i)
 33     {
 34         GPIO_ResetBits(GPIOB, GPIO_Pin_5);    //PB5清零
 35         if((data&0x80) == 0x00)
 36             GPIO_ResetBits(GPIOE, GPIO_Pin_10);
 37         else
 38             GPIO_SetBits(GPIOE, GPIO_Pin_10);
 39         data<<=1;    //左移一位并赋值给data
 40         GPIO_SetBits(GPIOB, GPIO_Pin_5);    //PB5置1,为了产生上升沿    
 41     }
 42 
 43 
 44     while(1)
 45     {
 46         GPIO_SetBits(GPIOA, GPIO_Pin_3);
 47         Delay_MS(1000);
 48         GPIO_ResetBits(GPIOA, GPIO_Pin_3);
 49         Delay_MS(1000);    
 50     }
 51         
 52 }
 53 
 54 /*******************************************************************************
 55 * Function Name  : Delay_Ms
 56 * Description    : delay 1 ms.
 57 * Input          : dly (ms)
 58 * Output         : None
 59 * Return         : None
 60 *******************************************************************************/
 61 void Delay_MS(u16 dly)
 62 {
 63     u16 i,j;
 64     for(i=0;i<dly;i++)
 65         for(j=1000;j>0;j--);
 66 }
 67 
 68 /*******************************************************************************
 69 * Function Name  : RCC_Configuration
 70 * Description    : Configures the different system clocks.
 71 * Input          : None
 72 * Output         : None
 73 * Return         : None
 74 *******************************************************************************/
 75 void RCC_Configuration(void)
 76 {
 77     //----------使用外部RC晶振-----------
 78     RCC_DeInit();            //初始化为缺省值
 79     RCC_HSEConfig(RCC_HSE_ON);    //使能外部的高速时钟 
 80     while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);    //等待外部高速时钟使能就绪
 81     
 82     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);    //Enable Prefetch Buffer
 83     FLASH_SetLatency(FLASH_Latency_2);        //Flash 2 wait state
 84     
 85     RCC_HCLKConfig(RCC_SYSCLK_Div1);        //HCLK = SYSCLK
 86     RCC_PCLK2Config(RCC_HCLK_Div1);            //PCLK2 =  HCLK
 87     RCC_PCLK1Config(RCC_HCLK_Div2);            //PCLK1 = HCLK/2
 88     RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);    //PLLCLK = 8MHZ * 9 =72MHZ
 89     RCC_PLLCmd(ENABLE);            //Enable PLLCLK
 90 
 91     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);    //Wait till PLLCLK is ready
 92     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);    //Select PLL as system clock
 93     while(RCC_GetSYSCLKSource()!=0x08);        //Wait till PLL is used as system clock source
 94     
 95     //---------打开相应外设时钟--------------------
 96     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);    //使能APB2外设的GPIOA的时钟
 97     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE);             
 98 }
 99 
100 /*******************************************************************************
101 * Function Name  : GPIO_Configuration
102 * Description    : 初始化GPIO外设
103 * Input          : None
104 * Output         : None
105 * Return         : None
106 *******************************************************************************/
107 void GPIO_Configuration(void)
108 {
109     //CLK:PB5  CLR:PE11 DATA:PE10
110     GPIO_InitTypeDef    GPIO_InitStructure;        //声明一个结构体变量
111     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;     //选择PB.5
112     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     //管脚频率为50MHZ
113     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     //输出模式为推挽输出
114     GPIO_Init(GPIOB,&GPIO_InitStructure);                 //初始化GPIOB寄存器
115     
116     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 |GPIO_Pin_10 ;     //选择PE.10 PE.11
117     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     //管脚频率为50MHZ
118     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     //输出模式为推挽输出
119     GPIO_Init(GPIOE,&GPIO_InitStructure);                 //初始化GPIOE寄存器
120     
121     //开启时钟    必须在RCC_Configuration中设置        
122 } 
原文地址:https://www.cnblogs.com/chris-cp/p/3916517.html