(三)通用IO端口(GPIO)初始化:
1、RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|B|C,ENABLE):使能APB2总线外设时钟;
2、RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA|B|C,DISABLE):释放GPIO复位;
3、配置各个PIN端口(模拟输入_AIN、输入浮空_IN_FLOATING、输入上拉_IPU、输入下拉_IPD、开漏输出_OUT_OD、推挽式输出_OUT_PP、推挽式复用输出_AF_PP、开漏复用输出_AF_OD)和匹配速度。
4、GPIO初始化完成
四、GPIO实例
#include "stm32f10x.h"
#include "stm32_eval.h"
GPIO_InitTypeDef GPIO_InitStructure;
void RCC_Configuration(void);
void Delay(__IO uint32_t nCount);
int main(void)
{
RCC_Configuration();
#if 0
// 配置所有未使用GPIO引脚为输入模式(浮空输入),这样可以降低功耗,并且提高器件的抗EMI/EMC 的性能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Init(GPIOC, &GPIO_InitStructure);
//armfly :注释掉的原因是当代码在外部存储器运行时,GPIOD,E,F,G部分IO用于FSMC,因此对这些IO不能重置,否则导致取指异常
// GPIO_Init(GPIOD, &GPIO_InitStructure);
// GPIO_Init(GPIOE, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_GPIOE, DISABLE);
#ifdef USE_STM3210E_EVAL
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);
// armfly :由于代码在外部SRAM运行时,GPIOD,E,F,G部分IO用于FSMC,
因此对这些IO不能重置,否则导致取指异常
//GPIO_Init(GPIOF, &GPIO_InitStructure);
//GPIO_Init(GPIOG, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, DISABLE);
#endif
#endif
// 初始化STM3210X-EVAL板上的4个LED,即初始化LED对应的IO引脚
STM_EVAL_LEDInit(LED1);
STM_EVAL_LEDInit(LED2);
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
while (1)
{
STM_EVAL_LEDOn(LED1);
// 原始值 = 0xAFFFF,但是当代码在外部RAM运行时,效率很低,会延迟10秒以上,而到代码在内部RAM或内部Flash执行时,速度很快,小于100ms
Delay(0xAFFFF);
// Turn on LD2 and LD3
STM_EVAL_LEDOn(LED2);
STM_EVAL_LEDOn(LED3);
// Turn off LD1
STM_EVAL_LEDOff(LED1);
// Insert delay
Delay(0xAFFFF);
// Turn on LD4
STM_EVAL_LEDOn(LED4);
//Turn off LD2 and LD3
STM_EVAL_LEDOff(LED2);
STM_EVAL_LEDOff(LED3);
//Insert delay
Delay(0xAFFFF);
//Turn off LD4
STM_EVAL_LEDOff(LED4);
}
}
void RCC_Configuration(void)
{
// 建立MCU系统,初始化嵌入式FLASH接口,初始化锁相环(附注1)和系统频率参数
SystemInit();
}
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
#ifdefUSE_FULL_ASSERT
// @briefReports the name of the source file and the source line number
//where the assert_param error has occurred.
//@paramfile: pointer to the source file name
// @paramline: assert_param error line source number
// @retval None
void assert_failed(uint8_t* file, uint32_t line)
{
// User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d", file, line)
//Infinite loop
while (1)
{
}
}
#endif
//******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****
void SystemInit (void)在system_stm32f10x.c中:
void SystemInit (void)
{
// 复位RCC时钟配置到默认状态(用于调试)
// Set HSION bit
RCC->CR |= (uint32_t)0x00000001;
//Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits
#ifndef STM32F10X_CL
RCC->CFGR &= (uint32_t)0xF8FF0000;
#else
RCC->CFGR &= (uint32_t)0xF0FF0000;
#endif //STM32F10X_CL
// Reset HSEON, CSSON and PLLON bits
RCC->CR &= (uint32_t)0xFEF6FFFF;
// Reset HSEBYP bit
RCC->CR &= (uint32_t)0xFFFBFFFF;
//Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits