[GPIO]stm32f103c8t6 笔记LED闪烁

1.写LED.c代码2

#include "led.h"
//初始化PB5和PA1为输出口.并使能这两个口的时钟            
//LED IO口初始化
void LED_Init(void)
{
 
 GPIO_InitTypeDef  GPIO_InitStructure;  //定义相关结构体
     
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);     
//使能PA,PB,PC端口时钟
    
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;                 //管脚 端口配置
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;          //推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         //IO口速度为50MHz
 GPIO_Init(GPIOA, &GPIO_InitStructure);                     //根据设定参数初始化GPIOA1

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;                 //管脚 端口配置
 GPIO_Init(GPIOB, &GPIO_InitStructure);                       //推挽输出 ,IO口速度为50MHz

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
 GPIO_Init(GPIOC, &GPIO_InitStructure);
    
}

2.写LED.h 代码 

#ifndef __LED_H
#define __LED_H	 
#include "sys.h"

#define LEDA1 PAout(1)   // 对 A1 IO口输出进行操作
#define LEDB5 PBout(5)   // 对 B5 IO口输出进行操作

void LED_Init(void);   //初始化

//或者直接对IO口的电平进行操作,这里以PC13 IO口举例
#define LED_OFF GPIO_SetBits(GPIOC,GPIO_Pin_13)			//拉高电平
#define LED_ON GPIO_ResetBits(GPIOC,GPIO_Pin_13)		//拉低电平
#define LED_REV GPIO_WriteBit(GPIOC, GPIO_Pin_13,(BitAction)(1-(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_13)))) //对输出的bit进行修改

		 				    
#endif

 3.写main.c代码

#include "sys.h"
#include "delay.h"
#include "led.h"
 
 
 int main(void)
 {	
	delay_init();	    //延时函数初始化	  
	LED_Init();		  	//LED灯初始化
	while(1)
	{
		LEDA1=0;				//给A1口低电平	
		LEDB5=1;				//给B5口高电平      两者形成电势差,小灯点亮
		delay_ms(500);	 //延时500ms
		LEDA1=1;				//给A1口高电平
		LEDB5=0;				//给B5口低电平      二极管单向导电性,小灯灭
		delay_ms(500);	//延时500ms
	}
 }

 (视频不懂上传,下一次视频)

笔记:

1.GPIO_SetBits函数为拉高电平

   GPIO_ResetBits函数为拉低电平

2.GPIO_WriteBit函数对单个IO口的电平进行操作

   GPIO的八种工作模式去掌握

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值