四、GPIO
stm32f407VG这款单片机上共有PA-PE共5×16共80个复用IO口,每个通用I / O端口有4个32位配置寄存器(GPIOx_MODERGPIOx_OTYPER GPIOx_OSPEEDR GPIOx_PUPDR),两个32位数据寄存器(GPIOx_IDR和GPIOx_ODR),一个32位的置位/复位寄存器(GPIOx_BSRR),32位锁定寄存器(GPIOx_LCKR)和两个32位的备用功能选择寄存器(GPIOx_AFRHGPIOx_AFRL)。 设置步骤: 1. 使能相关时钟 2. 设置相应的IO口为输入或输出 3. 设置输入\输出的类型 4. 设置输出的速度(如果设置为输入,此步跳过) 5. 如果输出速度>=50M,开启补偿单元 6. 设置上拉下拉寄存器 7. 通过复位置位寄存器和输入输出数据寄存器进行操作。
程序: [cpp] view plaincopy
- /************************************
- 标题:操作GPIO的练习
- 软件平台:IAR for ARM6.21
- 硬件平台:stm32f4-discovery
- 主频:168M
-
- author:小船
- data:2012-01-16
- *************************************/
-
- #include <stm32f4xx.h>
-
- uint32_t Gb_TimingDelay;
-
- void Delay(uint32_t nTime);
-
- void main ()
- {
- SysTick_Config(SystemCoreClock / 1000); //设置systemtick一毫秒中断
-
- RCC->AHB1ENR |= 0x00000008; //使能GPIOD时钟
- RCC->APB2ENR |= (1<<14); //使能syscfg时钟
-
- GPIOD->MODER &= 0x00FFFFFF; //设置PD12,13,14,15输出
- GPIOD->MODER |= 0x55000000;
-
- GPIOD->OTYPER &= 0xFFFF0FFF; //设置PD12,13,14,15推挽输出
-
- GPIOD->OSPEEDR &= 0x00FFFFFF; //PD12,13,14,15 速度100m
- GPIOD->OSPEEDR |= 0xff000000;
-
- SYSCFG->CMPCR = 0x00000001; //使用IO补偿单元,
- //当GPIO速度超过50M的时候要考虑使用此设置
-
- GPIOD->PUPDR &= 0x00FFFFFF; //PD12,13,14,15 无上拉无下拉
-
- GPIOD->BSRRH = 0xf000; //reset register GPIOx_BSRRH, write only
- //set register GPIOx_BSRRL, write only
-
- while(1)
- {
- GPIOD->BSRRH = 0xf000;
- GPIOD->BSRRL = 0x1000;
- Delay(500);
-
- GPIOD->BSRRH = 0xf000;
- GPIOD->BSRRL = 0x1000<<1;
- Delay(500);
-
- GPIOD->BSRRH = 0xf000;
- GPIOD->BSRRL = 0x1000<<2;
- Delay(500);
-
- GPIOD->BSRRH = 0xf000;
- GPIOD->BSRRL = 0x1000<<3;
- Delay(500);
- }
- }
-
- void Delay(uint32_t nTime)
- {
- Gb_TimingDelay = nTime;
-
- while(Gb_TimingDelay != 0);
- }
-
-
- void SysTick_Handler(void)
- {
- if (Gb_TimingDelay != 0x00)
- {
- Gb_TimingDelay--;
- }
- }
|