32单片机2.点亮LED灯

单片机部分LED原理图:

由图可知,单片机上一共三个灯,分别是:端口是GPIOB0pin的绿灯,端口是GPIOB1pin的蓝灯,端口是GOIOB5pin的红灯,他们的共阳极接在了3.3v上,如果想要其中的一个灯亮,只需要给其对应的pin幅值为0就可以。

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);

以上的函数是我们常用给单片机pin脚赋值的函数。第一个是置位函数,参数是接口和接口的pin,第二个函数是复位函数,参数和置位函数一样,第三个是写入函数,参数是接口、pin脚、和要写入的值,第四个是对整个接口一起赋值,参数是接口,和要赋的值。

因为我们要使用端口,所以先要配置端口的参数。就要用到这个函数

/**
  * @brief  Initializes the GPIOx peripheral according to the specified
  *         parameters in the GPIO_InitStruct.
  * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  * @param  GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that
  *         contains the configuration information for the specified GPIO peripheral.
  * @retval None
  */
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

这个函数的第一个参数是我们要初始化的端口,第二个参数我们需要一个结构体指针。这个结构体在“stm32f10x_gpio.h”这个头文件定义过了

typedef struct
{
  uint16_t GPIO_Pin;             /*!< Specifies the GPIO pins to be configured.
                                      This parameter can be any value of @ref GPIO_pins_define */

  GPIOSpeed_TypeDef GPIO_Speed;  /*!< Specifies the speed for the selected pins.
                                      This parameter can be a value of @ref GPIOSpeed_TypeDef */

  GPIOMode_TypeDef GPIO_Mode;    /*!< Specifies the operating mode for the selected pins.
                                      This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;

我们需要干的事情,就是先定义以一个结构体,然后给结构体赋值,来达到配置端口的作用。然后通过GPIO_Init这个函数通过定义好的结构体的地址,把其中相应的值赋给端口,我们此时要使用的接口是GPIOB

    GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_5;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);

把端口设置成推挽输出模式,使用的pin脚为5,频率是10MHz。

配置完成,按理来说已经完成了点亮LED灯的点亮。但是下载进单片机发现单片机并没有亮灯反应。使用APB2外设的话,必须先打开APB2的时钟才行

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

这样就可以打开APB2外设的GPIOB时钟,就可以正常使用了。

全部代码如下

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_5;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);
	GPIO_WriteBit(GPIOB,GPIO_Pin_0,Bit_RESET);
	while(1)
	{
		GPIO_WriteBit(GPIOB,GPIO_Pin_5,Bit_RESET);
		Delay_ms(500);
		GPIO_WriteBit(GPIOB,GPIO_Pin_5,Bit_SET);
		Delay_ms(500);
	}
}

实现了一个灯,那其他两个灯也同理,整合一下,写了一个流水灯

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_0|GPIO_Pin_1;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_10MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);
	while(1)
	{
		GPIO_WriteBit(GPIOB,GPIO_Pin_5,Bit_RESET);
		Delay_ms(1000);
		GPIO_WriteBit(GPIOB,GPIO_Pin_5,Bit_SET);
		Delay_ms(1000);
		GPIO_WriteBit(GPIOB,GPIO_Pin_1,Bit_RESET);
		Delay_ms(1000);
		GPIO_WriteBit(GPIOB,GPIO_Pin_1,Bit_SET);
		Delay_ms(1000);
		GPIO_WriteBit(GPIOB,GPIO_Pin_0,Bit_RESET);
		Delay_ms(1000);
		GPIO_WriteBit(GPIOB,GPIO_Pin_0,Bit_SET);
		Delay_ms(1000);
	}
}

值得一提的是我们在设置结构体的pin脚的时候,可以设置多个pin脚,用|(或)连接起来就行。

第二天学了点亮LED灯,第三天加油!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值