您当前的位置: 首页 >  stm32

仙剑情缘

暂无认证

  • 3浏览

    0关注

    333博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

STM32F103VE GPIO输入输出功能

仙剑情缘 发布时间:2019-10-27 18:02:57 ,浏览量:3

  • 使用标准库配置GPIO输出功能
使能外设GPIO时钟,函数RCC_APB2PeriphClockCmd 

使用GPIO_Init函数初时GPIO

GPIO_ResetBits 函数

GPIO_SetBits函数

GPIO_WriteBit函数

GPIO_Write函数

假如有PC6,PC7,PD6三个IO口输出来驱动LED 

使它们简单地闪烁,代码如下

#include "stm32f10x.h"

void delay(u32 ms)
{
	u32 i;
	while(ms--)
	{
		for(i=1000;i>0;i--);
	}
}
int main (void)
{
	 SystemInit();

         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);  // Enable GPIOA clock
	 GPIO_InitTypeDef GPIO_InitStructure; 
		
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; 
	
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    // 推挽输出
	 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	 GPIO_Init(GPIOC, &GPIO_InitStructure);  
	 GPIO_Init(GPIOD, &GPIO_InitStructure);  
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; 
	 GPIO_Init(GPIOC, &GPIO_InitStructure);  
	
	while(1)		
	{
		  GPIO_ResetBits(GPIOC, GPIO_Pin_6 );   //Method 1
		 delay(2000);
		 GPIO_SetBits(GPIOC, GPIO_Pin_6 );  
		 delay(2000);
		
		GPIO_WriteBit(GPIOC, GPIO_Pin_7, Bit_SET);   // Method 2
		delay(2000);
			GPIO_WriteBit(GPIOC, GPIO_Pin_7, Bit_RESET); 
		delay(2000);
		
		 GPIO_Write(GPIOD, GPIO_Pin_6);   //Method 3
		delay(2000);
		 GPIO_Write(GPIOD, ~GPIO_Pin_6); 
		delay(2000);
	}
}
  • GPIO输入功能 
GPIO_ReadInputDataBit函数

GPIO_ReadInputData函数  

GPIO_ReadOutputData函数

 

假定如下图,一个按键控制一个LED

实现代码如下:

#include "stm32f10x.h"

u16 pinVal;
void delay(u32 ms)
{
	u32 i;
	while(ms--)
	{
		for(i=1000;i>0;i--);
	}
}
int main (void)
{
	SystemInit();

  RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);  // Enable GPIO clock
  GPIO_InitTypeDef GPIO_InitStructure; 
		
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; 
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    // 浮空输入
	 GPIO_Init(GPIOE, &GPIO_InitStructure);  
	 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;  
	GPIO_Init(GPIOE, &GPIO_InitStructure); 
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;  
	GPIO_Init(GPIOE, &GPIO_InitStructure); 

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;  
	GPIO_Init(GPIOE, &GPIO_InitStructure); 
	
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    // 推挽输出
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; 
        GPIO_Init(GPIOC, &GPIO_InitStructure);  
	 
        GPIO_Init(GPIOD, &GPIO_InitStructure);   
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; 
	GPIO_Init(GPIOC, &GPIO_InitStructure); 
	 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; 
	GPIO_Init(GPIOD, &GPIO_InitStructure); 
	
	while(1)		
	{
		pinVal = GPIO_ReadInputData(GPIOE);
		if(pinVal & GPIO_Pin_2)
			GPIO_ResetBits(GPIOC,GPIO_Pin_6);
		else	
			GPIO_SetBits(GPIOC,GPIO_Pin_6);
		
	        if(pinVal & GPIO_Pin_3)
			GPIO_ResetBits(GPIOC,GPIO_Pin_7);
		else
			GPIO_SetBits(GPIOC,GPIO_Pin_7);
		if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4))
			GPIO_WriteBit(GPIOD,GPIO_Pin_6,Bit_RESET);
		else
			GPIO_WriteBit(GPIOD,GPIO_Pin_6,Bit_SET);
		
		if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_5))
			GPIO_ResetBits(GPIOD,GPIO_Pin_13);
		else
			GPIO_SetBits(GPIOD,GPIO_Pin_13);
		
	}
}

 

 

关注
打赏
1658017818
查看更多评论
立即登录/注册

微信扫码登录

0.0359s