从零开始的stm32学习(一):点灯

一、 引入头文件

#include "stm32f10x.h"                  // Device header

二、操作GPIO的三步:

(一)RCC开启GPIO时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);  

(二)初始化GPIO

  1. GPIO引脚定义
GPIO_InitTypeDef GPIO_InitStructure;
  1. GPIO输出模式定义
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

GPIO输出模式
GPIO模式

  1. GPIO引脚赋值
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0 ;
  1. GPIO频率选择
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
  1. GPIO初始化
GPIO_Init(GPIOA,&GPIO_InitStructure);

由于使用的是GPIOA 0号引脚,故有3和5中的定义

(三)四种输出函数控制GPIO

  1. GPIO_ResetBits()(将指定引脚设置为低电平)
    GPIO_ResetBits
GPIO_ResetBits (GPIOA,GPIO_Pin_0);
  1. GPIO_SetBits()(将指定引脚设置为高电平)

GPIO_SetBits

GPIO_SetBits (GPIOA,GPIO_Pin_0);
  1. GPIO_WriteBit()(对指定引脚写入高或低)GPIO_WriteBit
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);

上代码块中 Bit_RESET=(BitAcition)0。Bit_SET=(BitAction)1。

  1. GPIO_Write()(写入GPIO端口的值)
    GPIO_Write()
GPIO_Write(GPIOA,~0x0001);

低电平点亮,因此有~取反。

三、引入Delay函数(闪烁,流水灯可用)

在Target下新建System文件夹,将Delay.h,Delay.c存放在里面。

#include "Delay.h"
delay_ms(100)

四、合并代码

#include "stm32f10x.h"                  // Device header

int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0 ;
	GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
    GPIO_ResetBits (GPIOA,GPIO_Pin_0);
	while(1){
	
	}
}

五、连接电路

点灯电路图

至此,就完成了点灯

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值