stm32点亮LED 测试代码及目录结构

.

main.c - 使用PB12, PB13, PB14, PB15, PB5, PB6, PB7 这七个PB口点亮LED. 注意PB3和PB4是特殊口, 直接调用无效.

#include "delay.h"
#include "stm32f10x.h"

int main(void) {
    u16 i;
    u8 j;
    uint16_t gpios[] = {GPIO_Pin_12, GPIO_Pin_13, GPIO_Pin_14, GPIO_Pin_15, GPIO_Pin_5, GPIO_Pin_6, GPIO_Pin_7};
    GPIO_InitTypeDef  GPIO_InitStructure;
    delay_init(); //延时函数初始化      
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /使能PB端口时钟
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
    GPIO_Init(GPIOB, &GPIO_InitStructure);  //根据设定参数初始化GPIOB.5

    while(1) {
        i = (i+20) % 500;
        for (j = 0; j < 6; j++) {
            GPIO_SetBits(GPIOB, gpios[j]);
            delay_ms(i);
            GPIO_ResetBits(GPIOB,gpios[j]);
        }
        for (j = 6; j > 0; j--) {
            GPIO_SetBits(GPIOB, gpios[j]);
            delay_ms(i);
            GPIO_ResetBits(GPIOB,gpios[j]);
        }
    }
}

.

makefile

#工程的名称及最后生成文件的名字
TARGET = test001

#设定临时性环境变量
export CC             = arm-none-eabi-gcc           
export AS             = arm-none-eabi-as
export LD             = arm-none-eabi-ld
export OBJCOPY        = arm-none-eabi-objcopy

#读取当前工作目录
TOP=$(shell pwd)

#设定包含文件目录
INC_FLAGS= -I $(TOP)/core \
           -I $(TOP)/stm32f10x_lib/inc \
           -I $(TOP)/system \
           -I $(TOP)/user

CFLAGS =  -W -Wall -g -mcpu=cortex-m3 -mthumb -D STM32F10X_HD -D USE_STDPERIPH_DRIVER $(INC_FLAGS) -O0 -std=gnu11
C_SRC=$(shell find ./ -name '*.c')  
C_OBJ=$(C_SRC:%.c=%.o)          

.PHONY: all clean update      

all:$(C_OBJ)
    $(CC) $(C_OBJ) -T stm32_f103ze_gcc.ld -o $(TARGET).elf   -mthumb -mcpu=cortex-m3 -Wl,--start-group -lc -lm -Wl,--end-group -specs=nano.specs -specs=nosys.specs -static -Wl,-cref,-u,Reset_Handler -Wl,-Map=Project.map -Wl,--gc-sections -Wl,--defsym=malloc_getpagesize_P=0x80 
    $(OBJCOPY) $(TARGET).elf  $(TARGET).bin -Obinary 
    $(OBJCOPY) $(TARGET).elf  $(TARGET).hex -Oihex

$(C_OBJ):%.o:%.c
    $(CC) -c $(CFLAGS) -o $@ $<
clean:
    rm -f $(shell find ./ -name '*.o')
    rm -f $(shell find ./ -name '*.d')
    rm -f $(shell find ./ -name '*.map')
    rm -f $(shell find ./ -name '*.elf')
    rm -f $(shell find ./ -name '*.bin')
    rm -f $(shell find ./ -name '*.hex')

update:
    openocd \
        -f /usr/local/share/openocd/scripts/interface/stlink-v2.cfg  \
        -f /usr/local/share/openocd/scripts/target/stm32f1x.cfg \
        -c init \
        -c halt \
        -c "flash write_image erase $(TOP)/test001.hex" \
        -c reset \
        -c shutdown

 

目录结构

├── core
│   ├── core_cm3.h
│   ├── core_cmFunc.h
│   └── core_cmInstr.h
├── LICENSE
├── makefile
├── README.md
├── stm32_f103ze_gcc.ld
├── stm32f10x_lib
│   ├── inc
│   │   ├── misc.h
│   │   ├── stm32f10x_adc.h
│   │   ├── stm32f10x_bkp.h
│   │   ├── stm32f10x_can.h
│   │   ├── stm32f10x_cec.h
│   │   ├── stm32f10x_conf.h
│   │   ├── stm32f10x_crc.h
│   │   ├── stm32f10x_dac.h
│   │   ├── stm32f10x_dbgmcu.h
│   │   ├── stm32f10x_dma.h
│   │   ├── stm32f10x_exti.h
│   │   ├── stm32f10x_flash.h
│   │   ├── stm32f10x_fsmc.h
│   │   ├── stm32f10x_gpio.h
│   │   ├── stm32f10x.h
│   │   ├── stm32f10x_i2c.h
│   │   ├── stm32f10x_iwdg.h
│   │   ├── stm32f10x_pwr.h
│   │   ├── stm32f10x_rcc.h
│   │   ├── stm32f10x_rtc.h
│   │   ├── stm32f10x_sdio.h
│   │   ├── stm32f10x_spi.h
│   │   ├── stm32f10x_tim.h
│   │   ├── stm32f10x_usart.h
│   │   └── stm32f10x_wwdg.h
│   └── src
│       ├── misc.c
│       ├── stm32f10x_adc.c
│       ├── stm32f10x_bkp.c
│       ├── stm32f10x_can.c
│       ├── stm32f10x_cec.c
│       ├── stm32f10x_crc.c
│       ├── stm32f10x_dac.c
│       ├── stm32f10x_dbgmcu.c
│       ├── stm32f10x_dma.c
│       ├── stm32f10x_exti.c
│       ├── stm32f10x_flash.c
│       ├── stm32f10x_fsmc.c
│       ├── stm32f10x_gpio.c
│       ├── stm32f10x_i2c.c
│       ├── stm32f10x_iwdg.c
│       ├── stm32f10x_pwr.c
│       ├── stm32f10x_rcc.c
│       ├── stm32f10x_rtc.c
│       ├── stm32f10x_sdio.c
│       ├── stm32f10x_spi.c
│       ├── stm32f10x_tim.c
│       ├── stm32f10x_usart.c
│       └── stm32f10x_wwdg.c
├── system
│   ├── delay.c
│   ├── delay.h
│   ├── startup_stm32f10x_hd.c
│   ├── system_stm32f10x.c
│   └── system_stm32f10x.h
└── user
    └── main.c

6 directories, 61 files

 

源码下载 https://pan.baidu.com/s/1J6qEPQIhBId-Zo5nUzOgAQ 密码: 5t3d

.

posted on 2018-04-19 19:28  Milton  阅读(527)  评论(0编辑  收藏  举报

导航