程序代码如下:
#include "stm32f10x.h"
int main (void)
{
#if 0
// 开GPIOB端口时钟
RCC_APB2ENR |= ( (1) << 3 );
// 配置IO口为通用推免输出模式
GPIOB->CRH &= ~( (0xf0000000) << (8*0) );
GPIOB->CRH |= ( (1) << (28*1) );
// 控制 ODR 寄存器
//;GPIOB_ODR &= ~(1<<0);
//GPIOB->ODR |= ((1)<<(15*1));
GPIOB->ODR&=~(0x00008000);
RCC_APB2ENR |= ( (1) << 3 );
// 配置IO口为通用推免输出模式
GPIOB->CRH &= ~( (0x0f000000) << (7*0) );
GPIOB->CRH |= ( (1) << (24*1) );
// 控制 ODR 寄存器
GPIOB->ODR |= ((1)<<(14*1));
#elif 1
//开GPIOB口端口时钟
RCC_APB2ENR &=~((0x00000008)<<(3*0));
RCC_APB2ENR |=((1)<<(1*3));
//配置GPIO端口为通用推免输出模式
GPIOB->CRH&=~((0x0f000000)<<((7)*0));
GPIOB->CRH|=((1)<<(24*1));
GPIOB->CRL&=~((0x000000f0)<<((2)*0));
GPIOB->CRL|=((1)<<(4*1));
//控制GPIOB口输出数据
GPIOB->ODR&=~((0x00004002)<<((14)*0));
GPIOB->ODR|=(1<<(1*1));
GPIOB->ODR|=(1<<(14*1));
#endif
}
void SystemInit(void)
{
// 函数体为空,目的是为了骗过编译器不报错
}
头文件:
// 用来存放STM32寄存器映射的代码
// 外设 perirhral
#define PERIPH_BASE ((unsigned int)0x40000000)
#define APB1PERIPH_BASE PERIPH_BASE
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
#define RCC_BASE (AHBPERIPH_BASE + 0x1000)
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
#define RCC_APB2ENR *(unsigned int*)(RCC_BASE + 0x18)
//#define GPIOB_CRL *(unsigned int*)(GPIOB_BASE + 0x00)
//#define GPIOB_CRH *(unsigned int*)(GPIOB_BASE + 0x04)
//#define GPIOB_ODR *(unsigned int*)(GPIOB_BASE + 0x0C)
typedef unsigned int uint_32;
typedef struct
{
uint_32 CRL;
uint_32 CRH;
uint_32 IDR;
uint_32 ODR;
uint_32 BSRR;
uint_32 BRR;
uint_32 LCKR;
} GPIOB_Typedef;
#define GPIOB ((GPIOB_Typedef*)(GPIOB_BASE))
实验验证现场照片: