stm32f407探索者开发板(五)——跑马灯实验(寄存器版本)
一、步骤
- 使能I0口时钟。配置相关寄存器寄存器RCC->AHB1ENR
- 初始化IO口模式。配置四个配置寄存器
GPIOx_MODER/GPIOx_OTYPER/GPIOx_OSPEEDR/GPIOx_PUPDR - 操作I0口,输出高低电平。配置寄存器GPIOX_ ODR或者BSRRL/BSRRH。
二、配置RCC->AHB1ENR
#include "led.h"
#include "stm32f4xx.h"
void LED_Init(void)
{
RCC->AHB1ENR|= 1<<5; //将第五位设置为1其余不变
//PF9
//PF10
}
三、配置IO口模式
红框区域配置为01,其余位置不变
先清零,再置1
//清零:要18、19位为00,取反为11,即3左移2*9
GPIOF->MODER &= ~(3<<2*9);
//18位赋值为1,结合上述则为01
GPIOF->MODER |= 1<<(2*9);
配置成50MHz,为10
GPIOF->OSPEEDR &= ~(3<<2*9);
GPIOF->OSPEEDR |= 2<<(2*9);
设置为01上拉
GPIOF->PUPDR &= ~(3<<2*9);
GPIOF->PUPDR |= 1<<(2*9);
GPIOF->OTYPER &= ~(1<<9);//一个位配置一个寄存器
GPIOF->OTYPER |= 0<<9;
就是输出
四、代码展示
4.1 main.c
#include "stm32f4xx.h"
#include "led.h"
#include "delay.h"
int main(void)
{
delay_init(168);
LED_Init();
while(1){
GPIOF->ODR &= ~(1<<9);
GPIOF->ODR &= ~(1<<10);
delay_ms(500);
GPIOF->ODR |= 1<<9;
GPIOF->ODR |= 1<<10;
delay_ms(1000);
}
}
4.2 led.c
#include "led.h"
#include "stm32f4xx.h"
void LED_Init(void)
{
RCC->AHB1ENR|= 1<<5; //将第五位设置为1其余不变
//PF9
//清零:要18、19位为00,取反为11,即3左移2*9
GPIOF->MODER &= ~(3<<2*9);
//18位赋值为1,结合上述则为01
GPIOF->MODER |= 1<<(2*9);
GPIOF->OSPEEDR &= ~(3<<2*9);
GPIOF->OSPEEDR |= 2<<(2*9);
GPIOF->PUPDR &= ~(3<<2*9);
GPIOF->PUPDR |= 1<<(2*9);
GPIOF->OTYPER &= ~(1<<9);//一个位配置一个寄存器
GPIOF->OTYPER |= 0<<9;
GPIOF->ODR |= 1<<9;//1
//GPIOF->ODR &= ~(1<<9);
//PF10
GPIOF->MODER &= ~(3<<2*10);
GPIOF->MODER |= 1<<(2*10);
GPIOF->OSPEEDR &= ~(3<<2*10);
GPIOF->OSPEEDR |= 2<<(2*10);
GPIOF->PUPDR &= ~(3<<2*10);
GPIOF->PUPDR |= 1<<(2*10);
GPIOF->OTYPER &= ~(1<<10);//一个位配置一个寄存器
GPIOF->OTYPER |= 0<<10;
GPIOF->ODR |= 1<<10;//1 输出
}
4.3 led.h
#ifndef __LED_H_
#define __LED_H_
void LED_Init(void);
#endif