1.点亮LED
c文件:
// bsp :board support package 板级支持包
#include "bsp_led.h"
//.h文件中放的是宏,当以后需要修改硬件部分时,只需要修改宏就完事了,不需要在函数中修改
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct; //GPIO_InitTypeDef是GPIO头文件里的结构体
//GPIO_InitStruct是定义的初始化结构体
RCC_APB2PeriphClockCmd(LED_G_GPIO_CLK, ENABLE);//
GPIO_InitStruct.GPIO_Pin = LED_G_GPIO_PIN; //PIN脚
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; //速度
GPIO_Init(LED_G_GPIO_PORT, &GPIO_InitStruct); //GPIO初始化函数
}
h文件:
#ifndef __BSP_LED_H
#define __BSP_LED_H
#include "stm32f10x.h"
#define LED_G_GPIO_PIN GPIO_Pin_0 //PIN脚
#define LED_G_GPIO_PORT GPIOB //端口
#define LED_G_GPIO_CLK RCC_APB2Periph_GPIOB//时钟
#define ON 1
#define OFF 0
// \ C语言里面叫续行符,后面不能有任何的东西
#define LED_G(a) if(a) \
GPIO_ResetBits(LED_G_GPIO_PORT, LED_G_GPIO_PIN); \
else GPIO_SetBits(LED_G_GPIO_PORT, LED_G_GPIO_PIN);
//以上为带参宏的写法,a为形参
void LED_GPIO_Config(void); //初始化函数放入头文件
#endif /* __BSP_LED_H */
main.c:
#include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h>
#include "bsp_led.h"
void Delay( uint32_t count )
{
for(; count!=0; count--);
}
int main(void)
{
// 来到这里的时候,系统的时钟已经被配置成72M。
LED_GPIO_Config();
while(1)
{
//GPIO_SetBits(LED_G_GPIO_PORT, LED_G_GPIO_PIN);
LED_G(OFF);
Delay(0xFFFFF);
//GPIO_ResetBits(LED_G_GPIO_PORT, LED_G_GPIO_PIN);
LED_G(ON);
Delay(0xFFFFF);
}
}
2.按键检测
由于火哥的开发板已经有了硬件消抖,所以就没有写软件消抖了。
c文件:
#include "bsp_key.h"
void KEY_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK, ENABLE);
GPIO_InitStruct.GPIO_Pin = KEY1_GPIO_PIN; //脚
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入模式
GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStruct);
}
uint8_t Key_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin)
{
if( GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON )
{
// 松手检测
while( GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON );//当摁下时,一直在内循环
return KEY_ON; //当松手时,跳出循环,返回1,所以为8位函数
}
else return KEY_OFF;
}
h文件:
#ifndef __BSP_KEY_H
#define __BSP_KEY_H
#include "stm32f10x.h"
#define KEY_ON 1
#define KEY_OFF 0
#define KEY1_GPIO_PIN GPIO_Pin_0
#define KEY1_GPIO_PORT GPIOA
#define KEY1_GPIO_CLK RCC_APB2Periph_GPIOA
void KEY_GPIO_Config(void);
uint8_t Key_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin);
#endif /* __BSP_KEY_H */
man.c:
#include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h>
#include "bsp_led.h"
#include "bsp_key.h"
void Delay( uint32_t count )
{
for(; count!=0; count--);
}
int main(void)
{
// 来到这里的时候,系统的时钟已经被配置成72M。
LED_GPIO_Config();
KEY_GPIO_Config();
while(1)
{
if( Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) ==KEY_ON )
LED_G_GPIO_PORT->ODR ^= LED_G_GPIO_PIN; //翻转一次
// if
}
}
简单的东西又看了这么久,哎,心态有点崩。
还有两篇文章要写要改,加油吧。