stm32学习之GPIO_LED
今天开始写stm32,先增加一个hareware文件夹来存放led.c和led.h,然后就开始编写了;
led.h
#ifndef __LED_H #define __LED_H void LED_Init(void); #endif
led.c
1 #include "led.h" 2 #include "stm32f10x.h" 3 4 void LED_Init(void) 5 { 6 7 GPIO_InitTypeDef GPIO_InitStructure; //指针形式,为GPIO_InitTypeDef命名 8 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能GPIO_A,在stm32f10x_gpio.c可以找到对应的入口参数 9 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);//使能GPIO_D 10 11 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //输出模式,在stmf10x_gpio.h可以找到对应的入口参数,及其他模式选择 12 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8; //指定输出引脚,在stmf10x_gpio.h可以找到对应的入口参数,及其他Pin脚选择 13 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //输出速率,在stmf10x_gpio.h可以找到对应的入口参数,及其他速率选择 14 GPIO_Init(GPIOA,&GPIO_InitStructure); // 根据GPIO_InitStruct中指定的参数初始化外设GPIOx寄存器,输入参数1: GPIOx:x可以是A,B,C,D或者E,来选择GPIO外设,输入参数2: GPIO_InitStruct:指向结构GPIO_InitTypeDef的指针,包含了外设GPIO的配置信息。 15 GPIO_SetBits(GPIOA,GPIO_Pin_8); //初始化引脚 16 17 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; 18 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; 19 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; 20 GPIO_Init(GPIOD,&GPIO_InitStructure); 21 GPIO_SetBits(GPIOD,GPIO_Pin_2); 22 23 }
准备工作做好后,就可以编写主程序了
main.c
1 #include "led.h" 2 #include "stm32f10x.h" 3 #include "delay.h" 4 5 int main(void) 6 { 7 delay_init(); 8 LED_Init(); 9 10 while(1) 11 { 12 GPIO_SetBits(GPIOA,GPIO_Pin_8); 13 GPIO_SetBits(GPIOD,GPIO_Pin_2); 14 delay_ms(500); 15 GPIO_ResetBits(GPIOA,GPIO_Pin_8); 16 GPIO_ResetBits(GPIOD,GPIO_Pin_2); 17 delay_ms(500); 18 19 20 } 21 22 }
在编写程序的时候,遇到了以下三个问题:
1)USER\main.c(22): warning: #1-D: last line of file ends without a newline;
解决方法:代码最后一行以“enter”按键(换行符)为结束;
2)
解决方法:
图中的_sys_exit(int x)改为void _sys_exit(int x);
3)
图中的USER文件夹要添加main.c文件。
最后,学程序最主要是弄懂原理,而不只是会搭建程序,知其然而知其所以然是最重要的。

浙公网安备 33010602011771号