好文档 - 专业文书写作范文服务资料分享网站

STM32启动代码分析、简化、实战 

天下 分享 时间: 加入收藏 我要投稿 点赞

8. 勾选Create HEX File

9. 编译链接,产生hex文件。

10. 下载到开发板上运行。

11. 运行结果

/*************************************************************************/ /* 本程序精炼自STM32官方固件库,留下必要的代码,并做了详细说明 */ /*************************************************************************/ /* 定义时钟结构体 */

typedef struct {

volatile unsigned int CR; volatile unsigned int CFGR; volatile unsigned int CIR;

volatile unsigned int APB2RSTR; volatile unsigned int APB1RSTR; volatile unsigned int AHBENR; volatile unsigned int APB2ENR; volatile unsigned int APB1ENR; volatile unsigned int BDCR; volatile unsigned int CSR; volatile unsigned int AHBRSTR; volatile unsigned int CFGR2;

} RCC_TypeDef;

/* 定义时钟结构体的指针变量 */

#define RCC ((RCC_TypeDef *)0x40021000)

/* memory mapped structure for System Control Block (SCB) */ typedef struct {

volatile unsigned int CPUID; volatile unsigned int ICSR;

volatile unsigned int VTOR; /* 存放中断向量表的起始地址的寄存器 */ volatile unsigned int AIRCR; volatile unsigned int SCR; volatile unsigned int CCR; volatile unsigned int SHP[12]; volatile unsigned int SHCSR; volatile unsigned int CFSR; volatile unsigned int HFSR; volatile unsigned int DFSR; volatile unsigned int MMFAR; volatile unsigned int BFAR; volatile unsigned int AFSR; volatile unsigned int DFR; volatile unsigned int ADR; volatile unsigned int MMFR[4]; volatile unsigned int ISAR[5];

} SCB_Type; #define SCB ((SCB_Type *)0xE000ED00) /* 定义对应结构体指针变量 */

/* 定义GPIO结构体 */

typedef struct {

volatile unsigned int CRL; /*配置寄存器GPIOx_CRL,偏移地址00*/ volatile unsigned int CRH; /*配置寄存器GPIOx_CRH,偏移地址04*/ volatile unsigned int IDR; /*数据寄存器GPIOx_IDR, 偏移地址08 */ volatile unsigned int ODR; /*数据寄存器GPIOx_ODR,偏移地址0C*/ volatile unsigned int BSRR; /*置位寄存器GPIOx_BSRR, 偏移地址10*/ volatile unsigned int BRR; /*复位寄存器GPIOx_BRR,偏移地址14*/ volatile unsigned int LCKR; /*锁定寄存器GPIOx_LCKR,偏移地址18*/ } GPIO_TypeDef;

/* 定义GPIO结构体指针变量 */

#define GPIOD ((GPIO_TypeDef *)0x40011400)

/*************************************************************************/ /* main函数 */ /*************************************************************************/ int main(void) {

/* 初始化系统时钟 */

RCC->CR |= (unsigned int)0x00000001; RCC->CFGR &= (unsigned int)0xF8FF0000; RCC->CR &= (unsigned int)0xFEF6FFFF; RCC->CR &= (unsigned int)0xFFFBFFFF; RCC->CFGR &= (unsigned int)0xFF80FFFF; RCC->CR &= (unsigned int)0xEBFFFFFF; RCC->CIR = 0x00FF0000; RCC->CFGR2 = 0x00000000; /* Vector Table Relocation in Internal SRAM.*/ SCB->VTOR = ((unsigned int)0x20000000); /* 打开GPIOD口时钟 */

RCC->APB2ENR |= 0x20;

/* 设置GPIOD第2脚为推挽输出模式,速率为50MHz */ GPIOD->CRL &= 0xFFFFF0FF; GPIOD->CRL |= 0x00000300; /* 点亮PD2口的LED */ GPIOD->BRR &= 0xFFFFFFFB; while (1) { }

}

/**************************** *****END OF FILE**********************/

STM32启动代码分析、简化、实战 

8.勾选CreateHEXFile9.编译链接,产生hex文件。10.下载到开发板上运行。11.运行结果/********************************************************************
推荐度:
点击下载文档文档为doc格式
3rmzx269sd48fsc2ak5p
领取福利

微信扫码领取福利

微信扫码分享