一、前期准备:
1、STM32F4xx中文参考手册.pdf 先上代码
#include "stm32f4xx.h"
#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_ODR ( *(volatile unsigned int *)(0x40021400 + 0x14) )
#define GPIOA_MODER ( *(volatile unsigned int *)(0x40020000 + 0x00) )
#define GPIOA_IDR ( *(volatile unsigned int *)(0x40020000 + 0x10) )
int main()
{
RCC->AHB1ENR |= (1<<5);
GPIOF_MODER |= (1<<18);
GPIOF_MODER &= ~(1<<19);
GPIOF_OTYPER &= ~(1<<9);
GPIOF_OSPEEDR |= (3<<18);
GPIOF_PUPDR |= (1<<18);
GPIOF_PUPDR &= ~(1<<19);
GPIOA_MODER &= ~(1<<0);
GPIOA_MODER &= ~(1<<1);
GPIOF_ODR |= (1<<9);
while(1)
{
if( GPIOA_IDR & 0x1 )
{
GPIOF_ODR &= ~(1<<9);
}
else
{
GPIOF_ODR |= (1<<9);
}
}
}
这个代码是基于STM32寄存器开发之点亮led灯 的基础上做了调整,用按键实现点亮led灯,本文章仅介绍按键部分的代码。
#define GPIOA_MODER ( *(volatile unsigned int *)(0x40020000 + 0x00) )
章节:2.3 存储器映射 上图得知GPIOA的起始地址是0x40020000 因为按键的串口位置是PF0,它在0位置
#define GPIOA_IDR ( *(volatile unsigned int *)(0x40020000 + 0x10) )
输入数据寄存器偏移地址是0x10
GPIOA_MODER &= ~(1<<0);
GPIOA_MODER &= ~(1<<1);
while(1)
{
if( GPIOA_IDR & 0x1 )
{
GPIOF_ODR &= ~(1<<9);
}
else
{
GPIOF_ODR |= (1<<9);
}
}
|