在嵌入式中,點亮一盞燈,猶如“hello word”般重要
點亮一盞燈,是小事,成功點亮是大事
而我本人習慣一類文件放在一個 .c 文件裏
那麼,我們先在USER 目錄中,添加 gpio.c 和 gpio.h 以及 led.c led.h文件
然後點擊這個位置,將.c文件加入到項目中
添加成功之後
在gpio.c文件中寫入
#include "stm32f10x.h"
#include "gpio.h"
/*電路設計是 GPIOB01 引腳控制LED */
void GPIO_Config_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO 結構體
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //開啓GPIOB的時鐘
GPIO_InitStructure.GPIO_Pin = LED_GPIO; //定時GPIO口
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//該GPIO的輸出模式
GPIO_Init(LED_GPIO_Port,&GPIO_InitStructure);
}
在gpio.h文件中寫入
#ifndef __GPIO_H__
#define __GPIO_H__
#define LED_GPIO GPIO_Pin_1 //定義引腳宏
#define LED_GPIO_Port GPIOB
void GPIO_Config_Init(void); //函數聲明
#endif
在led.c中輸入
#include "led.h"
void LED_ON()
{
GPIO_SetBits(GPIOB,GPIO_Pin_1); //輸出高電平
}
void LED_OFF()
{
GPIO_ResetBits(GPIOB,GPIO_Pin_1);//輸出低電平
}
在led.h中輸入
#ifndef __LED_H__
#define __LED_H__
#include "stm32f10x.h"
#include "gpio.h"
void LED_ON();
void LED_OFF();
#endif
然後在main函數中,調用即可
在main.c文件中加入以下代碼
#include "stm32f10x.h"
#include <stdio.h>
#include "led.h"
#include "gpio.h"
int main(void)
{
GPIO_Config_Init();//GPIO 初始化
while(1)
{
LED_ON();
// LED_OFF();
}
}
編譯,燒錄,看看效果
如果沒有點亮,看看是不是時鐘沒有配置正確,是否enable了?
有啥問題,歡迎添加微信:LinLinux6_13 ,互相學習