一、开发环境配置
1.keil5软件安装
(1)在微信公众号或者优快云博客上下载安装包,和注册机软件。
(2)打开安装包,→next
(4)更改路径,→next
(5)→NEXT
(6)安装中
(7)安装完成后关闭,使用管理员身份打开,点击许可与管理,英文License Management
(8) 将CID填入,Generate生成
(9)注册信息填入
(10)安装完成
2.Keil5 新建工程
(1)新建工程项目
(2)选择路径,取工程名
(3)选择型号
(4)勾选对应选项
(5)新建main.c文件(CTRL+N)
二、程序代码编写
1.使用寄存器方式点亮LED灯
//用来存放STM寄存器映射
#define PERIPH_BASE ((unsigned int)0x40000000)//AHB
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
#define GPIOA_BASE (APB2PERIPH_BASE + 0x0800)
//GPIOA_BASE=0x40000000+0x10000+0x0800=0x40010800,????GPIOA????
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
//GPIOB_BASE=0x40000000+0x10000+0x0C00=0x40010C00,????GPIOB????
#define GPIOC_BASE (APB2PERIPH_BASE + 0x1000)
//GPIOC_BASE=0x40000000+0x10000+0x1000=0x40011000,????GPIOC????
#define GPIOD_BASE (APB2PERIPH_BASE + 0x1400)
//GPIOD_BASE=0x40000000+0x10000+0x1400=0x40011400,????GPIOD????
#define GPIOE_BASE (APB2PERIPH_BASE + 0x1800)
//GPIOE_BASE=0x40000000+0x10000+0x0800=0x40011800,????GPIOE????
#define GPIOF_BASE (APB2PERIPH_BASE + 0x1C00)
//GPIOF_BASE=0x40000000+0x10000+0x0800=0x40011C00,????GPIOF????
#define GPIOG_BASE (APB2PERIPH_BASE + 0x2000)
//GPIOG_BASE=0x40000000+0x10000+0x0800=0x40012000,????GPIOG????
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define LED0 MEM_ADDR(BITBAND(GPIOC_ODR_Addr,13))
//#define LED0 *((volatile unsigned long *)(0x422101a0)) //PA8
/?typedef????
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;
} RCC_TypeDef;
#define RCC ((RCC_TypeDef *)0x40021000)
/?typedef????
typedef struct
{
volatile unsigned int CRL;
volatile unsigned int CRH;
volatile unsigned int IDR;
volatile unsigned int ODR;
volatile unsigned int BSRR;
volatile unsigned int BRR;
volatile unsigned int LCKR;
} GPIO_TypeDef;
//GPIOA????GPIOA_BASE,GPIOA_BASE??????????GPIO_TypeDef
#define GPIOC ((GPIO_TypeDef *)GPIOC_BASE)
void LEDInit( void )
{
RCC->APB2ENR|=1<<4; //使能PORTA时钟
GPIOC->CRH&=0XFFFFFFF0;
GPIOC->CRH|=0X00000003;//PA8 推挽输出
GPIOC->ODR|=1<<13; //PA8 输出高
}
/???
void Delay_ms( volatile unsigned int t)
{
unsigned int i,n;
for (n=0;n<t;n++)
for (i=0;i<800;i++);
}
int main(void)
{
LEDInit();
while (1)
{
LED0=0;//LED亮
Delay_ms(500);//延时
LED0=1;//LED灭
Delay_ms(500);//延时
}
}
2.使用库函数方式点亮LED灯
#include "sys.h"
#include "delay.h"
#include "usart.h"
#define LED_Pin GPIO_PIN_13
#define LED_GPIO_Port GPIOC
void Delay(__IO uint32_t nCount);
void Delay(__IO uint32_t nCount)
{
while(nCount--){}
}
int main(void)
{
GPIO_InitTypeDef GPIO_Initure;
HAL_Init(); //初始化HAL库
Stm32_Clock_Init(RCC_PLL_MUL9); //设置时钟,72M
__HAL_RCC_GPIOC_CLK_ENABLE(); //开启GPIOA时钟
GPIO_Initure.Pin=LED_Pin; //PA8
GPIO_Initure.Mode=GPIO_MODE_OUTPUT_PP; //推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH; //高速
HAL_GPIO_Init(LED_GPIO_Port,&GPIO_Initure);
while(1)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET); //PC13置1
Delay(0x7FFFFF);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET); //PC13置0
Delay(0x7FFFFF);
}
}