自学stm32过程被卡住了两天,暂停了,复盘一下

本文介绍了如何在STM32F4单片机上通过RCC_AHB1ENR寄存器配置GPIOA和GPIOF组的时钟,随后展示了如何使用这些GPIO引脚实现LED流水灯的亮灭控制,强调了理论学习与实践操作的重要性。
摘要由CSDN通过智能技术生成

首先还是学会看手册很重要

要打开GPIO A组时钟,你需要执行类似的操作,只需将相应的位设置为1即。在STM32F4系列单片机中,GPIOA组的时钟使能位于RCC_AHB1ENR寄存器的第0位。

下面是打开GPIO A组时钟的代码示例:

```c
// 打开GPIO A组时钟
RCC_AHB1ENR |= (1 << 0);
```

这行代码将RCC_AHB1ENR寄存器的第0位设置为1,以启用GPIO A组的时钟。这样,你就可以在程序中使用GPIOA组的引脚了。

打开GPIO F组时钟  
    //打开F组时钟
    RCC_AHB1ENR |= (1<<5);//第五位置1

看手册中的位0和位5

    RCC_AHB1ENR |= (1<<5);//第五位置1

    RCC_AHB1ENR |= (1<<0);//第零位置1

之前因为板子还没到,一直学理论的,动手操作后才遇到很多困难,于是暂停了进度...去解决问题(叹气)~

以下是实践成功的流水灯(寄存器实现)

//main.c
#include "stm32f4xx.h"
#include "led.h"
int main(void)
{
	int j;
	LED_Init();
	while(1)
	{
		

		for(j=0;j<3;j++)
		{
			led1();
			delay(1000);
		}
		
	}

}
//led.h
#ifndef __LED_H
#define __LED_H	 
#include "stm32f4xx.h"
/*
// 定义PF9 PF10的相关寄存器
#define   RCC_AHB1ENR   *(volatile unsigned int *)(0x40023800 + 0x30)

#define   GPIOF_MODER   *(volatile unsigned int *)(0x40021400 + 0x00)         
#define   GPIOF_OTYPER  *(volatile unsigned int *)(0x40021400 + 0x04)
#define   GPIOF_OSPEEDR *(volatile unsigned int *)(0x40021400 + 0x08)
#define   GPIOF_PUPDR   *(volatile unsigned int *)(0x40021400 + 0x0C)
#define   GPIOF_BSRR    *(volatile unsigned int *)(0x40021400 + 0x18)



// 定义PE13 PE14的相关寄存器
#define   GPIOE_MODER   *(volatile unsigned int *)(0x40021000 + 0x00)         
#define   GPIOE_OTYPER  *(volatile unsigned int *)(0x40021000 + 0x04)
#define   GPIOE_OSPEEDR *(volatile unsigned int *)(0x40021000 + 0x08)
#define   GPIOE_PUPDR   *(volatile unsigned int *)(0x40021000 + 0x0C)
#define   GPIOE_BSRR    *(volatile unsigned int *)(0x40021000 + 0x18)
*/
#define   RCC_AHB1ENR   *(volatile unsigned int *)(0x40023800 + 0x30)
// 定义PA6的相关寄存器
#define GPIOA_MODER   (*(volatile unsigned int *)(0x40020000 + 0x00))
#define GPIOA_OTYPER  (*(volatile unsigned int *)(0x40020000 + 0x04))
#define GPIOA_OSPEEDR (*(volatile unsigned int *)(0x40020000 + 0x08))
#define GPIOA_PUPDR   (*(volatile unsigned int *)(0x40020000 + 0x0C))
#define GPIOA_IDR     (*(volatile unsigned int *)(0x40020000 + 0x10))
#define GPIOA_ODR     (*(volatile unsigned int *)(0x40020000 + 0x14))
#define GPIOA_BSRR    (*(volatile unsigned int *)(0x40020000 + 0x18))
#define GPIOA_LCKR    (*(volatile unsigned int *)(0x40020000 + 0x1C))
#define GPIOA_AFRL    (*(volatile unsigned int *)(0x40020000 + 0x20))
#define GPIOA_AFRH    (*(volatile unsigned int *)(0x40020000 + 0x24))

// 定义PA7的相关寄存器,偏移量与PA6相同,只需加上引脚偏移量
#define GPIOA_PIN_OFFSET 0x08 // 每个引脚寄存器偏移量
#define GPIOA_PA7_OFFSET (GPIOA_PIN_OFFSET * 7) // PA7相对于PA6的偏移量

#define GPIOA_PA7_MODER   (*(volatile unsigned int *)(0x40020000 + 0x00 + GPIOA_PA7_OFFSET))
#define GPIOA_PA7_OTYPER  (*(volatile unsigned int *)(0x40020000 + 0x04 + GPIOA_PA7_OFFSET))
#define GPIOA_PA7_OSPEEDR (*(volatile unsigned int *)(0x40020000 + 0x08 + GPIOA_PA7_OFFSET))
#define GPIOA_PA7_PUPDR   (*(volatile unsigned int *)(0x40020000 + 0x0C + GPIOA_PA7_OFFSET))
#define GPIOA_PA7_IDR     (*(volatile unsigned int *)(0x40020000 + 0x10 + GPIOA_PA7_OFFSET))
#define GPIOA_PA7_ODR     (*(volatile unsigned int *)(0x40020000 + 0x14 + GPIOA_PA7_OFFSET))
#define GPIOA_PA7_BSRR    (*(volatile unsigned int *)(0x40020000 + 0x18 + GPIOA_PA7_OFFSET))
#define GPIOA_PA7_LCKR    (*(volatile unsigned int *)(0x40020000 + 0x1C + GPIOA_PA7_OFFSET))
#define GPIOA_PA7_AFRL    (*(volatile unsigned int *)(0x40020000 + 0x20 + GPIOA_PA7_OFFSET))
#define GPIOA_PA7_AFRH    (*(volatile unsigned int *)(0x40020000 + 0x24 + GPIOA_PA7_OFFSET))

void LED_Init(void);//初始化
void delay(int s);
void led1();
#endif
//led.c
#include "led.h"

//初始化PA8和PD2为输出口.并使能这两个口的时钟		    
//LED IO初始化

/*

引脚说明
LED0 -- PA6
LED1 -- PA7
*/


void LED_Init(void) {
    // 打开GPIO A组时钟
    RCC_AHB1ENR |= (1 << 0);
	//设置 通用输出模式
	GPIOA_MODER &= ~(1<<13);
	GPIOA_MODER |= (1<<12);       //设置PA6口
	
	GPIOA_MODER &= ~(1<<15);
	GPIOA_MODER |= (1<<14);       //设置PA7口
	
	//设置推挽输出类型
	GPIOA_OTYPER &= ~(1<<6);     //设置PA6口 
	GPIOA_OTYPER &= ~(1<<7);     //设置PA7口
	
	//设置速度寄存器  25 MHz(中速)
	GPIOA_OSPEEDR &= ~(1<<13);
	GPIOA_OSPEEDR |= (1<<12);      //设置PA6口 
	
	GPIOA_OSPEEDR &= ~(1<<15);
	GPIOA_OSPEEDR |= (1<<14);      //设置PA7口 
	
	//设置上下拉模式
	GPIOA_PUPDR &= ~(1<<13);
	GPIOA_PUPDR |= (1<<12);     //设置PA6口 

	GPIOA_PUPDR &= ~(1<<15);
	GPIOA_PUPDR |= (1<<14);			//设置PA7口
}

void delay(int s)
{
	int i,j;
 
	for (i = 0;i<s;i++)
	 {
			for (j = 0;j<30000;j++)
		 {
		 }
	 
	 }
 
}
 
 
 
void led1(void)
{
    // 亮灭 PA6
    GPIOA_BSRR |= (1 << 6);  // PA6 LED1 亮
    delay(1000);
    GPIOA_BSRR |= (1 << (6 + 16));  // PA6 LED1 灭
    delay(1000);

    // 亮灭 PA7
    GPIOA_BSRR |= (1 << 7);  // PA7 LED2 亮
    delay(1000);
    GPIOA_BSRR |= (1 << (7 + 16));  // PA7 LED2 灭
    delay(1000);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值