4 重新组织MDK工程
将LED作为驱动代码单独提取出来,划分到驱动的层次中。首先编写led.h文件,来定义类型和接口,代码如下。
#ifndef LED_H
#define LED_H
typedef enum led_num
{
LED0,
LED1
} led_num_t;
void led_init(void);
void led_on(led_num_t num);
void led_off(led_num_t num);
#endif
实现led.h文件中定义的接口函数,编写在led.c源文件中,代码如下(根据需要添加其他LED灯的else代码)。
#include "stm32f10x.h" // Device header
#include "led.h"
void led_init(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
GPIOB->CRL = (GPIOB->CRL & ~(0xF << 20)) | (0x2 << 20);
}
void led_on(led_num_t num)
{
if (num == LED0)
GPIOB->BSRR = 1 << (5 + 16);
}
void led_off(led_num_t num)
{
if (num == LED0)
GPIOB->BSRR = 1 << 5;
}
这里解释一下怎么将之前的
unsigned int *RCC_APB2ENR = (unsigned int *)0x40021018;