目录
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