现在的位置: 首页 > 综合 > 正文

GPIO控制

2013年09月22日 ⁄ 综合 ⁄ 共 4231字 ⁄ 字号 评论关闭

源代码: 

001 #include "stm32f10x.h"
002 #include "GLCD.h"
003 #include "USART.h"
004
005 void GPIO_Configuration(void)
006 {
007 GPIO_InitTypeDef GPIO_InitStructure;
008
009 /* Configure IO connected to LD1, LD2, LD3 and LD4 leds *********************/
010 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
011    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
012    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
013    GPIO_Init(GPIOD, &GPIO_InitStructure);
014
015     /* Configure USART1 Tx (PA.09) as alternate function push-pull */
016    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
017    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
018    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
019    GPIO_Init(GPIOA, &GPIO_InitStructure);
020    
021    /* Configure USART1 Rx (PA.10) as input floating */
022    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
023    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
024    GPIO_Init(GPIOA, &GPIO_InitStructure);
025 }
026
027 //系统中断管理
028 void NVIC_Configuration(void)
029 {
030    /* Configure the NVIC Preemption Priority Bits */ 
031    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
032
033 #ifdef  VECT_TAB_RAM 
034    /* Set the Vector Table base location at 0x20000000 */
035    NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
036 #else  /* VECT_TAB_FLASH  */
037    /* Set the Vector Table base location at 0x08000000 */
038    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  
039 #endif
040 }
041
042 //配置系统时钟,使能各外设时钟
043 void RCC_Configuration(void)
044 {
045 SystemInit();
046 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
047                            |RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
048                            |RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE
049          |RCC_APB2Periph_ADC1  | RCC_APB2Periph_AFIO
050                            |RCC_APB2Periph_SPI1, ENABLE );
051   // RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL ,ENABLE );
052      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4
053                            |RCC_APB1Periph_USART3|RCC_APB1Periph_TIM2                           
054                            , ENABLE );
055   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
056 }
057
058 void InitDis(void)
059 {
060    /* LCD Module init */
061    GLCD_init();
062    GLCD_clear(White);
063    GLCD_setTextColor(Black);
064    GLCD_displayStringLn(Line1, "     FireBull");
065    GLCD_displayStringLn(Line2, "   GPIO example");
066    GLCD_setTextColor(Red);
067 }
068
069 //配置所有外设
070 void Init_All_Periph(void)
071 {
072 RCC_Configuration();
073 // InitDis();
074 // GLCD_Test();
075 GPIO_Configuration();
076 // NVIC_Configuration();
077 // USART1_Configuration();
078 // USART1Write((u8*)"    FireBull  GPIO_example ",sizeof("    FireBull  GPIO_example "));
079 }
080
081 void Delay(vu32 nCount)
082 {
083   for(; nCount != 0; nCount--);
084 }
085
086 int main(void)
087 { 
088 Init_All_Periph();
089   while(1)
090    {
091   /* Turn on LD1 */
092      GPIO_SetBits(GPIOD, GPIO_Pin_8);
093      /* Insert delay */
094      Delay(0xAFFFF);
095
096      /* Turn on LD2 and LD3 */
097      GPIO_SetBits(GPIOD, GPIO_Pin_9 | GPIO_Pin_10);
098      /* Turn off LD1 */
099      GPIO_ResetBits(GPIOD, GPIO_Pin_8);
100      /* Insert delay */
101      Delay(0xAFFFF);
102
103      /* Turn on LD4 */
104      GPIO_SetBits(GPIOD, GPIO_Pin_11);
105      /* Turn off LD2 and LD3 */
106      GPIO_ResetBits(GPIOD, GPIO_Pin_10 | GPIO_Pin_9);
107      /* Insert delay */
108      Delay(0xAFFFF);
109
110      /* Turn off LD4 */
111      GPIO_ResetBits(GPIOD, GPIO_Pin_11);
112    }
113 }
114
115
116
117 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/kingboy100/archive/2009/11/17/4819520.aspx

 

函数 GPIO_Init

函数名

GPIO_Init

函数原形

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

功能描述

根据 GPIO_InitStruct中指定的参数初始化外设 GPIOx 寄存器

输入参数1

GPIOxx 可以是 ABCD或者 E,来选择 GPIO外设

输入参数2

GPIO_InitStruct:指向结构 GPIO_InitTypeDef的指针,包含了外设

 

typedef struct

{

u16 GPIO_Pin;

GPIOSpeed_TypeDef GPIO_Speed;

GPIOMode_TypeDef GPIO_Mode;

} GPIO_InitTypeDef;

GPIO_Pin

该参数选择待设置的 GPIO管脚,使用操作符“|”可以一次选中多个管脚。可以使用下表中的任意组合。

GPIO_Pin

描述

GPIO_Pin_None

无管脚被选中

GPIO_Pin_0

选中管脚 0

。。。

。。。

GPIO_Pin_15

选中管脚 15

GPIO_Pin_All

选中全部管脚

GPIO_Speed

GPIO_Speed 用以设置选中管脚的速率。Table 184.  给出了该参数可取的值

GPIO_Speed

描述

GPIO_Speed_10MHz

最高输出速率 10MHz

GPIO_Speed_2MHz

最高输出速率 2MHz

GPIO_Speed_50MHz

最高输出速率 50MHz

GPIO_Mode

GPIO_Mode用以设置选中管脚的工作状态。Table 185. 给出了该参数可取的值

GPIO_Speed

描述

GPIO_Mode_AIN

模拟输入

GPIO_Mode_IN_FLOATING

浮空输入

GPIO_Mode_IPD

下拉输入

GPIO_Mode_IPU

上拉输入

GPIO_Mode_Out_OD

开漏输出

GPIO_Mode_Out_PP

推挽输出

GPIO_Mode_AF_OD

复用开漏输出

GPIO_Mode_AF_PP

复用推挽输出

注意:

„  当某管脚设置为上拉或者下拉输入模式,使用寄存器 Px_BSRR PxBRR

„  GPIO_Mode 允许同时设置 GPIO方向(输入/输出)和对应的输入/输出设置, :位[7:4]对应 GPIO方向,

[4:0]对应配置。GPIO 方向有如下索引

抱歉!评论已关闭.