第三章——stm32f103zet6点亮LED

第一章——stm32f103建立工程

第二章——keil5修改工程名字

第三章——stm32f103zet6点亮LED

目录

1.硬件介绍

2.程序编写


1.硬件介绍

LED1接在PG7引脚相连 LED2与PG8相连,当单片机引脚拉低时候led灯被点亮

2.程序编写

打开第一章下载好的固件库里面的STM32F10x_StdPeriph_Lib_V3.6.0\Project\STM32F10x_StdPeriph_Examples\GPIO\IOToggle里面的main.c文件(可以使用记事本直接打开,我这里使用的是Visual C++ 6.0打开的)

 把main.c中的

#include "stm32f10x.h"

GPIO_InitTypeDef GPIO_InitStructure;

/* GPIOD Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

/* Configure PD0 and PD2 in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);

复制下来,粘贴到我们的led工程中main.c中点击编译

因为LED1接在PG7引脚相连 LED2与PG8相连所以代码改为

#include "stm32f10x.h"


GPIO_InitTypeDef GPIO_InitStructure;

int main(void)
{
    
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE);    	//使能GPIO端口时钟

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8;	//设置引脚
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;			//设置输出速率
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;			//设置引脚为通用推挽模式
  GPIO_Init(GPIOG, &GPIO_InitStructure);					//初始化GPIO端口
  while (1)
  {
		
  }
}

在GPIO_Init函数上面右键Go To Definition Of 'GPIO_Init' 后

 进入stm32f10x_gpio.c内,下拉找到

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

两个函数复制到main.c中

两个函数复制到main.c中,输入正确的引脚名称

#include "stm32f10x.h"

GPIO_InitTypeDef GPIO_InitStructure;

int main(void)
{
    
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE);    	//使能GPIO端口时钟

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8;	//设置引脚
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;			//设置输出速率
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;			//设置引脚为通用推挽模式
  GPIO_Init(GPIOG, &GPIO_InitStructure);					//初始化GPIO端口
	
//	GPIO_SetBits(GPIOG,GPIO_Pin_7);							//熄灭led
//	GPIO_SetBits(GPIOG,GPIO_Pin_8);							//熄灭led
	
  GPIO_ResetBits(GPIOG,GPIO_Pin_7);						    //打开led
  GPIO_ResetBits(GPIOG,GPIO_Pin_8);						    //打开led
  while (1)
  {
		
  }
}

点击编译下载到板子上就可以观察到LED1和LED2均亮起了

链接:https://pan.baidu.com/s/1us0kbK5BNjpha0NF2wr3og 
提取码:only

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值