STM32学习历程(day3)

通过GPIO点灯

首先先创建工程 这步比较繁琐 可以去参考江协科技[3-2]章节

想要驱动LED灯 要先使能时钟、然后再初始化、GPIO模式、引脚、以及输出速率

可以查看RCC的头文件 能看到三个使能函数 使能AHB、APB2、APB1 ,GPIO用APB2这个函数、

通过看RCC库函数的源码能知道 第一个参数是设备,第二个参数是使能或失能

void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);
/**
  * @brief  Enables or disables the High Speed APB (APB2) peripheral clock.
  * @param  RCC_APB2Periph: specifies the APB2 peripheral to gates its clock.
  *   This parameter can be any combination of the following values:
  *     @arg RCC_APB2Periph_AFIO, RCC_APB2Periph_GPIOA, RCC_APB2Periph_GPIOB,
  *          RCC_APB2Periph_GPIOC, RCC_APB2Periph_GPIOD, RCC_APB2Periph_GPIOE,
  *          RCC_APB2Periph_GPIOF, RCC_APB2Periph_GPIOG, RCC_APB2Periph_ADC1,
  *          RCC_APB2Periph_ADC2, RCC_APB2Periph_TIM1, RCC_APB2Periph_SPI1,
  *          RCC_APB2Periph_TIM8, RCC_APB2Periph_USART1, RCC_APB2Periph_ADC3,
  *          RCC_APB2Periph_TIM15, RCC_APB2Periph_TIM16, RCC_APB2Periph_TIM17,
  *          RCC_APB2Periph_TIM9, RCC_APB2Periph_TIM10, RCC_APB2Periph_TIM11     
  * @param  NewState: new state of the specified peripheral clock.
  *   This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
  assert_param(IS_FUNCTIONAL_STATE(NewState));
  if (NewState != DISABLE)
  {
    RCC->APB2ENR |= RCC_APB2Periph;
  }
  else
  {
    RCC->APB2ENR &= ~RCC_APB2Periph;
  }
}

使能完时钟后,接着就是初始化GPIO 也是通过查看GPIO的头文件及源码 获取到他编写的库函数

 一般常用的就是下面这些GPIO库函数 

GPIO_DeInit 是取消初始化 将其恢复到默认值

GPIO_AFIODeInit 同上

GPIO_Init 需要创建一个结构体 然后传他的地址 这个结构体有三个变量GPIO_Pin、GPIO_Speed、GPIO_Mode

就不做过多解释了哈 一般都能见名知意 主要说下设置寄存器这几个函数

GPIO_SetBits 是将传进来这个引脚的值设为高电平

GPIO_ResetBits是将其传进来的引脚设为低电平

GPIO_WriteBit 可以通过第三个参数来决定传进来的引脚是高电平还是低电平

GPIO_Write 可以直接在第二个参数传16进制数 来达到操控寄存器的目的

void GPIO_DeInit(GPIO_TypeDef* GPIOx);
void GPIO_AFIODeInit(void);
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
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);

下面就是一个流水灯的源码

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStruct);
	
	while (1)
	{
		GPIO_Write(GPIOA, ~0x0001); //0000 0000 0000 0001
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0002); //0000 0000 0000 0010
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0004); //0000 0000 0000 0100
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0008); //0000 0000 0000 1000
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0010); //0000 0000 0001 0000
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0020); //0000 0000 0010 0000
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0040); //0000 0000 0100 0000
		Delay_ms(100);
		GPIO_Write(GPIOA, ~0x0080); //0000 0000 1000 0000
		Delay_ms(100);
	}
}

通过GPIO驱使蜂鸣器

有了之前点亮流水灯的学习 这个就是非常简单了

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
int main()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStruct);
	
	while (1)
	{
		GPIO_ResetBits(GPIOB, GPIO_Pin_12); 
		Delay_ms(100);
		GPIO_SetBits(GPIOB, GPIO_Pin_12); 
		Delay_ms(100);
		GPIO_ResetBits(GPIOB, GPIO_Pin_12); 
		Delay_ms(100);
		GPIO_SetBits(GPIOB, GPIO_Pin_12); 
		Delay_ms(700);
		
	}
}

总结 

这个面包板非常好用 能diy自己想要的各种电路 ,还是闹了个小乌龙 前面不知道有更小一点的短接线 我直接用了这个很长的电线 导致看起来就很丑 不过效果还是一样的。今天还是和昨天一样 在公司午休的时候小刷了一下,然后下班回家就开始组装电路,然后创建工程、开始跟着写代码 先是跟着敲了一遍,实现功能后就又自己去查找头文件 看各个函数的功能及参数 至少保证能够看懂代码 如果不做巩固的话 之后缺陷就越来越多 可能会导致没有兴趣会继续学下去。这两个工程还是很简单的哈

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值