源代码:
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 | GPIOx:x 可以是 A,B,C,D或者 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 方向有如下索引
- GPIO输入模式 = 0x00
- GPIO输出模式 = 0x01
Table 186. 给出了所有 GPIO_Mode 的索引和编码
GPIO方向 | 索引 | 模式 | 设置 | 模式代码 |
GPIO Input | 0x00 | GPIO_Mode_AIN | 0x00 | 0x00 |
GPIO_Mode_IN_FLOATING | 0x04 | 0x04 | ||
GPIO_Mode_IPD | 0x08 | 0x28 | ||
GPIO_Mode_IPU | 0x08 | 0x48 | ||
GPIO Output | 0x01 | GPIO_Mode_Out_OD | 0x04 | 0x14 |
GPIO_Mode_Out_PP | 0x00 | 0x10 | ||
GPIO_Mode_AF_OD | 0x0C | 0x1C | ||
GPIO_Mode_AF_PP | 0x08 | 0x18 |
例:
/* Configure all the GPIOA in Input Floating mode */
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//*********************************//
函数名 | NVIC_PriorityGroupConfig |
函数原形 | void NVIC_PriorityGroupConfig(u32 NVIC_PriorityGroup) |
功能描述 | 设置优先级分组:先占优先级和从优先级 |
输入参数 | NVIC_PriorityGroup:优先级分组位长度 |
先决条件 | 优先级分组只能设置一次 |
NVIC_PriorityGroup
该参数设置优先级分组位长度(见 Table 270.)
NVIC_PriorityGroup | 描述 |
NVIC_PriorityGroup_0 | 先占优先级 0 位 从优先级 4位 |
NVIC_PriorityGroup_1 | 先占优先级 1 位 从优先级 3位 |
NVIC_PriorityGroup_2 | 先占优先级 2 位 从优先级 2位 |
NVIC_PriorityGroup_3 | 先占优先级 3 位 从优先级 1位 |
NVIC_PriorityGroup_4 | 先占优先级 4 位 从优先级 0位 |
例:
/* Configure the Priority Grouping with 1 bit */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//*********************//
函数名 | NVIC_SetVectorTable |
函数原形 | Void NVIC_SetVectorTable(u32 NVIC_VectTab, u32 Offset) |
功能描述 | 设置向量表的位置和偏移 |
输入参数1 | NVIC_VectTab:指定向量表位置在 RAM 还是在程序存储器 |
输入参数2 | Offset:向量表基地址的偏移量 对 FLASH,该参数值必须高于 0x08000100;对 RAM 必须高于 0x100。它同时必须是 256(64×4)的整数倍 |
返回值 | 指定中断活动位的新状态(SET 或者 RESET) |
NVIC_VectTab
该参数设置向量表基地址(见 Table 290.)
NVIC_ VectTab | 描述 |
NVIC_VectTab_FLASH | 向量表位于 FLASH |
NVIC_VectTab_RAM | 向量表位于 RAM |
例:
/* Vector Table is in FLASH at 0x0 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
//********************************//
SystemInit(); //配置时钟时必须有
//********************************//
函数名 | RCC_APB2PeriphClockCmd |
函数原形 | void RCC_APB2PeriphClockCmd(u32 RCC_APB2Periph, FunctionalState NewState) |
功能描述 | 使能或者失能 APB2 外设时钟 |
输入参数 1 | RCC_APB2Periph: 门控 APB2 外设时钟 |
输入参数 2 | NewState:指定外设时钟的新状态 这个参数可以取:ENABLE 或者 DISABLE |
RCC_APB2Periph
该参数被门控的APB2外设时钟,可以取下表的一个或者多个取值的组合作为该参数的值。
RCC_AHB2Periph | 描述 |
RCC_APB2Periph_AFIO | 功能复用 IO时钟 |
RCC_APB2Periph_GPIOA | GPIOA 时钟 |
RCC_APB2Periph_GPIOB | GPIOB 时钟 |
RCC_APB2Periph_GPIOC | GPIOC 时钟 |
RCC_APB2Periph_GPIOD | GPIOD 时钟 |
RCC_APB2Periph_GPIOE | GPIOE 时钟 |
RCC_APB2Periph_ADC1 | ADC1 时钟 |
RCC_APB2Periph_ADC2 | ADC2 时钟 |
RCC_APB2Periph_TIM1 | TIM1 时钟 |
RCC_APB2Periph_SPI1 | SPI1 时钟 |
RCC_APB2Periph_USART1 | USART1 时钟 |
RCC_APB2Periph_ALL | 全部 APB2外设时钟 |
例:
/* Enable GPIOA, GPIOB and SPI1 clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB | RCC_APB2Periph_SPI1, ENABLE);
//*******************************************//
函数名 | RCC_APB1PeriphClockCmd |
函数原形 | void RCC_APB1PeriphClockCmd(u32 RCC_APB1Periph, FunctionalState NewState) |
功能描述 | 使能或者失能 APB1 外设时钟 |
输入参数 1 | RCC_APB1Periph: 门控 APB1 外设时钟 |
输入参数 2 | NewState:指定外设时钟的新状态 这个参数可以取:ENABLE 或者 DISABLE |
RCC_APB1Periph
该参数被门控的APB1外设时钟,可以取下表的一个或者多个取值的组合作为该参数的值。
RCC_AHB1Periph | 描述 |
RCC_APB1Periph_TIM2 | TIM2 时钟 |
RCC_APB1Periph_TIM3 | TIM3 时钟 |
RCC_APB1Periph_TIM4 | TIM4 时钟 |
RCC_APB1Periph_WWDG | WWDG时钟 |
RCC_APB1Periph_SPI2 | SPI2 时钟 |
RCC_APB1Periph_USART2 | USART2 时钟 |
RCC_APB1Periph_USART3 | USART3 时钟 |
RCC_APB1Periph_I2C1 | I2C1 时钟 |
RCC_APB1Periph_I2C2 | I2C2 时钟 |
RCC_APB1Periph_USB | USB 时钟 |
RCC_APB1Periph_CAN | CAN时钟 |
RCC_APB1Periph_BKP | BKP时钟 |
RCC_APB1Periph_PWR | PWR 时钟 |
RCC_APB1Periph_ALL | 全部 APB1外设时钟 |
例:
/* Enable BKP and PWR clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,
ENABLE);
//************************************//
函数名 | RCC_AHBPeriphClockCmd |
函数原形 | void RCC_AHBPeriphClockCmd(u32 RCC_AHBPeriph, FunctionalState NewState) |
功能描述 | 使能或者失能 AHB 外设时钟 |
输入参数 1 | RCC_AHBPeriph: 门控 AHB 外设时钟 |
输入参数 2 | NewState:指定外设时钟的新状态 这个参数可以取:ENABLE 或者 DISABLE |
RCC_AHBPeriph
该参数被门控的AHB外设时钟,可以取下表的一个或者多个取值的组合作为该参数的值。
RCC_AHBPeriph | 描述 |
RCC_AHBPeriph_DMA | DMA时钟 |
RCC_AHBPeriph_SRAM | SRAM 时钟 |
RCC_AHBPeriph_FLITF | FLITF时钟 |
1. SRAM和FLITF时钟只能在睡眠(SLEEP)模式下被失能。
例:
/* Enable DMA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA);